Example usage for org.apache.pdfbox.pdmodel.common PDRectangle getHeight

List of usage examples for org.apache.pdfbox.pdmodel.common PDRectangle getHeight

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.common PDRectangle getHeight.

Prototype

public float getHeight() 

Source Link

Document

This will get the height of this rectangle as calculated by upperRightY - lowerLeftY.

Usage

From source file:at.knowcenter.wag.egov.egiz.pdfbox2.pdf.PDFPage.java

License:EUPL

/**
 * Registers a rectangle that bounds the path currently being drawn.
 * /*from w  w  w.  jav  a2  s  . co  m*/
 * @param bounds
 *            A rectangle depicting the bounds (coordinates originating from
 *            bottom left).
 * @author Datentechnik Innovation GmbH
 */
public void registerPathBounds(Rectangle bounds) {
    if (!bounds.isEmpty()) {
        logger.debug("Registering path bounds: " + bounds);

        // vertical start of rectangle (counting from top of page)
        float upperBoundYPositionFromTop;

        // vertical end of rectangle (counting from top of page)
        // this depicts the current end of path-related page content
        float lowerBoundYPositionFromTop;

        PDRectangle boundaryBox = this.getCurrentPage().getCropBox();

        if (boundaryBox == null) {
            boundaryBox = this.getCurrentPage().getMediaBox();
        }

        float pageHeight;

        switch (this.getCurrentPage().getRotation()) {
        case 90: // CW
            pageHeight = boundaryBox.getWidth();
            upperBoundYPositionFromTop = (float) bounds.getMinX();
            lowerBoundYPositionFromTop = (float) bounds.getMaxX();
            break;
        case 180:
            pageHeight = boundaryBox.getHeight();
            upperBoundYPositionFromTop = (float) bounds.getMinY();
            lowerBoundYPositionFromTop = (float) bounds.getMaxY();
            break;
        case 270: // CCW
            pageHeight = boundaryBox.getWidth();
            upperBoundYPositionFromTop = pageHeight - (float) bounds.getMaxX();
            lowerBoundYPositionFromTop = pageHeight - (float) bounds.getMinX();
            break;
        default:
            pageHeight = boundaryBox.getHeight();
            upperBoundYPositionFromTop = pageHeight - (float) bounds.getMaxY();
            lowerBoundYPositionFromTop = pageHeight - (float) bounds.getMinY();
            break;
        }

        // new maximum ?
        if (lowerBoundYPositionFromTop > maxPathRelatedYPositionFromTop) {
            // Is the rectangle (at least partly) located above the footer
            // line?
            // (effective page height := page height - footer line)
            if (upperBoundYPositionFromTop <= effectivePageHeight) {
                // yes: update current end of path-related page content
                maxPathRelatedYPositionFromTop = lowerBoundYPositionFromTop;
                logger.trace("New max path related y position (from top): " + maxPathRelatedYPositionFromTop);
            } else {
                // no: rectangle is fully located below the footer line ->
                // ignore
                logger.trace("Ignoring path bound below the footer line.");
            }
        }
    }
}

From source file:at.medevit.elexis.impfplan.ui.handlers.PrintVaccinationEntriesHandler.java

License:Open Source License

private void createPDF(Patient patient, Image image) throws IOException, COSVisitorException {
    PDDocumentInformation pdi = new PDDocumentInformation();
    Mandant mandant = (Mandant) ElexisEventDispatcher.getSelected(Mandant.class);
    pdi.setAuthor(mandant.getName() + " " + mandant.getVorname());
    pdi.setCreationDate(new GregorianCalendar());
    pdi.setTitle("Impfausweis " + patient.getLabel());

    PDDocument document = new PDDocument();
    document.setDocumentInformation(pdi);

    PDPage page = new PDPage();
    page.setMediaBox(PDPage.PAGE_SIZE_A4);
    document.addPage(page);//from w ww . j a va2s. c  om

    PDRectangle pageSize = page.findMediaBox();
    PDFont font = PDType1Font.HELVETICA_BOLD;

    PDFont subFont = PDType1Font.HELVETICA;

    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.beginText();
    contentStream.setFont(font, 14);
    contentStream.moveTextPositionByAmount(40, pageSize.getUpperRightY() - 40);
    contentStream.drawString(patient.getLabel());
    contentStream.endText();

    String dateLabel = sdf.format(Calendar.getInstance().getTime());
    String title = Person.load(mandant.getId()).get(Person.TITLE);
    String mandantLabel = title + " " + mandant.getName() + " " + mandant.getVorname();
    contentStream.beginText();
    contentStream.setFont(subFont, 10);
    contentStream.moveTextPositionByAmount(40, pageSize.getUpperRightY() - 55);
    contentStream.drawString("Ausstellung " + dateLabel + ", " + mandantLabel);
    contentStream.endText();

    BufferedImage imageAwt = convertToAWT(image.getImageData());

    PDXObjectImage pdPixelMap = new PDPixelMap(document, imageAwt);
    contentStream.drawXObject(pdPixelMap, 40, 30, pageSize.getWidth() - 80, pageSize.getHeight() - 100);
    contentStream.close();

    String outputPath = CoreHub.userCfg.get(PreferencePage.VAC_PDF_OUTPUTDIR,
            CoreHub.getWritableUserDir().getAbsolutePath());
    if (outputPath.equals(CoreHub.getWritableUserDir().getAbsolutePath())) {
        SWTHelper.showInfo("Kein Ausgabeverzeichnis definiert", "Ausgabe erfolgt in: " + outputPath
                + "\nDas Ausgabeverzeichnis kann unter Einstellungen\\Klinische Hilfsmittel\\Impfplan definiert werden.");
    }
    File outputDir = new File(outputPath);
    File pdf = new File(outputDir, "impfplan_" + patient.getPatCode() + ".pdf");
    document.save(pdf);
    document.close();
    Desktop.getDesktop().open(pdf);
}

From source file:at.oculus.teamf.technical.printing.Printer.java

License:Open Source License

/**
 * <h3>$print</h3>/*  w  w w. j av  a 2  s. co m*/
 * <p/>
 * <b>Description:</b>
 * <p/>
 * This method prints the given parameters into a PDF-Document and opens the file with the standard program.
 * <p/>
 * <b>Parameter</b>
 *
 * @param title Is a string which will be printed as title in the PDF Document.
 * @param text Is a string which will be printed as message in the PDF Document.
 */
public void print(String title, String text) throws IOException, COSVisitorException {

    //creates a new PDDocument object
    PDDocument document = new PDDocument();
    //create a page to the document
    PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
    //rectangle is for sizes (height and width) of page
    PDRectangle rectangle = page1.getMediaBox();
    //add page to document
    document.addPage(page1);

    //fonts for the document are implemented here
    PDFont fontPlain = PDType1Font.HELVETICA;
    PDFont fontBold = PDType1Font.HELVETICA_BOLD;

    //running variable to calculate which line you are in the document right now
    int line = 0;

    try {
        //create the content stream which will write into the document
        PDPageContentStream stream = new PDPageContentStream(document, page1);

        //always use all these lines for entering new text into the document
        //move textToPosition will set the cursor to the current position
        stream.beginText();
        stream.setFont(fontBold, 14);
        stream.moveTextPositionByAmount(SPACING_LEFT, rectangle.getHeight() - SPACING_TOP);
        stream.drawString(title + ":");
        stream.endText();

        //calculates the correct place and then writes the whole text into the PDF-Document
        int start = 0;
        int end = text.length();
        while (start < end) {
            String tobePrinted;
            if ((end - start) > MAX_CHARACTERS_PER_LINE) {
                int tempEnd = start + MAX_CHARACTERS_PER_LINE;
                while (text.charAt(tempEnd) != ' ') {
                    ++tempEnd;
                }
                tobePrinted = text.substring(start, start = ++tempEnd);

            } else {
                tobePrinted = text.substring(start);
                start = start + MAX_CHARACTERS_PER_LINE;
            }
            stream.beginText();
            stream.setFont(fontPlain, 12);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString(tobePrinted);
            stream.endText();

        }
        //print oculus image into pdf file
        BufferedImage awtImage = ImageIO
                .read(new File("/home/oculus/IdeaProjects/Oculus/Technical/src/res/oculus.JPG"));
        PDXObjectImage ximage = new PDPixelMap(document, awtImage);
        float scale = 0.3f; // alter this value to set the image size
        stream.drawXObject(ximage, 380, 780, ximage.getWidth() * scale, ximage.getHeight() * scale);

        //close stream afterwards
        stream.close();

        //save document and close document
        document.save(
                "/home/oculus/IdeaProjects/Oculus/Technical/src/at/oculus/teamf/technical/printing/output_files/"
                        + title + ".pdf");
        document.close();
        //open document with standard application for the file
        Desktop.getDesktop().open(new File(
                "/home/oculus/IdeaProjects/Oculus/Technical/src/at/oculus/teamf/technical/printing/output_files/"
                        + title + ".pdf"));
    } catch (IOException | COSVisitorException e) {
        throw e;
    }
}

From source file:at.oculus.teamf.technical.printing.Printer.java

License:Open Source License

/**
 * <h3>$printPrescription</h3>
 * <p/>/*from  w  w  w.  j av a  2  s. c o m*/
 * <b>Description:</b>
 * <p/>
 * This method prints the given parameters into a PDF-Document and opens the file with the standard program.
 * <p/>
 * <b>Parameter</b>
 *
 * @param iPrescription Is an Interface of a prescription from which all the information will be printed into
 *                      a PDF-File and then started with standard application from OS.
 */
public void printPrescription(IPrescription iPrescription, IDoctor iDoctor) throws COSVisitorException,
        IOException, CantGetPresciptionEntriesException, NoPrescriptionToPrintException {
    if (iPrescription == null) {
        throw new NoPrescriptionToPrintException();
    }

    //instantiate a new document, create a page and add the page to the document
    PDDocument document = new PDDocument();
    PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
    PDRectangle rectangle = page1.getMediaBox();
    document.addPage(page1);

    //create fonts for the document
    PDFont fontPlain = PDType1Font.HELVETICA;
    PDFont fontBold = PDType1Font.HELVETICA_BOLD;

    //running variable to calculate current line
    int line = 0;

    try {
        //new Stream to print into the file
        PDPageContentStream stream = new PDPageContentStream(document, page1);

        //print header (headlining)
        stream.beginText();
        stream.setFont(fontBold, 14);
        stream.moveTextPositionByAmount(SPACING_LEFT, rectangle.getHeight() - SPACING_TOP);
        stream.drawString("Prescription:");
        stream.endText();

        //get patient to print data from
        IPatient iPatient = iPrescription.getPatient();

        //write data from patient
        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString(iPatient.getFirstName() + " " + iPatient.getLastName());
        stream.endText();

        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        /* -- Team D: Add check on null -- */
        stream.drawString(iPatient.getBirthDay() == null ? "" : iPatient.getBirthDay().toString());
        /* -- -- -- */
        stream.endText();

        if (iPatient.getStreet() != null) {
            stream.beginText();
            stream.setFont(fontPlain, 12);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString(iPatient.getStreet());
            stream.endText();
        }

        if ((iPatient.getPostalCode() != null) && (iPatient.getCity() != null)) {
            stream.beginText();
            stream.setFont(fontPlain, 12);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString(iPatient.getPostalCode() + ", " + iPatient.getCity());
            stream.endText();
        }

        //next row
        ++line;

        //write data from doctor
        stream.beginText();
        stream.setFont(fontBold, 14);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString("Prescription issued from doctor:");
        stream.endText();

        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString(iDoctor.getTitle() + " " + iDoctor.getFirstName() + " " + iDoctor.getLastName());
        stream.endText();

        //next row
        ++line;

        //print all the entries in the prescription
        if (iPrescription.getPrescriptionEntries().size() > 0) {
            stream.beginText();
            stream.setFont(fontBold, 14);
            stream.moveTextPositionByAmount(SPACING_LEFT,
                    rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
            stream.drawString("Prescription Entries:");
            stream.endText();
            for (IPrescriptionEntry entry : iPrescription.getPrescriptionEntries()) {
                stream.beginText();
                stream.setFont(fontPlain, 12);
                stream.moveTextPositionByAmount(SPACING_LEFT,
                        rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
                stream.drawString("ID: " + entry.getId() + ", " + entry.getMedicine());
                stream.endText();
            }
        }

        //print oculus image into pdf file
        //Not working
        /*BufferedImage awtImage = ImageIO.read(new File("oculus.JPG"));
        PDXObjectImage ximage = new PDPixelMap(document, awtImage);
        float scale = 0.3f; // alter this value to set the image size
        stream.drawXObject(ximage, 380, 780, ximage.getWidth() * scale, ximage.getHeight() * scale);*/

        //signature field

        ++line;
        stream.beginText();
        stream.setFont(fontPlain, 12);
        stream.moveTextPositionByAmount(SPACING_LEFT,
                rectangle.getHeight() - LINE_HEIGHT * (++line) - SPACING_HEADER);
        stream.drawString("Doctor's Signature:");
        stream.endText();

        //print a line for signature field
        stream.setLineWidth(0.5f);
        stream.addLine(SPACING_LEFT, rectangle.getHeight() - LINE_HEIGHT * (line += 2) - SPACING_HEADER,
                SPACING_LEFT + 100, rectangle.getHeight() - LINE_HEIGHT * (line) - SPACING_HEADER);
        stream.closeAndStroke();

        //close the stream
        stream.close();

        //save the document and close it
        document.save("prescription.pdf"); //Todo: position from property file
        document.close();
        //open file with standard OS application
        Desktop.getDesktop().open(new File("prescription.pdf"));

        //Print file directly from standard printer (NOT SUPPORTED ON OCULUS-LINUX -- should be tested first!!!)
        //Desktop.getDesktop().print(new File("/home/oculus/IdeaProjects/Oculus/Technical/src/at/oculus/teamf/technical/printing/output_files/prescription.pdf"));
    } catch (COSVisitorException | CantGetPresciptionEntriesException | IOException e) {
        throw e;
    }
}

From source file:boxtable.table.Table.java

License:Apache License

/**
 * Starts a new page with the same size as the last one
 * /* www .j a  v a  2  s  . c  om*/
 * @param document
 *            The document the table is rendered to
 * @param stream
 *            The PDPageContentStream used to render the table up to now (will be closed after calling this method)
 * @return A new PDPageContentStream for rendering to the new page
 * @throws IOException
 *             If writing to the streams fails
 */
private PDPageContentStream newPage(final PDDocument document, final PDPageContentStream stream)
        throws IOException {
    final PDRectangle pageSize = document.getPage(document.getNumberOfPages() - 1).getMediaBox();
    handleEvent(EventType.END_PAGE, document, stream, 0, pageSize.getHeight(), pageSize.getWidth(),
            pageSize.getHeight());
    stream.close();
    final PDPage page = new PDPage(pageSize);
    document.addPage(page);
    PDPageContentStream newStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
    handleEvent(EventType.BEGIN_PAGE, document, newStream, 0, pageSize.getHeight(), pageSize.getWidth(),
            pageSize.getHeight());
    return newStream;
}

From source file:boxtable.table.Table.java

License:Apache License

/**
 * Renders this table to a document// w  w  w .j  a  va 2 s . co  m
 * 
 * @param document
 *            The document this table will be rendered to
 * @param width
 *            The width of the table
 * @param left
 *            The left edge of the table
 * @param top
 *            The top edge of the table
 * @param paddingTop
 *            The amount of free space at the top of a new page (if a page break is necessary)
 * @param paddingBottom
 *            The minimal amount of free space at the bottom of the page before inserting a page break
 * @return The bottom edge of the last rendered table part
 * @throws IOException
 *             If writing to the document fails
 */
@SuppressWarnings("resource")
public float render(final PDDocument document, final float width, final float left, float top,
        final float paddingTop, final float paddingBottom) throws IOException {
    float yPos = top;
    final PDPage page = document.getPage(document.getNumberOfPages() - 1);
    final PDRectangle pageSize = page.getMediaBox();
    PDPageContentStream stream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
    float height = getHeight(width);
    if (height > pageSize.getHeight() - paddingTop - paddingBottom) {
        final float[] colWidths = getColumnWidths(width);
        for (int i = 0; i < rows.size(); ++i) {
            if (rows.get(i).getHeight(colWidths) > yPos - paddingBottom) {
                drawBorder(stream, left, top, width, top - yPos);
                stream = newPage(document, stream);
                top = pageSize.getHeight() - paddingTop;
                yPos = top;
                yPos = renderRows(document, stream, 0, getNumHeaderRows(), width, left, yPos);
                i = Math.max(i, getNumHeaderRows());
            }
            yPos = renderRows(document, stream, i, i + 1, width, left, yPos);
        }
        drawBorder(stream, left, top, width, top - yPos);

        handleEvent(EventType.AFTER_TABLE, document, stream, left, top, width, top - yPos);
    } else {
        if (height > top - paddingBottom) {
            stream = newPage(document, stream);
            top = pageSize.getHeight() - paddingTop;
            yPos = top;
        }
        yPos = renderRows(document, stream, 0, -1, width, left, yPos);
        drawBorder(stream, left, top, width, top - yPos);
        handleEvent(EventType.AFTER_TABLE, document, stream, left, top, width, top - yPos);
    }
    stream.close();

    return yPos;
}

From source file:br.com.techne.gluonsoft.pdfgenerator.PDFGenerator.java

License:Apache License

/**
 * Add a new page to the PDF document//from  ww  w . j ava 2  s.  co m
 * @param title
 * @param strLineContent Text to be added to the page
 * @throws IOException 
 */
public void addNewPage(String pageTitle, String[] strLineContent) throws IOException {
    PDPage page = new PDPage(PDRectangle.A4);
    this.addNewPage(page);

    PDRectangle rect = new PDRectangle();
    rect = page.getMediaBox();

    final float PAGE_MAX_WIDTH = rect.getWidth() - (2 * PAGE_MARGIN);
    final float PAGE_MAX_HEIGHT = rect.getHeight();

    final float PAGE_X_ALIGN_CENTER = PAGE_MAX_WIDTH / 2; // (PAGE_MAX_WIDTH + (2 * PAGE_MARGIN)) / 2 ;

    PDPageContentStream pageContent = new PDPageContentStream(this.getPdfDocument(), page); // Page's Stream

    int line = 1;

    // Add the page's title
    if (pageTitle != null) {
        pageContent.beginText();
        pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT);
        pageContent.newLineAtOffset(PAGE_X_ALIGN_CENTER, PAGE_INITIAL_Y_POSITION); // CENTER
        pageContent.showText(pageTitle); // Title
        pageContent.endText();

        pageContent.beginText();
        pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT);
        pageContent.newLineAtOffset(PAGE_MARGIN, PAGE_INITIAL_Y_POSITION - 10 * (line++)); // pageContent.newLineAtOffset(PAGE_MARGIN, PAGE_INITIAL_Y_POSITION);
        pageContent.showText(""); // Line after title
        pageContent.endText();
    }

    // Add the page's content
    if (strLineContent != null && strLineContent.length > 0) {
        for (String strLine : strLineContent) {
            ArrayList<String> newLines = autoBreakLineIntoOthers(strLine, _MAX_LINE_CHARACTERS); // Break a text line into others lines to fit into page width.

            for (String str : newLines) {
                pageContent.beginText();
                pageContent.setFont(FONT_PLAIN, FONT_SIZE_DEFAULT);
                pageContent.newLineAtOffset(PAGE_MARGIN, PAGE_INITIAL_Y_POSITION - 10 * (line++));
                pageContent.showText(str);
                pageContent.endText();
            }
        }
    }

    pageContent.close();
}

From source file:ch.dowa.jassturnier.pdf.PdfGenerator.java

public void exportTemplateWithTable(TableBuilder tableBuilder, String fileName, String title)
        throws IOException {
    String vLogoPath = !ResourceLoader.readProperty("LOGOPATH").isEmpty()
            ? ResourceLoader.readProperty("LOGOPATH")
            : null;/*from www.j  ava2 s .  c om*/

    final PDDocument document = new PDDocument();
    boolean firstPage = true;
    PDImageXObject image = null;
    float imageStartX = 0F;
    float imageStartY = 0F;
    PDRectangle imgSize = null;

    TableDrawer drawer = TableDrawer.builder().table(tableBuilder.build()).startX(docContentStartX())
            .startY(docContentStartY()).endY(documentPadding).build();

    if (vLogoPath != null) {
        image = PDImageXObject.createFromFile(vLogoPath, document);
        imgSize = getImageSizePdf(image);
        imageStartX = imgEndX() - imgSize.getWidth();
        imageStartY = imgEndY() - imgSize.getHeight();
    }

    do {
        PDPage page = new PDPage(pageFormat);
        document.addPage(page);
        try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
            if (firstPage) {
                contentStream.beginText();
                contentStream.setFont(STANDART_FONT_BOLD, 14);
                contentStream.newLineAtOffset(docTitelStartX(), docTitelStartY());
                contentStream.showText(title);
                contentStream.endText();
                if (image != null) {
                    contentStream.drawImage(image, imageStartX, imageStartY, imgSize.getWidth(),
                            imgSize.getHeight());
                }
                drawer.startY(docContentSartFirsPageY());
                firstPage = false;
            }
            drawer.contentStream(contentStream).draw();
        }
        drawer.startY(docContentStartY());
    } while (!drawer.isFinished());

    document.save(fileName);
    document.close();
}

From source file:ch.dowa.jassturnier.pdf.PdfGenerator.java

public void exportTemplateWithTableMultiPage(HashMap<String, Table.TableBuilder> tableBuildersMap,
        String fileName) throws IOException {
    String vLogoPath = !ResourceLoader.readProperty("LOGOPATH").isEmpty()
            ? ResourceLoader.readProperty("LOGOPATH")
            : null;/*  w w w  . java  2s .c o m*/
    PDDocument mainDoc = new PDDocument();
    PDFMergerUtility merger = new PDFMergerUtility();
    for (Map.Entry<String, TableBuilder> entry : tableBuildersMap.entrySet()) {
        String title = entry.getKey();
        TableBuilder tableBuilder = entry.getValue();

        final PDDocument documentPage = new PDDocument();
        boolean firstPage = true;
        PDImageXObject image = null;
        float imageStartX = 0F;
        float imageStartY = 0F;
        PDRectangle imgSize = null;

        TableDrawer drawer = TableDrawer.builder().table(tableBuilder.build()).startX(docContentStartX())
                .startY(docContentStartY()).endY(documentPadding).build();

        if (vLogoPath != null) {
            image = PDImageXObject.createFromFile(vLogoPath, documentPage);
            imgSize = getImageSizePdf(image);
            imageStartX = imgEndX() - imgSize.getWidth();
            imageStartY = imgEndY() - imgSize.getHeight();
        }

        do {
            PDPage page = new PDPage(pageFormat);
            documentPage.addPage(page);
            try (PDPageContentStream contentStream = new PDPageContentStream(documentPage, page)) {
                if (firstPage) {
                    contentStream.beginText();
                    contentStream.setFont(STANDART_FONT_BOLD, 14);
                    contentStream.newLineAtOffset(docTitelStartX(), docTitelStartY());
                    contentStream.showText(title);
                    contentStream.endText();
                    if (image != null) {
                        contentStream.drawImage(image, imageStartX, imageStartY, imgSize.getWidth(),
                                imgSize.getHeight());
                    }
                    drawer.startY(docContentSartFirsPageY());
                    firstPage = false;
                }
                drawer.contentStream(contentStream).draw();
            }
            drawer.startY(docContentStartY());
        } while (!drawer.isFinished());

        merger.appendDocument(mainDoc, documentPage);
    }

    mainDoc.save(fileName);
    mainDoc.close();
}

From source file:chiliad.parser.pdf.ChiliadPDFParser.java

License:Apache License

private void processPage(int currentPageNumber, PDPage page) {
    startPage(page);//from ww  w.j a  v  a2  s .c o m
    PDRectangle mediaBox = page.findMediaBox();
    MPage pageContent = MPage.newInstance(source.getId(), currentPageNumber, (double) mediaBox.getWidth(),
            (double) mediaBox.getHeight());

    for (PageExtractor extractor : extractors) {
        pageContent = extractor.extract(page, pageContent);
    }
    writePageContent(pageContent);
    for (PageExtractor extractor : extractors) {
        extractor.reset();
    }
    endPage(page);

}