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

The following are Jave code examples for showing how to use stroke() 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: pcm-api   File: PdfBoxServiceImpl.java   Source Code and License Vote up 6 votes
private void drawTableLine(PDPageContentStream contentStream, TableAttribute tableAttribute, String[][] tableContent) throws IOException {
    final int rows = tableContent.length;
    final int cols = tableContent[0].length;
    final float rowHeight = Optional.of(tableAttribute.getRowHeight())
            .orElse(PdfBoxStyle.DEFAULT_TABLE_ROW_HEIGHT);
    log.debug("The row height of the table is: " + rowHeight);

    //set border color
    contentStream.setStrokingColor(tableAttribute.getBorderColor());

    //draw the rows
    final float tableWidth = calculateTableWidth(tableAttribute.getColumns());
    log.debug("The number of the table rows is: " + tableAttribute.getColumns().size());
    float nextLineY = tableAttribute.getYCoordinate();
    for (int i = 0; i <= rows; i++) {
        contentStream.moveTo(tableAttribute.getXCoordinate(), nextLineY);
        contentStream.lineTo(tableAttribute.getXCoordinate() + tableWidth, nextLineY);
        contentStream.stroke();
        nextLineY -= rowHeight;
    }

    //draw the columns
    final float tableHeight = rowHeight * rows;
    float nextLineX = tableAttribute.getXCoordinate();
    for (int i = 0; i < cols; i++) {
        contentStream.moveTo(nextLineX, tableAttribute.getYCoordinate());
        contentStream.lineTo(nextLineX, tableAttribute.getYCoordinate() - tableHeight);
        contentStream.stroke();
        nextLineX += tableAttribute.getColumns().get(i).getCellWidth();
    }
    //draw the right border
    contentStream.moveTo(nextLineX, tableAttribute.getYCoordinate());
    contentStream.lineTo(nextLineX, tableAttribute.getYCoordinate() - tableHeight);
    contentStream.stroke();
}
 
Example 2
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 3
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 4
Project: testarea-pdfbox2   File: TextAndGraphics.java   Source Code and License Vote up 6 votes
/**
 * <a href="https://stackoverflow.com/questions/44503236/how-to-write-text-draw-a-line-and-then-again-write-text-in-a-pdf-file-using-pdf">
 * How to write text, draw a line and then again write text in a pdf file using PDFBox
 * </a>
 * <p>
 * This test shows how to draw tetx, then graphics, then again text.
 * </p>
 */
@Test
public void testDrawTextLineText() throws IOException
{
    PDFont font = PDType1Font.HELVETICA;
    float fontSize = 14;
    float fontHeight = fontSize;
    float leading = 20;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
    Date date = new Date();

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

    PDPageContentStream contentStream = new PDPageContentStream(doc, page);
    contentStream.setFont(font, fontSize);

    float yCordinate = page.getCropBox().getUpperRightY() - 30;
    float startX = page.getCropBox().getLowerLeftX() + 30;
    float endX = page.getCropBox().getUpperRightX() - 30;

    contentStream.beginText();
    contentStream.newLineAtOffset(startX, yCordinate);
    contentStream.showText("Entry Form � Header");
    yCordinate -= fontHeight;  //This line is to track the yCordinate
    contentStream.newLineAtOffset(0, -leading);
    yCordinate -= leading;
    contentStream.showText("Date Generated: " + dateFormat.format(date));
    yCordinate -= fontHeight;
    contentStream.endText(); // End of text mode

    contentStream.moveTo(startX, yCordinate);
    contentStream.lineTo(endX, yCordinate);
    contentStream.stroke();
    yCordinate -= leading;

    contentStream.beginText();
    contentStream.newLineAtOffset(startX, yCordinate);
    contentStream.showText("Name: XXXXX");
    contentStream.endText();

    contentStream.close();
    doc.save(new File(RESULT_FOLDER, "textLineText.pdf"));
}
 
Example 5
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 6
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 7
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();
}