Java Code Examples for org.apache.pdfbox.pdmodel.PDPageContentStream.drawString()

The following are Jave code examples for showing how to use drawString() of the org.apache.pdfbox.pdmodel.PDPageContentStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: kumoreg   File: ReportPrintFormatter.java   Source Code and License Vote up 9 votes
private PDPage generatePage(String[] lines, int startAt, PDDocument document) throws IOException {
    PDPage page = new PDPage(new PDRectangle(612f, 396f));
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    // Positions are measured from the bottom left corner of the page at 72 DPI

    // Add report text to page
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(36+xOffset, 360+yOffset);
    contentStream.setFont(font, fontSize);
    int lineNumber = startAt;
    while (lineNumber < startAt + linesPerPage && lineNumber < lines.length) {
        contentStream.drawString(lines[lineNumber]);
        contentStream.moveTextPositionByAmount(0, (-1*fontSize));
        lineNumber += 1;
    }
    contentStream.endText();

    contentStream.close();

    return page;

}
 
Example 2
Project: FYS_T3   File: statisticsController.java   Source Code and License Vote up 7 votes
@FXML
public void printPDFFile(ActionEvent event) throws IOException {
    try {
        String fileName = "PDFoutput.pdf";

        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);

        PDPageContentStream content = new PDPageContentStream(doc, page);

        content.beginText();
        content.setFont(PDType1Font.TIMES_ROMAN, 26);
        content.moveTextPositionByAmount(220, 750);
        content.drawString("Titel");
        content.endText();

        content.beginText();
        content.setFont(PDType1Font.TIMES_ROMAN, 16);
        content.moveTextPositionByAmount(80, 700);
        content.drawString("Inhoud");
        content.endText();

        content.close();
        doc.save(fileName);
        doc.close();

        System.out.println("your file was saved in: " + System.getProperty("user.dir"));

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}
 
Example 3
Project: kumoreg   File: FullBadgePrintFormatter.java   Source Code and License Vote up 7 votes
private void drawAgeColorStripe(PDPageContentStream contentStream, PDFont font, Attendee attendee) throws IOException {
    // Draw age color stripe
    String stripeText = "VOID";
    if (attendee.getCurrentAgeRange() != null) {
        contentStream.setNonStrokingColor(Color.decode(attendee.getCurrentAgeRange().getStripeColor()));
        stripeText = attendee.getCurrentAgeRange().getStripeText();
    } else {
        contentStream.setNonStrokingColor(Color.black);
    }
    contentStream.fillRect(155, 92, 300, 45);

    contentStream.setLineWidth(0.5f);

    // Draw age range text in color stripe
    contentStream.beginText();
    contentStream.setFont(font, 32);
    contentStream.setNonStrokingColor(Color.white);
    contentStream.setStrokingColor(Color.black);
    contentStream.moveTextPositionByAmount(438, 105);
    contentStream.appendRawCommands("2 Tr ");       // Set text rendering mode
    Float ageRangeWidth = ((font.getStringWidth(stripeText) / 1000.0f) * 32);
    contentStream.moveTextPositionByAmount(-ageRangeWidth, 0);
    contentStream.drawString(stripeText);
    contentStream.endText();
}
 
Example 4
Project: kumoreg   File: FormatterBase.java   Source Code and License Vote up 6 votes
/**
 * Draws the given string, optionally supports scaling to fit.
 * @param x Left side of text, or center point of text if centered (1/72 inch)
 * @param y Bottom of text, in points (1/72 inch)
 * @param text Text to draw
 * @param optOrig Resize Options
 * @throws IOException Error generating PDF
 */
void drawStringWithResizing(PDPageContentStream stream, float x, float y, String text, ResizeOptions optOrig) throws IOException {
    ResizeOptions opt = new ResizeOptions(optOrig);
    float textSize = opt.font.getStringWidth(text); // in thousandths of font pt size.
    float size = opt.size;

    // If text size is greater than maximum width, recalculate the correct font size, based on our restrictions
    if (textSize * (size/1000.0f) > opt.maxTextWidth) {
        size = opt.maxTextWidth * 1000.0f / textSize;
        if (size < opt.minFontSize) {
            // We have utterly failed to fit the text with the minimum font size,
            // So we're forced to use that.
            size = opt.minFontSize;
        }
    }

    if (opt.centered) {
        x -= textSize * (size/(2*1000.0f));
    }

    // Actually draw the text
    stream.beginText();
    stream.setStrokingColor(Color.black);
    stream.setNonStrokingColor(Color.black);
    stream.moveTextPositionByAmount(x, y);
    stream.setFont(opt.font, size);
    stream.drawString(text);
    stream.endText();
}
 
Example 5
Project: kumoreg   File: FullBadgePrintFormatter.java   Source Code and License Vote up 6 votes
/**
 * Draws the given string, optionally supports scaling to fit.
 * @param contentStream Open stream to draw in to
 * @param x Left side of text, or center point of text if centered (1/72 inch)
 * @param y Bottom of text, in points (1/72 inch)
 * @param text Text to draw
 * @param optOrig Resize Options
 * @throws IOException Error generating PDF
 */
private void drawStringWithResizing(PDPageContentStream contentStream, float x, float y, String text, ResizeOptions optOrig) throws IOException {
    ResizeOptions opt = new ResizeOptions(optOrig);
    float textSize = opt.font.getStringWidth(text); // in thousandths of font pt size.
    float size = opt.size;

    // If text size is greater than maximum width, recalculate the correct font size, based on our restrictions
    if (textSize * (size/1000.0f) > opt.maxTextWidth) {
        size = opt.maxTextWidth * 1000.0f / textSize;
        if (size < opt.minFontSize) {
            // We have utterly failed to fit the text with the minimum font size,
            // So we're forced to use that.
            size = opt.minFontSize;
        }
    }

    if (opt.centered) {
        x -= textSize * (size/(2*1000.0f));
    }

    // Actually draw the text
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(x, y);
    contentStream.setFont(opt.font, size);
    contentStream.drawString(text);
    contentStream.endText();
}
 
Example 6
Project: kumoreg   File: FullBadgePrintFormatter.java   Source Code and License Vote up 6 votes
private void drawBadgeType(PDPageContentStream contentStream, Attendee attendee) throws IOException {
    // Draw badge type in color stripe
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(167, 105);
    contentStream.drawString(attendee.getBadge().getBadgeTypeText());
    contentStream.endText();
}
 
Example 7
Project: kumoreg   File: LiteBadgePrintFormatter.java   Source Code and License Vote up 5 votes
private PDPage generatePage(Attendee attendee, PDDocument document) throws IOException {
    PDPage page = new PDPage(new PDRectangle(612f, 396f));
    PDFont font = PDType1Font.HELVETICA_BOLD;
    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    // Positions are measured from the bottom left corner of the page at 72 DPI

    // Draw real name
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(220+xOffset, 175+yOffset);
    contentStream.setFont( font, 24 );
    contentStream.drawString(attendee.getFirstName() + " " + attendee.getLastName());
    contentStream.endText();

    // Draw age color stripe
    String stripeText = "VOID";
    if (attendee.getCurrentAgeRange() != null) {
        contentStream.setNonStrokingColor(Color.decode(attendee.getCurrentAgeRange().getStripeColor()));
        stripeText = attendee.getCurrentAgeRange().getStripeText();
    } else {
        contentStream.setNonStrokingColor(Color.black);
    }
    contentStream.fillRect(150+xOffset, 90+yOffset, 310, 44);

    contentStream.setLineWidth(0.5f);
    contentStream.beginText();
    contentStream.setFont(font, 24);
    contentStream.setNonStrokingColor(Color.white);
    contentStream.setStrokingColor(Color.black);
    contentStream.moveTextPositionByAmount(297+xOffset, 102+yOffset);
    contentStream.appendRawCommands("2 Tr ");       // Set text rendering mode

    Float ageRangeWidth = ((font.getStringWidth(stripeText) / 1000.0f) * 18) / 2;
    contentStream.moveTextPositionByAmount(-ageRangeWidth, 0);
    contentStream.drawString(stripeText);
    contentStream.endText();

    contentStream.close();

    return page;
}