List of usage examples for com.itextpdf.text.pdf PdfContentByte addTemplate
public void addTemplate(final PdfTemplate template, final double a, final double b, final double c, final double d, final double e, final double f)
From source file:de.aidger.utils.pdf.BalanceReportConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * /*from w w w.j av a 2 s.c om*/ * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File( Runtime.getInstance().getConfigPath() + "/templates/BalanceReportTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/BalanceReportTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4.rotate()); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.aidger.utils.pdf.BudgetReportConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * // ww w .ja v a 2s .c o m * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File(Runtime.getInstance().getConfigPath() + "/templates/BudgetReportTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/BudgetReportTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.aidger.utils.pdf.ControllingConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * //from w w w .ja v a2 s . c o m * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File(Runtime.getInstance().getConfigPath() + "/templates/ControllingTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/ControllingTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.aidger.utils.pdf.ProtocolConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * //w w w . j a v a2 s . c om * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File(Runtime.getInstance().getConfigPath() + "/templates/ProtocolTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/ProtocolTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4.rotate()); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.mat.utils.pdftools.PdfResize.java
License:Mozilla Public License
/** /**/*ww w. ja v a 2s. c o m*/ * <h4>FeatureDomain:</h4> * PublishingTools * <h4>FeatureDescription:</h4> * scales and move comntents of the pdf pages from fileSrc and output to * fileNew * <h4>FeatureResult:</h4> * <ul> * <li>create PDF - fileNew * </ul> * <h4>FeatureKeywords:</h4> * PDF Publishing * @param fileSrc - source-pdf * @param fileNew - scaled dest-pdf * @param factorX - scaling x * @param factorY - scaling y * @param pixelLeft - move right * @param pixelTop - move down * @throws Exception */ public static void resizePdf(String fileSrc, String fileNew, float factorX, float factorY, float pixelLeft, float pixelTop) throws Exception { // open reader PdfReader reader = new PdfReader(fileSrc); // get pagebasedata int pageCount = reader.getNumberOfPages(); Rectangle psize = reader.getPageSize(1); float width = psize.getHeight(); float height = psize.getWidth(); // open writer Document documentNew = new Document(new Rectangle(height * factorY, width * factorX)); PdfWriter writerNew = PdfWriter.getInstance(documentNew, new FileOutputStream(fileNew)); documentNew.open(); PdfContentByte cb = writerNew.getDirectContent(); // iterate pages int i = 0; while (i < pageCount) { i++; // imoport page from reader and scale it to writer documentNew.newPage(); PdfImportedPage page = writerNew.getImportedPage(reader, i); cb.addTemplate(page, factorX, 0, 0, factorY, pixelLeft, pixelTop); if (LOGGER.isInfoEnabled()) LOGGER.info("AddPage " + i + " from:" + fileSrc + " to:" + fileNew); } documentNew.close(); writerNew.close(); }
From source file:edu.umn.genomics.component.SavePDF.java
License:Open Source License
/** * Saves the Component to a Portable Document File, PDF, with the * file location selected using the JFileChooser. * @param c the Component to save as a PDF *//*from ww w. j av a 2s .c o m*/ public static boolean savePDF(Component c) throws IOException { System.out.println(""); final int w = c.getWidth() > 0 ? c.getWidth() : 1; final int h = c.getHeight() > 0 ? c.getHeight() : 1; final Dimension dim = c.getPreferredSize(); JFileChooser chooser = new JFileChooser(); JPanel ap = new JPanel(); ap.setLayout(new BoxLayout(ap, BoxLayout.Y_AXIS)); JPanel sp = new JPanel(new GridLayout(0, 1)); sp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Image Size")); final JTextField iwtf = new JTextField("" + w, 4); iwtf.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "width")); final JTextField ihtf = new JTextField("" + h, 4); ihtf.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "height")); JButton curSzBtn = new JButton("As Viewed: " + w + "x" + h); curSzBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iwtf.setText("" + w); ihtf.setText("" + h); } }); sp.add(curSzBtn); if (dim != null && dim.getWidth() > 0 && dim.getHeight() > 0) { JButton prefSzBtn = new JButton("As Preferred: " + dim.width + "x" + dim.height); prefSzBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iwtf.setText("" + dim.width); ihtf.setText("" + dim.height); } }); sp.add(prefSzBtn); } sp.add(iwtf); sp.add(ihtf); ap.add(sp); chooser.setAccessory(ap); // ImageFilter filter = new ImageFilter(fmt); // chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(c); boolean status = false; if (returnVal == JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); //Added the below code to add th .pdf extension if it is not provided by the user String name = file.getAbsolutePath(); if (!(name.substring(name.length() - 4, name.length()).equalsIgnoreCase(".pdf"))) { name = name.concat(".pdf"); file = new File(name); } int iw = w; int ih = h; try { iw = Integer.parseInt(iwtf.getText()); ih = Integer.parseInt(ihtf.getText()); } catch (Exception ex) { ExceptionHandler.popupException("" + ex); } iw = iw > 0 ? iw : w; ih = ih > 0 ? ih : h; if (iw != w || ih != h) { c.setSize(iw, ih); } // step 1: creation of a document-object Document document = new Document(); try { // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it // we create a fontMapper and read all the fonts in the font directory DefaultFontMapper mapper = new DefaultFontMapper(); // mapper.insertDirectory("c:\\winnt\\fonts"); com.itextpdf.text.Rectangle pgSize = document.getPageSize(); // we create a template and a Graphics2D object that corresponds with it PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(iw, ih); tp.setWidth(iw); tp.setHeight(ih); Graphics2D g2 = tp.createGraphics(iw, ih, mapper); g2.setStroke(new BasicStroke(.1f)); //cb.setLineWidth(.1f); //cb.stroke(); c.paintAll(g2); g2.dispose(); //cb.addTemplate(tp, 0, 0); float sfx = (float) (pgSize.getWidth() / iw); float sfy = (float) (pgSize.getHeight() / ih); // preserve the aspect ratio float sf = (float) Math.min(sfx, sfy); cb.addTemplate(tp, sf, 0f, 0f, sf, 0f, 0f); } catch (DocumentException de) { ExceptionHandler.popupException("" + de); } catch (IOException ioe) { ExceptionHandler.popupException("" + ioe); } catch (Exception ex) { ExceptionHandler.popupException("" + ex); } // step 5: we close the document document.close(); if (iw != w || ih != h) { c.setSize(w, h); } } return true; }
From source file:nz.ac.waikato.cms.doc.HyperLinkGrades.java
License:Open Source License
/** * Adds the index with locations to the existing PDF. * * @param locations the locations to index * @param input the input PDF/* w w w .java 2s .c o m*/ * @param output the output PDF * @return true if successfully generated */ public static boolean addIndex(List<Location> locations, File input, File output) { try { // copy pages, add target PdfReader reader = new PdfReader(input.getAbsolutePath()); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(output.getAbsolutePath())); document.open(); PdfContentByte canvas = writer.getDirectContent(); PdfImportedPage page; float height = 0; for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); page = writer.getImportedPage(reader, i); canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0); Chunk loc = new Chunk(" "); loc.setLocalDestination("loc" + i); height = page.getHeight(); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(loc), 50, height - 50, 0); } // add index for (int i = 0; i < locations.size(); i++) { Location loc = locations.get(i); if (i % MAX_ITEMS_PER_PAGE == 0) document.newPage(); String text = loc.getID() + " " + (loc.getName() == null ? "???" : loc.getName()); Chunk chunk = new Chunk("Page " + (loc.getPage() + 1) + ": " + text); chunk.setAction(PdfAction.gotoLocalPage("loc" + (loc.getPage() + 1), false)); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(chunk), 50, height - 100 - (i % MAX_ITEMS_PER_PAGE) * 20, 0); } document.close(); return true; } catch (Exception e) { System.err.println("Failed to overlay locations!"); e.printStackTrace(); return false; } }
From source file:org.gmdev.pdftrick.engine.MergeFiles.java
License:Open Source License
/** * Materially multiple pdf files are written merged file on a disk * @param list/* w ww . jav a 2 s.co m*/ * @param outputStream * @throws DocumentException * @throws IOException */ private void doMerge(List<StreamPwdContainer> list, OutputStream outputStream) throws DocumentException, IOException { HashMap<Integer, String> rotationFromPages = factory.getRotationFromPages(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); int z = 0; for (StreamPwdContainer boom : list) { InputStream in = boom.getIn(); PdfReader reader = null; if (!boom.getPwd().equalsIgnoreCase("")) { reader = new PdfReader(in, boom.getPwd().getBytes()); } else { reader = new PdfReader(in); } for (int i = 1; i <= reader.getNumberOfPages(); i++) { z++; int rotation = reader.getPageRotation(i); //set size Rectangle pageSize_ = reader.getPageSize(i); Rectangle pageSize = null; if (rotation == 270 || rotation == 90) { pageSize = new Rectangle(pageSize_.getHeight(), pageSize_.getWidth()); } else { pageSize = pageSize_; } document.setPageSize(pageSize); writer.setCropBoxSize(pageSize); document.newPage(); // import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); // add the page to the destination pdf if (rotation == 270) { cb.addTemplate(page, 0, 1.0f, -1.0f, 0, reader.getPageSizeWithRotation(i).getWidth(), 0); rotationFromPages.put(z, "" + rotation); } else if (rotation == 180) { cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0); rotationFromPages.put(z, "" + rotation); } else if (rotation == 90) { cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight()); rotationFromPages.put(z, "" + rotation); } else { cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } in.close(); } outputStream.flush(); document.close(); outputStream.close(); }