List of usage examples for com.lowagie.text.pdf PdfContentByte addTemplate
public void addTemplate(PdfTemplate template, float x, float y)
From source file:com.eteks.sweethome3d.swing.HomePDFPrinter.java
License:Open Source License
/** * Writes to <code>outputStream</code> the print of a home in PDF format. *//*from w w w. j ava2 s . com*/ public void write(OutputStream outputStream) throws IOException { PageFormat pageFormat = HomePrintableComponent.getPageFormat(this.home.getPrint()); Document pdfDocument = new Document( new Rectangle((float) pageFormat.getWidth(), (float) pageFormat.getHeight())); try { // Get a PDF writer that will write to the given PDF output stream PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, outputStream); pdfDocument.open(); // Set PDF document description pdfDocument.addAuthor(System.getProperty("user.name", "")); String pdfDocumentCreator = this.preferences.getLocalizedString(HomePDFPrinter.class, "pdfDocument.creator"); pdfDocument.addCreator(pdfDocumentCreator); pdfDocument.addCreationDate(); String homeName = this.home.getName(); if (homeName != null) { pdfDocument.addTitle(this.controller.getContentManager().getPresentationName(homeName, ContentManager.ContentType.PDF)); } PdfContentByte pdfContent = pdfWriter.getDirectContent(); HomePrintableComponent printableComponent = new HomePrintableComponent(this.home, this.controller, this.defaultFont); // Print each page for (int page = 0, pageCount = printableComponent.getPageCount(); page < pageCount; page++) { // Check current thread isn't interrupted if (Thread.interrupted()) { throw new InterruptedIOException(); } PdfTemplate pdfTemplate = pdfContent.createTemplate((float) pageFormat.getWidth(), (float) pageFormat.getHeight()); Graphics g = pdfTemplate.createGraphicsShapes((float) pageFormat.getWidth(), (float) pageFormat.getHeight()); printableComponent.print(g, pageFormat, page); pdfContent.addTemplate(pdfTemplate, 0, 0); g.dispose(); if (page != pageCount - 1) { pdfDocument.newPage(); } } pdfDocument.close(); } catch (DocumentException ex) { IOException exception = new IOException("Couldn't print to PDF"); exception.initCause(ex); throw exception; } catch (InterruptedPrinterException ex) { throw new InterruptedIOException("Print to PDF interrupted"); } catch (PrinterException ex) { IOException exception = new IOException("Couldn't print to PDF"); exception.initCause(ex); throw exception; } }
From source file:com.exam.server.ConvertPDF.java
public static void addPieChart(JFreeChart chart, int width, int height, PdfWriter writer) { // PdfWriter writer = null; // Document document = new Document(); try {/*from w ww. ja va 2 s.co m*/ // writer = PdfWriter.getInstance(document, new FileOutputStream( // fileName)); System.out.println("writing pie chart document "); // document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); } catch (Exception e) { e.printStackTrace(); } System.out.println("writing done:: "); }
From source file:com.iana.boesc.utility.BOESCUtil.java
License:Open Source License
/** * /*from ww w . j a va2s . co m*/ * @param list * @param outputStream * @throws DocumentException * @throws IOException */ public static void doMerge(List<InputStream> list, OutputStream outputStream) throws DocumentException, IOException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); for (InputStream in : list) { PdfReader reader = new PdfReader(in); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); // import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); // add the page to the destination pdf cb.addTemplate(page, 0, 0); } } outputStream.flush(); document.close(); outputStream.close(); }
From source file:com.jk.framework.pdf.PDFMergeUtil.java
License:Apache License
/** * Concat PD fs./* w w w .java 2 s. com*/ * * @param pdfs * the pdfs * @param outputStream * the output stream * @param paginate * the paginate * @throws PdfException * the pdf exception */ // //////////////////////////////////////////////////////////////// public static void concatPDFs(final List<InputStream> pdfs, final OutputStream outputStream, final boolean paginate) throws PdfException { final Document document = new Document(); try { final List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; final Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { final InputStream pdf = iteratorPDFs.next(); final PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream final PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); // BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, // BaseFont.CP1252, BaseFont.NOT_EMBEDDED); final PdfContentByte cb = writer.getDirectContent(); // Holds the // PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; final Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { final PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); // cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (final Exception e) { throw new PdfException(e); } finally { if (document.isOpen()) { document.close(); } try { if (outputStream != null) { outputStream.close(); } } catch (final IOException ioe) { ioe.printStackTrace(); } } }
From source file:com.logiware.accounting.reports.ArDisputeReportCreator.java
@Override public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();/*from w w w. j a v a 2s . co m*/ String text = "Page " + writer.getPageNumber() + " of "; float textBase = document.bottom() - 20; float textSize = helvFont.getWidthPoint(text, 12); cb.beginText(); cb.setFontAndSize(helvFont, 12); cb.setTextMatrix((document.right() / 2) - (textSize / 2), textBase); cb.showText(text); cb.endText(); cb.addTemplate(pageTemplate, (document.right() / 2) + (textSize / 2), textBase); cb.restoreState(); }
From source file:com.moss.check.us.CheckPdfRenderer.java
License:Open Source License
public void render(CheckModel model, OutputStream out) throws Exception { Document document = new Document(); document.setPageSize(new Rectangle(PAGE_WIDTH, PAGE_HEIGHT)); PdfWriter writer = PdfWriter.getInstance(document, out); document.open();/*ww w. j av a2 s. com*/ PdfContentByte cb = writer.getDirectContent(); Check check = new Check(); check.defaultFont = BaseFont.createFont("Helvetica", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); check.defaultFontSize = 8; check.defaultFontLeading = 10; check.largeFontSize = 9; check.largeFontLeading = 12; check.fixedWidthFont = createFixedFont(); check.fixedWidthFontSize = 8; check.voidFont = BaseFont.createFont("Helvetica-Bold", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); check.voidFontSize = 14; check.micrFont = createMicrFont(); check.micrFontSize = 12; check.model = model; check.x = 0; check.y = 0; check.renderMode = CheckRenderMode.CHECK; check.render(cb); if (StubPrintMode.CHECK_DUPLICATE == model.stubPrintMode) { check.renderMode = CheckRenderMode.STUB; check.y = document.top() - (8.2f * POINTS_IN_A_CM); check.render(cb); } else if (StubPrintMode.CUSTOM == model.stubPrintMode) { PdfReader reader = new PdfReader(model.customStubPdf); PdfImportedPage customPage = writer.getImportedPage(reader, 1); cb.addTemplate(customPage, 0f, 0f); } else { throw new RuntimeException("Unknown stub print mode: " + model.stubPrintMode); } document.close(); }
From source file:com.orange.atk.atkUI.corecli.utils.PdfUtilities.java
License:Apache License
public void addTemplate(String pdfFileName, String templatePDFFileName) throws Exception { // see example on http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/copystamp/AddWatermarkPageNumbers.java // 1. copy/*w w w . j av a 2s .c om*/ File tmpPDFFile = new File(tmpDir, "tmpPDF.pdf"); copyFile(new File(pdfFileName), tmpPDFFile); // 2. add template on all pages // we create a reader for a certain document PdfReader reader = new PdfReader(tmpPDFFile.getAbsolutePath()); // we create a stamper that will copy the document to a new file PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(pdfFileName)); // adding content to each page int n = reader.getNumberOfPages(); int i = 0; // a reader for the template document PdfReader reader2 = new PdfReader(templatePDFFileName); PdfContentByte under; while (i < n) { i++; // template under the existing page under = stamp.getUnderContent(i); //under.addTemplate(stamp.getImportedPage(reader2, 1), 1, 0, 0, 1, 0, 0); under.addTemplate(stamp.getImportedPage(reader2, 1), -10, -50); } // closing PdfStamper will generate the new PDF file stamp.close(); }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildFooter(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();/* w w w . j av a 2s. c o m*/ String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.bottom() - 25; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.bottom() - 10); cb.lineTo(document.right(), document.bottom() - 10); cb.stroke(); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7); cb.setTextMatrix(document.right() - textSize, textBase - 10); cb.showText(footer.getGeneratedBy()); textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7); cb.setTextMatrix(document.right() - textSize, textBase - 20); cb.showText(generationDate); cb.endText(); try { textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7); ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()), document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10, Element.ALIGN_LEFT); ct.go(); } catch (DocumentException e) { LOG.warn("Problem with additional text generation in report footer."); } try { ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10, Element.ALIGN_LEFT); ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light())); if (!"".equals(footer.getAddress())) { ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light())); } if (!"".equals(footer.getPhoneEmail())) { ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light())); } ct.go(); } catch (DocumentException e) { LOG.warn("Problem with company text generation in report footer."); } cb.addTemplate(total, document.right() - adjust, textBase); cb.restoreState(); }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildHeader(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();//from w ww . ja va 2 s.c o m String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.top() + 22; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(total, document.right() - adjust, textBase); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.top() + 12); cb.lineTo(document.right(), document.top() + 12); cb.stroke(); cb.restoreState(); }
From source file:com.rapidminer.gui.actions.export.ImageExporter.java
License:Open Source License
private void exportVectorGraphics(String formatName, File outputFile) throws ImageExportException { Component component = printableComponent.getExportComponent(); int width = component.getWidth(); int height = component.getHeight(); try (FileOutputStream fs = new FileOutputStream(outputFile)) { switch (formatName) { case PDF: // create pdf document with slightly increased width and height // (otherwise the image gets cut off) Document document = new Document(new Rectangle(width + 5, height + 5)); PdfWriter writer = PdfWriter.getInstance(document, fs); document.open();/*from ww w.j a v a 2s . c o m*/ PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(width, height); Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); component.print(g2); g2.dispose(); cb.addTemplate(tp, 0, 0); document.close(); break; case SVG: exportFreeHep(component, fs, new SVGGraphics2D(fs, new Dimension(width, height))); break; case EPS: exportFreeHep(component, fs, new PSGraphics2D(fs, new Dimension(width, height))); break; default: // cannot happen break; } } catch (Exception e) { throw new ImageExportException( I18N.getMessage(I18N.getUserErrorMessagesBundle(), "error.image_export.export_failed"), e); } }