Java Code Examples for org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink#setRectangle()

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink#setRectangle() . 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 check out the related API usage on the sidebar.
Example 1
Source File: AddLink.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/54986135/how-to-use-pdfbox-to-create-a-link-i-can-click-to-go-to-another-page-in-the-same">
 * How to use PDFBox to create a link i can click to go to another page in the same document
 * </a>
 * <p>
 * The OP used destination.setPageNumber which is not ok for local
 * links. Furthermore, he forgot to add the link to the page and
 * to give it a rectangle.
 * </p>
 */
@Test
public void testAddLinkToMwb_I_201711() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("/mkl/testarea/pdfbox2/content/mwb_I_201711.pdf")) {
        PDDocument document = Loader.loadPDF(resource);

        PDPage page = document.getPage(1);

        PDAnnotationLink link         = new PDAnnotationLink();
        PDPageDestination destination = new PDPageFitWidthDestination();
        PDActionGoTo action           = new PDActionGoTo();

        //destination.setPageNumber(2);
        destination.setPage(document.getPage(2));
        action.setDestination(destination);
        link.setAction(action);
        link.setPage(page);

        link.setRectangle(page.getMediaBox());
        page.getAnnotations().add(link);

        document.save(new File(RESULT_FOLDER, "mwb_I_201711-with-link.pdf"));
    }
}
 
Example 2
Source File: PdfManipulator.java    From estatio with Apache License 2.0 6 votes vote down vote up
private void addHyperlink(
        final float x, final float y,
        final String hyperlink,
        final PDPage pdPage) throws IOException {

    PDAnnotationLink txtLink = new PDAnnotationLink();

    PDRectangle position = new PDRectangle();
    PDBorderStyleDictionary underline = new PDBorderStyleDictionary();
    underline.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
    txtLink.setBorderStyle(underline);

    position.setLowerLeftX(x);
    position.setLowerLeftY(y);
    position.setUpperRightX(X_MARGIN_LEFT + BOX_WIDTH);
    position.setUpperRightY(y + TEXT_LINE_HEIGHT);
    txtLink.setRectangle(position);

    PDActionURI action = new PDActionURI();
    action.setURI(hyperlink);
    txtLink.setAction(action);
    pdPage.getAnnotations().add(txtLink);
}
 
Example 3
Source File: ReportLink.java    From cat-boot with Apache License 2.0 5 votes vote down vote up
private float addLink(PDDocument document, int pageNumber, float startX, float startY, PdfTextStyle textConfig) {
    PDAnnotationLink txtLink = new PDAnnotationLink();
    txtLink.setColor(textConfig.getColor());

    PDBorderStyleDictionary underline = getLinkUnderline();
    txtLink.setBorderStyle(underline);

    try {
        float textWidth = (textConfig.getFont().getStyle(textConfig.getStyle()).getStringWidth(text) / 1000) * textConfig.getFontSize();

        float startLinkY = startY + textConfig.getFontSize();
        float endLinkY = startY - underline.getWidth();

        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(startX);
        position.setLowerLeftY(startLinkY);
        position.setUpperRightX(startX + textWidth);
        position.setUpperRightY(endLinkY);
        txtLink.setRectangle(position);

        PDActionURI action = new PDActionURI();
        action.setURI(link);
        txtLink.setAction(action);

        PDPage page = document.getDocumentCatalog().getPages().get(pageNumber);
        page.getAnnotations().add(txtLink);

        return endLinkY;
    } catch (IOException e) {
        LOG.warn("Could not add link: " + e.getClass() + " - " + e.getMessage());
        return startY;
    }
}
 
Example 4
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
private static PDAnnotationLink createLink(PDPage page, PDRectangle rect, Color color,
    LinkStyle linkStyle) {
PDAnnotationLink pdLink = new PDAnnotationLink();
pdLink.setBorderStyle(toBorderStyle(linkStyle));
PDRectangle rotatedRect = transformToPageRotation(rect, page);
pdLink.setRectangle(rotatedRect);
setAnnotationColor(pdLink, color);
return pdLink;
   }
 
Example 5
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
private static PDAnnotationLink createLink(PDPage page, PDRectangle rect, Color color,
    LinkStyle linkStyle) {
PDAnnotationLink pdLink = new PDAnnotationLink();
pdLink.setBorderStyle(toBorderStyle(linkStyle));
PDRectangle rotatedRect = transformToPageRotation(rect, page);
pdLink.setRectangle(rotatedRect);
setAnnotationColor(pdLink, color);
return pdLink;
   }