org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink. 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: HyperlinkAnnotationProcessor.java    From pdfbox-layout with MIT License 6 votes vote down vote up
@Override
   public void afterRender(PDDocument document) throws IOException {
for (Entry<PDPage, List<Hyperlink>> entry : linkMap.entrySet()) {
    PDPage page = entry.getKey();
    List<Hyperlink> links = entry.getValue();
    for (Hyperlink hyperlink : links) {
	PDAnnotationLink pdLink = null;
	if (hyperlink.getHyperlinkURI().startsWith("#")) {
	    pdLink = createGotoLink(hyperlink);
	} else {
	    pdLink = CompatibilityHelper.createLink(page, 
		    hyperlink.getRect(), hyperlink.getColor(),
		    hyperlink.getLinkStyle(),
		    hyperlink.getHyperlinkURI());
	}
	page.getAnnotations().add(pdLink);
    }

}
   }
 
Example #3
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 #4
Source File: PDAnnotationBleach.java    From DocBleach with MIT License 5 votes vote down vote up
void sanitizeLinkAnnotation(PDAnnotationLink annotationLink) {
  if (annotationLink.getAction() == null) {
    return;
  }
  LOGGER.debug("Found&removed annotation link - action, was {}", annotationLink.getAction());
  pdfBleachSession.recordJavascriptThreat("Annotation", "External link");
  annotationLink.setAction(null);
}
 
Example #5
Source File: PDAnnotationBleach.java    From DocBleach with MIT License 5 votes vote down vote up
void sanitizeAnnotation(PDAnnotation annotation) {
  if (annotation instanceof PDAnnotationLink) {
    sanitizeLinkAnnotation((PDAnnotationLink) annotation);
  }

  if (annotation instanceof PDAnnotationWidget) {
    sanitizeWidgetAnnotation((PDAnnotationWidget) annotation);
  }
}
 
Example #6
Source File: PDAnnotationBleachTest.java    From DocBleach with MIT License 5 votes vote down vote up
@Test
void sanitizeAnnotationLink() {
  PDAnnotationLink annotationLink = new PDAnnotationLink();
  annotationLink.setAction(new PDActionJavaScript());
  instance.sanitizeLinkAnnotation(annotationLink);

  assertThreatsFound(session, 1);
  assertNull(annotationLink.getAction());

  reset(session);

  instance.sanitizeLinkAnnotation(annotationLink);
  assertThreatsFound(session, 0);
}
 
Example #7
Source File: PDLinkAppearanceHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Get the line with of the border.
 * 
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 * 
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth()
{
    PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();

    PDBorderStyleDictionary bs = annotation.getBorderStyle();

    if (bs != null)
    {
        return bs.getWidth();
    }

    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3)
    {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber)
        {
            return ((COSNumber) base).floatValue();
        }
    }

    return 1;
}
 
Example #8
Source File: Splitter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void processAnnotations(PDPage imported) throws IOException
{
    List<PDAnnotation> annotations = imported.getAnnotations();
    for (PDAnnotation annotation : annotations)
    {
        if (annotation instanceof PDAnnotationLink)
        {
            PDAnnotationLink link = (PDAnnotationLink)annotation;   
            PDDestination destination = link.getDestination();
            if (destination == null && link.getAction() != null)
            {
                PDAction action = link.getAction();
                if (action instanceof PDActionGoTo)
                {
                    destination = ((PDActionGoTo)action).getDestination();
                }
            }
            if (destination instanceof PDPageDestination)
            {
                // TODO preserve links to pages within the splitted result  
                ((PDPageDestination) destination).setPage(null);
            }
        }
        // TODO preserve links to pages within the splitted result  
        annotation.setPage(null);
    }
}
 
Example #9
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 #10
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
public static PDAnnotationLink createLink(PDPage page, PDRectangle rect, Color color,
    LinkStyle linkStyle, final String uri) {
PDAnnotationLink pdLink = createLink(page, rect, color, linkStyle);

PDActionURI actionUri = new PDActionURI();
actionUri.setURI(uri);
pdLink.setAction(actionUri);
return pdLink;
   }
 
Example #11
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
public static PDAnnotationLink createLink(PDPage page, PDRectangle rect, Color color,
    LinkStyle linkStyle, final PDDestination destination) {
PDAnnotationLink pdLink = createLink(page, rect, color, linkStyle);

PDActionGoTo gotoAction = new PDActionGoTo();
gotoAction.setDestination(destination);
pdLink.setAction(gotoAction);
return pdLink;
   }
 
Example #12
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 #13
Source File: HyperlinkAnnotationProcessor.java    From pdfbox-layout with MIT License 5 votes vote down vote up
private PDAnnotationLink createGotoLink(Hyperlink hyperlink) {
String anchor = hyperlink.getHyperlinkURI().substring(1);
PageAnchor pageAnchor = anchorMap.get(anchor);
if (pageAnchor == null) {
    throw new IllegalArgumentException(String.format(
	    "anchor named '%s' not found", anchor));
}
PDPageXYZDestination xyzDestination = new PDPageXYZDestination();
xyzDestination.setPage(pageAnchor.getPage());
xyzDestination.setLeft((int) pageAnchor.getX());
xyzDestination.setTop((int) pageAnchor.getY());
return CompatibilityHelper.createLink(pageAnchor.getPage(), hyperlink.getRect(),
	hyperlink.getColor(), hyperlink.getLinkStyle(), xyzDestination);
   }
 
Example #14
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
public static PDAnnotationLink createLink(PDPage page, PDRectangle rect, Color color,
    LinkStyle linkStyle, final String uri) {
PDAnnotationLink pdLink = createLink(page, rect, color, linkStyle);

PDActionURI actionUri = new PDActionURI();
actionUri.setURI(uri);
pdLink.setAction(actionUri);
return pdLink;
   }
 
Example #15
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
public static PDAnnotationLink createLink(PDPage page, PDRectangle rect, Color color,
    LinkStyle linkStyle, final PDDestination destination) {
PDAnnotationLink pdLink = createLink(page, rect, color, linkStyle);

PDActionGoTo gotoAction = new PDActionGoTo();
gotoAction.setDestination(destination);
pdLink.setAction(gotoAction);
return pdLink;
   }
 
Example #16
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;
   }