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

The following examples show how to use org.apache.pdfbox.pdmodel.PDPageContentStream#setLineWidth() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: easytable   File: DrawingUtil.java    License: MIT License 5 votes vote down vote up
public static void drawLine(PDPageContentStream contentStream, PositionedLine line) throws IOException {
    contentStream.moveTo(line.getStartX(), line.getStartY());
    contentStream.setLineWidth(line.getWidth());
    contentStream.lineTo(line.getEndX(), line.getEndY());
    contentStream.setStrokingColor(line.getColor());
    contentStream.stroke();
    contentStream.setStrokingColor(line.getResetColor());
}
 
Example 2
Source Project: cat-boot   File: PdfBoxHelper.java    License: Apache License 2.0 5 votes vote down vote up
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
Source Project: testarea-pdfbox2   File: AddImage.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/50988007/clip-an-image-with-pdfbox">
 * Clip an image with PDFBOX
 * </a>
 * <p>
 * This test demonstrates how to clip an image and frame the clipping area.
 * </p>
 */
@SuppressWarnings("deprecation")
@Test
public void testImageAddClipped() throws IOException {
    try (   InputStream imageResource = getClass().getResourceAsStream("Willi-1.jpg")   )
    {
        PDDocument doc = new PDDocument();
        PDImageXObject pdImage = PDImageXObject.createFromByteArray(doc, ByteStreams.toByteArray(imageResource), "Willi");

        int w = pdImage.getWidth();
        int h = pdImage.getHeight();

        PDPage page = new PDPage();
        doc.addPage(page);
        PDRectangle cropBox = page.getCropBox();
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);

        contentStream.setStrokingColor(25, 200, 25);
        contentStream.setLineWidth(4);
        contentStream.moveTo(cropBox.getLowerLeftX(), cropBox.getLowerLeftY() + h/2);
        contentStream.lineTo(cropBox.getLowerLeftX() + w/3, cropBox.getLowerLeftY() + 2*h/3);
        contentStream.lineTo(cropBox.getLowerLeftX() + w, cropBox.getLowerLeftY() + h/2);
        contentStream.lineTo(cropBox.getLowerLeftX() + w/3, cropBox.getLowerLeftY() + h/3);
        contentStream.closePath();
        //contentStream.clip();
        contentStream.appendRawCommands("W ");
        contentStream.stroke();

        contentStream.drawImage(pdImage, cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), w, h);

        contentStream.close();

        doc.save(new File(RESULT_FOLDER, "image-clipped.pdf"));
        doc.close();
    }
}
 
Example 4
Source Project: estatio   File: PdfManipulator.java    License: Apache License 2.0 5 votes vote down vote up
private void addBox(
        final float x, final float y, final float height,
        final PDPageContentStream cs) throws IOException {
    cs.setLineWidth(BOX_LINE_WIDTH);
    cs.setStrokingColor(Color.DARK_GRAY);
    cs.addRect(
            x - (float) BOX_LINE_WIDTH,
            y - (float) BOX_LINE_WIDTH,
            BOX_WIDTH + 2* (float) BOX_LINE_WIDTH,
            height + 2* (float) BOX_LINE_WIDTH);
    cs.stroke();
    cs.setNonStrokingColor(BOX_FILL);
    cs.addRect(x, y, BOX_WIDTH, height);
    cs.fill();
}
 
Example 5
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();
            }
        }
        if(!noHorizontalBorders) {
            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 6
Source Project: cat-boot   File: ReportTable.java    License: Apache License 2.0 4 votes vote down vote up
private void placeBorders(PDPageContentStream stream, float startY, float endY, float x, float allowedWidth,
                          boolean isFirstLine, boolean isLastLine) throws IOException {
    if (!border) {
        return;
    }
    stream.setStrokingColor(0, 0, 0);
    stream.setLineWidth(0.3f);
    float y0 = startY;
    float y1 = endY;
    float x1 = x + allowedWidth;
    if (drawInnerHorizontal) {
        if (!noBottomBorder || noBottomBorder && !isLastLine) {
            drawLine(stream, x, x1, y1, y1);
        }
    }

    // top border
    if (!noTopBorder && isFirstLine) {
        drawLine(stream, x, x1, y0, y0);
    }
    // bottom border
    if (!noBottomBorder && isLastLine) {
        drawLine(stream, x, x1, y1, y1);
    }

    float currentX = x;
    for (int i = 0; i < cellWidths.length; i++) {
        float width = cellWidths[i];
        if (
                (i == 0 && drawOuterVertical) || // left
                        (i > 0 && drawInnerVertical) // inner
        ) {
            drawLine(stream, currentX, currentX, y0, y1);
        }
        currentX += width * allowedWidth;
    }
    // draw last
    if (drawOuterVertical) {
        drawLine(stream, currentX, currentX, y0, y1);
    }
}
 
Example 7
/**
 * <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 = Loader.loadPDF(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();
    }
}