Java Code Examples for com.itextpdf.text.pdf.PdfReader#getNormalizedRectangle()
The following examples show how to use
com.itextpdf.text.pdf.PdfReader#getNormalizedRectangle() .
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: InsertPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
/** * <p> * A primitive attempt at copying links from page <code>sourcePage</code> * of <code>PdfReader reader</code> to page <code>targetPage</code> of * <code>PdfStamper stamper</code>. * </p> * <p> * This method is meant only for the use case at hand, i.e. copying a link * to an external URI without expecting any advanced features. * </p> */ void copyLinks(PdfStamper stamper, int targetPage, PdfReader reader, int sourcePage) { PdfDictionary sourcePageDict = reader.getPageNRelease(sourcePage); PdfArray annotations = sourcePageDict.getAsArray(PdfName.ANNOTS); if (annotations != null && annotations.size() > 0) { for (PdfObject annotationObject : annotations) { annotationObject = PdfReader.getPdfObject(annotationObject); if (!annotationObject.isDictionary()) continue; PdfDictionary annotation = (PdfDictionary) annotationObject; if (!PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) continue; PdfArray rectArray = annotation.getAsArray(PdfName.RECT); if (rectArray == null || rectArray.size() < 4) continue; Rectangle rectangle = PdfReader.getNormalizedRectangle(rectArray); PdfName hightLight = annotation.getAsName(PdfName.H); if (hightLight == null) hightLight = PdfAnnotation.HIGHLIGHT_INVERT; PdfDictionary actionDict = annotation.getAsDict(PdfName.A); if (actionDict == null || !PdfName.URI.equals(actionDict.getAsName(PdfName.S))) continue; PdfString urlPdfString = actionDict.getAsString(PdfName.URI); if (urlPdfString == null) continue; PdfAction action = new PdfAction(urlPdfString.toString()); PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(), rectangle, hightLight, action); stamper.addAnnotation(link, targetPage); } } }
Example 2
Source File: ProcessLink.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
/** * <a href="https://stackoverflow.com/questions/49370352/how-do-i-get-a-get-destination-page-of-a-link-in-pdf-file"> * How do I get a get destination page of a link in PDF file? * </a> * <br/> * local-link.pdf - output of the test {@link CreateLink}. * <p> * This test shows how to access data of the target page, once by * directly reading from the page dictionary referenced from the * link destination, once by first determining the page number and * then using {@link PdfReader} helper methods. * </p> */ @Test public void testDetermineTargetPage() throws IOException { try ( InputStream src = getClass().getResourceAsStream("local-link.pdf") ) { PdfReader reader = new PdfReader(src); PdfDictionary page = reader.getPageN(1); PdfArray annots = page.getAsArray(PdfName.ANNOTS); for (int i = 0; i < annots.size(); i++) { PdfDictionary annotation = annots.getAsDict(i); if (PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) { PdfArray d = annotation.getAsArray(PdfName.DEST); if (d == null) { PdfDictionary action = annotation.getAsDict(PdfName.A); if (action != null) d = action.getAsArray(PdfName.D); } if (d != null && d.size() > 0) { System.out.println("Next destination -"); PdfIndirectReference pageReference = d.getAsIndirectObject(0); // Work with target dictionary directly PdfDictionary pageDict = d.getAsDict(0); PdfArray boxArray = pageDict.getAsArray(PdfName.CROPBOX); if (boxArray == null) { boxArray = pageDict.getAsArray(PdfName.MEDIABOX); } Rectangle box = PdfReader.getNormalizedRectangle(boxArray); System.out.printf("* Target page object %s has cropbox %s\n", pageReference, box); // Work via page number for (int pageNr = 1; pageNr <= reader.getNumberOfPages(); pageNr++) { PRIndirectReference pp = reader.getPageOrigRef(pageNr); if (pp.getGeneration() == pageReference.getGeneration() && pp.getNumber() == pageReference.getNumber()) { System.out.printf("* Target page %s has cropbox %s\n", pageNr, reader.getCropBox(pageNr)); break; } } } } } } }