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

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

Introduction

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

Prototype

public float getWidth() 

Source Link

Document

This will get the width of this rectangle as calculated by upperRightX - lowerLeftX.

Usage

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   ww w.  j  a  v  a 2  s. co  m*/

    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:boxtable.table.Table.java

License:Apache License

/**
 * Starts a new page with the same size as the last one
 * /*from w  w  w  .  j  av a2s .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:br.com.techne.gluonsoft.pdfgenerator.PDFGenerator.java

License:Apache License

/**
 * Add a new page to the PDF document/* w ww.  ja  v a  2 s.c  o  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:br.com.techne.gluonsoft.pdfgenerator.PDFGenerator.java

License:Apache License

/**
 * Create a page with table.//from   w ww. ja v a2  s.  co  m
 * @param tableTitle Table Title
 * @param tableColumnsName   Array of Strings with table columns titles.
 * @param strTableContent Array of Strings with the table data.
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public void addNewTable(String tableTitle, ArrayList<String> tableColumnsName,
        ArrayList<String> strTableContent) throws IOException {
    PDRectangle rectLandscapeOrientation = new PDRectangle(PDRectangle.A4.getHeight(),
            PDRectangle.A4.getWidth()); // Changed width to height to get Landscape Orientation

    PDPage tablePage = new PDPage(PDRectangle.A4);
    PDRectangle rect = tablePage.getMediaBox();

    this.getPdfDocument().addPage(tablePage);

    PDPageContentStream pageContent = new PDPageContentStream(this.getPdfDocument(), tablePage);

    final float margin = 20f;
    final int tableRows = strTableContent != null ? strTableContent.size() : 0;
    final int tableColumns = tableColumnsName != null ? tableColumnsName.size() : 0;
    final float rowHeigth = 20f;
    final float tableWidth = rect.getWidth() - (2 * margin);
    final float tableHeight = rowHeigth * tableRows;
    final float tableColWidth = tableWidth / (float) tableColumns;
    final float tableCelMargin = 5f;

    // Draw the lines
    float nextY = PAGE_INITIAL_Y_POSITION;
    for (int r = 0; r <= tableRows; r++) {
        pageContent.drawLine(margin, nextY, margin + tableWidth, nextY);
        nextY -= rowHeigth;
    }

    // Draw the columns
    float nextX = margin;
    for (int i = 0; i <= tableColumns; i++) {
        pageContent.drawLine(nextX, PAGE_INITIAL_Y_POSITION, nextX, PAGE_INITIAL_Y_POSITION - tableHeight);
        nextX += tableColWidth;
    }

    pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT); // Initial Font for the columns' titles

    float textPosX = margin + tableCelMargin;
    float textPosY = PAGE_INITIAL_Y_POSITION - 15;

    // Title
    float centerX = tableWidth / 2 - (margin * 2);
    float xAlignLeft = margin;
    pageContent.beginText();
    pageContent.newLineAtOffset(xAlignLeft, PAGE_INITIAL_Y_POSITION + 5);
    pageContent.showText(tableTitle);
    pageContent.endText();

    // Columns' names
    for (int i = 0; i < tableColumnsName.size(); i++) {
        String columnName = tableColumnsName.get(i);
        System.out.println(columnName);

        pageContent.beginText();
        pageContent.newLineAtOffset(textPosX, textPosY);
        pageContent.showText(columnName);
        pageContent.endText();
        textPosX += tableColWidth;
    }

    //       textPosY -= rowHeigth;
    //       textPosX = margin + tableCelMargin;

    // Cels' content (Add the text)
    int actualCol = 0;
    pageContent.setFont(FONT_PLAIN, FONT_SIZE_DEFAULT);
    for (int i = 0; i < strTableContent.size(); i++) {
        if (actualCol % tableColumns == 0) {
            actualCol = 0;
            textPosY -= rowHeigth;
            textPosX = margin + tableCelMargin;
        }

        String celText = strTableContent.get(i);
        System.out.println(celText);
        pageContent.beginText();
        pageContent.newLineAtOffset(textPosX, textPosY);
        pageContent.showText(celText);
        pageContent.endText();
        textPosX += tableColWidth;

        actualCol++;
    }

    pageContent.close();
}

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

License:Apache License

/**
 * Create a page with table./* ww  w  .j  a  va 2 s  . c o  m*/
 * @param tableTitle Table Title
 * @param strTableContent   Array of Strings, where the column's titles goes into the first array line and the data goes into others lines.
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public void addNewTable(String tableTitle, String[][] strTableContent) throws IOException {
    PDPage tablePage = new PDPage(PDRectangle.A4);

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

    this.getPdfDocument().addPage(tablePage);

    PDPageContentStream pageContent = new PDPageContentStream(this.getPdfDocument(), tablePage);

    final float margin = 20f;
    final int tableRows = strTableContent.length;
    final int tableColumns = strTableContent[0].length;
    final float rowHeigth = 20f;
    final float tableWidth = rect.getWidth() - (2 * margin);
    final float tableHeight = rowHeigth * tableRows;
    final float tableColWidth = tableWidth / (float) tableColumns;
    final float tableCelMargin = 5f;

    // Draw the lines
    float nextY = PAGE_INITIAL_Y_POSITION;
    for (int r = 0; r <= tableRows; r++) {
        pageContent.drawLine(margin, nextY, margin + tableWidth, nextY);
        nextY -= rowHeigth;
    }

    // Draw the columns
    float nextX = margin;
    for (int i = 0; i <= tableColumns; i++) {
        pageContent.drawLine(nextX, PAGE_INITIAL_Y_POSITION, nextX, PAGE_INITIAL_Y_POSITION - tableHeight);
        nextX += tableColWidth;
    }

    pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT); // Fonte inicial para o ttulo das colunas

    float textPosX = margin + tableCelMargin;
    float textPosY = PAGE_INITIAL_Y_POSITION - 15;

    // Title
    float centerX = tableWidth / 2 - (margin * 2);
    pageContent.beginText();
    pageContent.newLineAtOffset(centerX, PAGE_INITIAL_Y_POSITION + 5);
    pageContent.showText(tableTitle);
    pageContent.endText();

    // Cels' content (Add the text)
    for (int l = 0; l < strTableContent.length; l++) {
        for (int c = 0; c < strTableContent[l].length; c++) {
            String celText = strTableContent[l][c];

            if (l > 0) {
                pageContent.setFont(FONT_PLAIN, FONT_SIZE_DEFAULT);
            }

            pageContent.beginText();
            pageContent.newLineAtOffset(textPosX, textPosY);
            pageContent.showText(celText);
            pageContent.endText();
            textPosX += tableColWidth;
        }

        textPosY -= rowHeigth;
        textPosX = margin + tableCelMargin;
    }

    pageContent.close();
}

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

public PdfGenerator(PDRectangle format) {
    pageFormat = format;
    documentPadding = format.getWidth() / 12F;
}

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   w w  w. ja  v  a  2s  .c  o  m*/

    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;//ww w.  jav a 2  s.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  w ww  .  j  a  v a 2 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);

}

From source file:com.devnexus.ting.web.controller.PdfUtils.java

License:Apache License

public PdfUtils(float margin, String title) throws IOException {
    this.margin = margin;
    doc = new PDDocument();
    baseFont = PDType0Font.load(doc, PdfUtils.class.getResourceAsStream("/fonts/Arial.ttf"));
    headerFont = PDType1Font.HELVETICA_BOLD;
    subHeaderFont = PDType1Font.HELVETICA_BOLD;
    devnexusLogo = PDDocument.load(PdfUtils.class.getResourceAsStream("/fonts/devnexus-logo.pdf"));

    this.currentPage = new PDPage();
    this.pages.add(currentPage);
    this.doc.addPage(currentPage);

    final PDRectangle mediabox = currentPage.getMediaBox();
    this.width = mediabox.getWidth() - 2 * margin;

    float startX = mediabox.getLowerLeftX() + margin;
    float startY = mediabox.getUpperRightY() - margin;

    this.initialHeightCounter = startY;
    this.heightCounter = startY;

    LOGGER.info(String.format(/*  w ww.ja v a 2  s.com*/
            "Margin: %s, width: %s, startX: %s, "
                    + "startY: %s, heightCounter: %s, baseFontSize: %s, headerFontSize: %s",
            margin, width, startX, startY, heightCounter, baseFont, headerFont));

    contents = new PDPageContentStream(doc, currentPage);

    // Add Logo

    final LayerUtility layerUtility = new LayerUtility(doc);
    final PDFormXObject logo = layerUtility.importPageAsForm(devnexusLogo, 0);
    final AffineTransform affineTransform = AffineTransform.getTranslateInstance(100, startY - 50);
    affineTransform.scale(2d, 2d);
    layerUtility.appendFormAsLayer(currentPage, logo, affineTransform, "devnexus-logo");
    this.heightCounter -= 100;

    this.contents.beginText();

    this.contents.setFont(headerFont, headerFontSize);
    this.currentLeading = this.lineSpacing * baseFontSize;
    this.contents.setLeading(this.currentLeading);

    contents.newLineAtOffset(50, heightCounter);

    println(title);

    this.contents.setFont(baseFont, baseFontSize);
    this.currentLeading = this.lineSpacing * baseFontSize;
    this.contents.setLeading(this.currentLeading);

    println();

}