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

The following are Jave code examples for showing how to use setLineWidth() 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: 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 2
Project: kumoreg   File: AttendeeBadge2017.java   Source Code and License Vote up 6 votes
private void drawBadgeNumber(PDPage page, PDFont font, Attendee attendee) throws IOException {
        String badgeNumber = attendee.getBadgeNumber();
        if (badgeNumber == null) {
            return;     // no text, don't draw anything
        }

        List<String> badgeNumberParts = BadgeLib.splitBadgeNumber(badgeNumber);

        PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);

        String stripeColor = attendee.getCurrentAgeRange().getStripeColor();
        stream.setNonStrokingColor(BadgeLib.getForegroundColor(stripeColor));
        // Bounding box:
//         stream.fillRect(163, 95, 40, 30);

        PDRectangle boundingBox = new PDRectangle(163, 95, 40, 30);
        stream.setLineWidth(0.25f);
        stream.beginText();
        int fontSize = BadgeLib.findMaxFontSize(font, badgeNumberParts,boundingBox);
        stream.setFont(font, fontSize);

        float textWidth = font.getStringWidth(badgeNumberParts.get(0));
        Float offset = textWidth * (fontSize/(2*1000.0f));
        stream.newLineAtOffset(185-offset, 105+fontSize);   // First character position
        stream.showText(badgeNumberParts.get(0));

        if (badgeNumberParts.size() > 1) {
            textWidth = font.getStringWidth(badgeNumberParts.get(1));
            Float newOffset = textWidth * (fontSize/(2*1000.0f));
            stream.newLineAtOffset(offset-newOffset, -1*fontSize);   // First character position
            stream.showText(badgeNumberParts.get(1));
        }
        stream.close();
    }
 
Example 3
Project: kumoreg   File: AttendeeBadge2017.java   Source Code and License Vote up 6 votes
private void drawBadgeTypeText(PDPage page, PDFont font, Attendee attendee) throws IOException {
        if (attendee.getBadge() == null || attendee.getBadge().getBadgeTypeText() == null) {
            return;     // no text, don't draw anything
        }
        String badgeTypeText = attendee.getBadge().getBadgeTypeText();
        PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);

//        PDRectangle boundingBox = new PDRectangle(206, 85, 253, 44);

        stream.setLineWidth(0.5f);
        stream.beginText();
        int fontSize = BadgeLib.findMaxLineSize(font, badgeTypeText,240, 40);
        stream.setFont(font, fontSize);
        if (attendee.getBadge() != null &&
                (attendee.getBadge().getBadgeTypeText().equals("Friday") ||
                        attendee.getBadge().getBadgeTypeText().equals("Saturday") ||
                        attendee.getBadge().getBadgeTypeText().equals("Sunday"))) {
            if (attendee.getBadge() != null && attendee.getBadge().getBadgeTypeBackgroundColor() != null) {
                stream.setNonStrokingColor(BadgeLib.getForegroundColor(attendee.getCurrentAgeRange().getStripeColor()));
            } else {
                stream.setNonStrokingColor(Color.WHITE);
                stream.setStrokingColor(Color.black);
            }

        } else {
            if (attendee.getBadge() != null && attendee.getBadge().getBadgeTypeBackgroundColor() != null) {
                stream.setNonStrokingColor(BadgeLib.getForegroundColor(attendee.getBadge().getBadgeTypeBackgroundColor()));
            } else {
                stream.setNonStrokingColor(Color.WHITE);
                stream.setStrokingColor(Color.black);
            }

        }

        float textWidth = font.getStringWidth(badgeTypeText);
        Float offset = textWidth * (fontSize/(2*1000.0f));
        stream.newLineAtOffset(330-offset, 100);   // First character position
        stream.showText(badgeTypeText);
        stream.close();
    }
 
Example 4
Project: cat-boot   File: PdfBoxHelper.java   Source Code and License Vote up 6 votes
public static void addTextSimpleUnderlined(PDPageContentStream stream, PdfTextStyle textConfig, float textX, float textY, String text) {
    addTextSimple(stream, textConfig, textX, textY, text);
    try {
        float lineOffset = textConfig.getFontSize() / 8F;
        stream.setStrokingColor(textConfig.getColor());
        stream.setLineWidth(0.5F);
        stream.moveTo(textX, textY - lineOffset);
        stream.lineTo(textX + getTextWidth(textConfig.getCurrentFontStyle(), textConfig.getFontSize(), text), textY - lineOffset);
        stream.stroke();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 5
Project: testarea-pdfbox2   File: TestEmptySignatureField.java   Source Code and License Vote up 6 votes
/**
 * <a href="http://stackoverflow.com/questions/37601092/pdfbox-identify-specific-pages-and-functionalities-recommendations">
 * PDFBox identify specific pages and functionalities recommendations
 * </a>
 * 
 * <p>
 * This test shows how to add an empty signature field with a custom appearance
 * to an existing PDF.
 * </p>
 */
@Test
public void testAddEmptySignatureField() throws IOException
{
    try (   InputStream sourceStream = getClass().getResourceAsStream("test.pdf");
            OutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "test-with-empty-sig-field.pdf")))
    {
        PDFont font = PDType1Font.HELVETICA;
        PDResources resources = new PDResources();
        resources.put(COSName.getPDFName("Helv"), font);

        PDDocument document = PDDocument.load(sourceStream);
        PDAcroForm acroForm = new PDAcroForm(document);
        acroForm.setDefaultResources(resources);
        document.getDocumentCatalog().setAcroForm(acroForm);

        PDRectangle rect = new PDRectangle(50, 750, 200, 50);

        PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
        PDAppearanceStream appearanceStream = new PDAppearanceStream(document);
        appearanceStream.setBBox(rect.createRetranslatedRectangle());
        appearanceStream.setResources(resources);
        appearanceDictionary.setNormalAppearance(appearanceStream);
        PDPageContentStream contentStream = new PDPageContentStream(document, appearanceStream);
        contentStream.setStrokingColor(Color.BLACK);
        contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
        contentStream.setLineWidth(2);
        contentStream.addRect(0, 0, rect.getWidth(), rect.getHeight());
        contentStream.fill();
        contentStream.moveTo(1 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.lineTo(2 * rect.getHeight() / 4, 3 * rect.getHeight() / 4);
        contentStream.moveTo(1 * rect.getHeight() / 4, 3 * rect.getHeight() / 4);
        contentStream.lineTo(2 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.moveTo(3 * rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.lineTo(rect.getWidth() - rect.getHeight() / 4, 1 * rect.getHeight() / 4);
        contentStream.stroke();
        contentStream.setNonStrokingColor(Color.DARK_GRAY);
        contentStream.beginText();
        contentStream.setFont(font, rect.getHeight() / 5);
        contentStream.newLineAtOffset(3 * rect.getHeight() / 4, -font.getBoundingBox().getLowerLeftY() * rect.getHeight() / 5000);
        contentStream.showText("Customer");
        contentStream.endText();
        contentStream.close();

        PDSignatureField signatureField = new PDSignatureField(acroForm);
        signatureField.setPartialName("SignatureField");
        PDPage page = document.getPage(0);

        PDAnnotationWidget widget = signatureField.getWidgets().get(0);
        widget.setAppearance(appearanceDictionary);
        widget.setRectangle(rect);
        widget.setPage(page);

        page.getAnnotations().add(widget);
        acroForm.getFields().add(signatureField);

        document.save(output);
        document.close();
    }
}
 
Example 6
Project: BoxTable   File: Bordered.java   Source Code and License Vote up 5 votes
/**
 * Renders the borders
 * 
 * @param stream
 *            The stream used to render the borders
 * @param left
 *            The coordinate of the left edge of the object
 * @param top
 *            The coordinate of the top edge of the object
 * @param width
 *            The width the object will be rendered
 * @param height
 *            The height the object will be rendered
 * @throws IOException
 *             If writing to the stream fails
 */
protected void drawBorder(final PDPageContentStream stream, final float left, final float top, final float width, final float height) throws IOException {
	stream.moveTo(left, top);
	float lineWidth = -1;
	if (topBorder > 0) {
		stream.setLineWidth(topBorder);
		lineWidth = topBorder;
		stream.lineTo(left + width, top);
	} else {
		stream.moveTo(left + width, top);
	}
	if (rightBorder > 0) {
		if (lineWidth != rightBorder) {
			stream.setLineWidth(rightBorder);
		}
		lineWidth = rightBorder;
		stream.lineTo(left + width, top - height);
	} else {
		stream.moveTo(left + width, top - height);
	}
	if (bottomBorder > 0) {
		if (lineWidth != bottomBorder) {
			stream.setLineWidth(bottomBorder);
		}
		lineWidth = bottomBorder;
		stream.lineTo(left, top - height);
	} else {
		stream.moveTo(left, top - height);
	}
	if (leftBorder > 0) {
		if (lineWidth != leftBorder) {
			stream.setLineWidth(leftBorder);
		}
		lineWidth = leftBorder;
		stream.setLineWidth(leftBorder);
		stream.lineTo(left, top);
	} else {
		stream.moveTo(left, top);
	}
	if (topBorder > 0) {
		if (lineWidth != topBorder) {
			stream.setLineWidth(topBorder);
		}
		lineWidth = topBorder;
		stream.setLineWidth(topBorder);
		stream.lineTo(left + width, top);
	}
	stream.stroke();
}
 
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;
}
 
Example 8
Project: kumoreg   File: AttendeeBadge2017.java   Source Code and License Vote up 5 votes
private void drawVerticalAgeRangeText(PDPage page, PDFont font, Attendee attendee) throws IOException {
    PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
    stream.setLineWidth(0.5f);
    stream.beginText();
    stream.setFont(font, 30);

    String stripeColor = attendee.getCurrentAgeRange().getStripeColor();
    stream.setNonStrokingColor(BadgeLib.getForegroundColor(stripeColor));

    stream.newLineAtOffset(172, 275);   // First character position

    String ageString = BadgeLib.getAgeRangeAtCon(attendee, currentDateForAgeCalculation);
    if (ageString.toLowerCase().equals("adult")) {
        stream.showText("A");
        stream.newLineAtOffset(-1, -32);
        stream.showText("D");
        stream.newLineAtOffset(0, -32);
        stream.showText("U");
        stream.newLineAtOffset(2, -32);
        stream.showText("L");
        stream.newLineAtOffset(2, -32);
        stream.showText("T");
    } else if (ageString.toLowerCase().equals("youth")) {
        stream.showText("Y");
        stream.newLineAtOffset(-2, -32);
        stream.showText("O");
        stream.newLineAtOffset(0, -32);
        stream.showText("U");
        stream.newLineAtOffset(4, -32);
        stream.showText("T");
        stream.newLineAtOffset(-3, -32);
        stream.showText("H");
    } else if (ageString.toLowerCase().equals("child")) {
        stream.showText("C");
        stream.newLineAtOffset(0, -32);
        stream.showText("H");
        stream.newLineAtOffset(5, -32);
        stream.showText("I");
        stream.newLineAtOffset(-3, -32);
        stream.showText("L");
        stream.newLineAtOffset(-2, -32);
        stream.showText("D");
    } else {
        stream.showText("V");
        stream.newLineAtOffset(0, -32);
        stream.showText("O");
        stream.newLineAtOffset(5, -32);
        stream.showText("I");
        stream.newLineAtOffset(-5, -32);
        stream.showText("D");
    }
    stream.close();
}
 
Example 9
Project: cat-boot   File: ReportTable.java   Source Code and License Vote up 5 votes
private void placeBorders(PDPageContentStream stream, float startY, float endY, float x, float allowedWidth) throws IOException {
    if (border) {
        stream.setStrokingColor(0, 0, 0);
        stream.setLineWidth(0.3f);
        float y0 = startY - BORDER_Y_DELTA;
        float y1 = endY - (BORDER_Y_DELTA + 1);
        if (!noInnerBorders) {
            if (!noTopBorder || noTopBorder && !placeFirstBorder) {
                stream.moveTo(x, y0);
                stream.lineTo(x + allowedWidth, y0);
                stream.stroke();
            }
            if (!noBottomBorder || noBottomBorder && !placeLastBorder) {
                stream.moveTo(x, y1);
                stream.lineTo(x + allowedWidth, y1);
                stream.stroke();
            }
        } else {
            if (!noTopBorder && placeFirstBorder) {
                stream.moveTo(x, y0);
                stream.lineTo(x + allowedWidth, y0);
                stream.stroke();
            }
            if (!noBottomBorder && placeLastBorder) {
                stream.moveTo(x, y1);
                stream.lineTo(x + allowedWidth, y1);
                stream.stroke();
            }
        }
        float currX = x;
        stream.moveTo(currX, y0);
        stream.lineTo(currX, y1);
        stream.stroke();
        for (float width : cellWidths) {
            if (!noInnerBorders) {
                stream.moveTo(currX, y0);
                stream.lineTo(currX, y1);
                stream.stroke();
            }
            currX += width * allowedWidth;
        }
        stream.moveTo(currX, y0);
        stream.lineTo(currX, y1);
        stream.stroke();
    }
}
 
Example 10
Project: BoxTable   File: TextCell.java   Source Code and License Vote up 4 votes
/**
 * Helper method to draw a fine line
 * 
 * @param stream
 *            The stream used to draw the line
 * @param left
 *            The left end of the line
 * @param top
 *            The vertical position of the line
 * @param width
 *            The width of the line
 * @throws IOException
 *             If writing to the stream fails
 */
private void drawLine(final PDPageContentStream stream, final float left, final float top, final float width) throws IOException {
	stream.setLineWidth(0.25f);
	stream.moveTo(left, top);
	stream.lineTo(left + width, top);
	stream.stroke();
}