Java Code Examples for com.itextpdf.text.pdf.PdfReader#getPdfObject()

The following examples show how to use com.itextpdf.text.pdf.PdfReader#getPdfObject() . 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: ImageExtraction.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * @see #testExtractImageLikeSteveB()
 */
private static List<BufferedImage> FindImages(PdfReader reader, PdfDictionary pdfPage) throws IOException
{
    List<BufferedImage> result = new ArrayList<>(); 
    Iterable<PdfObject> imgPdfObject = FindImageInPDFDictionary(pdfPage);
    for (PdfObject image : imgPdfObject)
    {
        int xrefIndex = ((PRIndirectReference)image).getNumber();
        PdfObject stream = reader.getPdfObject(xrefIndex);
        // Exception occurs here :
        PdfImageObject pdfImage = new PdfImageObject((PRStream)stream);
        BufferedImage img = pdfImage.getBufferedImage();

        // Do something with the image
        result.add(img);
    }
    return result;
}
 
Example 2
Source File: InsertPage.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <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 3
Source File: SameFieldTwice.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/31402602/how-to-rename-only-the-first-found-duplicate-acrofield-in-pdf">
 * How to rename only the first found duplicate acrofield in pdf?
 * </a>
 * <br>
 * <a href="http://s000.tinyupload.com/index.php?file_id=34970992934525199618">
 * test_duplicate_field2.pdf
 * </a>
 * <p>
 * Demonstration of how to transform generate a new field for a widget.
 * </p> 
 */
@Test
public void testWidgetToField() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("test_duplicate_field2.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test_duplicate_field2-widgetToField.pdf"))   )
    {
        PdfReader reader = new PdfReader(resource);

        PdfDictionary form = reader.getCatalog().getAsDict(PdfName.ACROFORM);
        PdfArray fields = form.getAsArray(PdfName.FIELDS);
        for (PdfObject object: fields)
        {
            PdfDictionary field = (PdfDictionary) PdfReader.getPdfObject(object);
            if ("Text1".equals(field.getAsString(PdfName.T).toString()))
            {
                PdfDictionary newField = new PdfDictionary();
                PRIndirectReference newFieldRef = reader.addPdfObject(newField);
                fields.add(newFieldRef);
                newField.putAll(field);
                newField.put(PdfName.T, new PdfString("foobar"));
                PdfArray newKids = new PdfArray();
                newField.put(PdfName.KIDS, newKids);
                PdfArray kids = field.getAsArray(PdfName.KIDS);
                PdfObject widget = kids.remove(0);
                newKids.add(widget);
                PdfDictionary widgetDict = (PdfDictionary) PdfReader.getPdfObject(widget);
                widgetDict.put(PdfName.PARENT, newFieldRef);
                break;
            }
        }

        PdfStamper stamper = new PdfStamper(reader, result);
        stamper.close();
    }
}
 
Example 4
Source File: MarkAnnotationReadOnly.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/37275267/how-to-make-pdf-annotation-as-read-only-using-itext">
 * how to make pdf annotation as read only using itext?
 * </a>
 * <br/>
 * test-annotated.pdf <i>simple PDF with sticky note</i>
 * 
 * <p>
 * This test shows how to set the read-only flags of all annotations of a document.
 * </p>
 */
@Test
public void testMarkAnnotationsReadOnly() throws IOException, DocumentException
{
    try (   InputStream resourceStream = getClass().getResourceAsStream("test-annotated.pdf");
            OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "test-annotated-ro.pdf"))    )
    {
        PdfReader reader = new PdfReader(resourceStream);
        PdfStamper stamper = new PdfStamper(reader, outputStream);

        for (int page = 1; page <= reader.getNumberOfPages(); page++)
        {
            PdfDictionary pageDictionary = reader.getPageN(page);
            PdfArray annotationArray = pageDictionary.getAsArray(PdfName.ANNOTS);
            if (annotationArray == null)
                continue;
            for (PdfObject object : annotationArray)
            {
                PdfObject directObject = PdfReader.getPdfObject(object);
                if (directObject instanceof PdfDictionary)
                {
                    PdfDictionary annotationDictionary = (PdfDictionary) directObject;
                    PdfNumber flagsNumber = annotationDictionary.getAsNumber(PdfName.F);
                    int flags = flagsNumber != null ? flagsNumber.intValue() : 0;
                    flags |= PdfAnnotation.FLAGS_READONLY;
                    annotationDictionary.put(PdfName.F, new PdfNumber(flags));
                }
            }
        }

        stamper.close();
    }
}
 
Example 5
Source File: VerifyAcroFormSignatures.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This method checks the signatures referenced from the AcroForm Fields.  
 */
void verify(PdfReader reader) throws GeneralSecurityException
{
    PdfDictionary top = (PdfDictionary)PdfReader.getPdfObjectRelease(reader.getCatalog().get(PdfName.ACROFORM));
    if (top == null)
    {
        System.out.println("No AcroForm, so nothing to verify");
        return;
    }

    PdfArray arrfds = (PdfArray)PdfReader.getPdfObjectRelease(top.get(PdfName.FIELDS));
    if (arrfds == null || arrfds.isEmpty())
    {
        System.out.println("No AcroForm Fields, so nothing to verify");
        return;
    }

    for (PdfObject object : arrfds)
    {
        object = PdfReader.getPdfObject(object);
        if (object == null)
        {
            System.out.println("* A null entry.");
        }
        else if (!object.isDictionary())
        {
            System.out.println("* A non-dictionary entry.");
        }
        else
        {
            verify(reader, (PdfDictionary) object, null, null, null, false);
        }
    }
}
 
Example 6
Source File: VerifyAcroFormSignatures.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Copied from {@link AcroFields#verifySignature(String, String)} and changed to work on the
 * given signature dictionary.
 */
public PdfPKCS7 verifySignature(PdfReader reader, PdfDictionary v, String provider) {
    if (v == null)
        return null;
    try {
        PdfName sub = v.getAsName(PdfName.SUBFILTER);
        PdfString contents = v.getAsString(PdfName.CONTENTS);
        PdfPKCS7 pk = null;
        if (sub.equals(PdfName.ADBE_X509_RSA_SHA1)) {
            PdfString cert = v.getAsString(PdfName.CERT);
            if (cert == null)
                cert = v.getAsArray(PdfName.CERT).getAsString(0);
            pk = new PdfPKCS7(contents.getOriginalBytes(), cert.getBytes(), provider);
        }
        else
            pk = new PdfPKCS7(contents.getOriginalBytes(), sub, provider);
        updateByteRange(reader, pk, v);
        PdfString str = v.getAsString(PdfName.M);
        if (str != null)
            pk.setSignDate(PdfDate.decode(str.toString()));
        PdfObject obj = PdfReader.getPdfObject(v.get(PdfName.NAME));
        if (obj != null) {
          if (obj.isString())
            pk.setSignName(((PdfString)obj).toUnicodeString());
          else if(obj.isName())
            pk.setSignName(PdfName.decodeName(obj.toString()));
        }
        str = v.getAsString(PdfName.REASON);
        if (str != null)
            pk.setReason(str.toUnicodeString());
        str = v.getAsString(PdfName.LOCATION);
        if (str != null)
            pk.setLocation(str.toUnicodeString());
        return pk;
    }
    catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}