Java Code Examples for com.itextpdf.text.pdf.PdfDictionary#getAsString()

The following examples show how to use com.itextpdf.text.pdf.PdfDictionary#getAsString() . 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 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 2
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);
    }
}
 
Example 3
Source File: PortfolioFileExtraction.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
static void collectFolders(Map<Integer, File> collection, PdfDictionary folder, File baseDir)
{
    PdfString name = folder.getAsString(PdfName.NAME);
    File folderDir = new File(baseDir, name.toString());
    folderDir.mkdirs();
    PdfNumber id = folder.getAsNumber(PdfName.ID);
    collection.put(id.intValue(), folderDir);

    PdfDictionary next = folder.getAsDict(PdfName.NEXT);
    if (next != null)
        collectFolders(collection, next, baseDir);
    PdfDictionary child = folder.getAsDict(CHILD);
    if (child != null)
        collectFolders(collection, child, folderDir);
}
 
Example 4
Source File: SignatureExtractor.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * This method extracts integrated signature information from the PDF
 * this class is instantiated with.
 *
 * @return
 */
//@SuppressWarnings("unchecked")
public Map<String, SignatureData> extractSignatures() {
    final Map<String, SignatureData> result = new HashMap<String, SignatureData>();
    final AcroFields fields = reader.getAcroFields();
    for (String name : fields.getSignatureNames()) {
        PdfDictionary sigDict = fields.getSignatureDictionary(name);
        PdfString contents = sigDict.getAsString(PdfName.CONTENTS);
        PdfName subFilter = sigDict.getAsName(PdfName.SUBFILTER);
        if (contents != null) {
            byte[] contentBytes = contents.getOriginalBytes();
            byte[] containerBytes = null;
            /*
            ContentInfo contentInfo = null;
            try {
                contentInfo = new ContentInfoImpl(contentBytes);
                byte[] bytes = contentInfo.getEncoded();
                if (bytes.length <= contentBytes.length)
                {
                    boolean equal = true;
                    for (int i = 0; i < bytes.length; i++)
                    {
                        if (bytes[i] != contentBytes[i])
                        {
                            System.err.println("Re-encoded differs at " + i);
                            equal = false;
                            break;
                        }
                    }
                    if (equal)
                        containerBytes = bytes;
                }
                else
                {
                    System.err.println("Re-encoded data too long");
                }
            }
            catch (GeneralSecurityException e)
            {
                System.err.println("Failure decoding content as container.");
                e.printStackTrace();
            }
            */

            Date signingTime = null;
            Object pdfDateEntry = sigDict.get(PdfName.M);
            if (pdfDateEntry != null) {
                Calendar cal = PdfDate.decode(pdfDateEntry.toString());
                if (cal != null) {
                    signingTime = cal.getTime();
                }
            }

            result.put(name, new SignatureData(/*contentInfo,*/ containerBytes, contentBytes, subFilter, signingTime));
        }
    }
    return result;
}
 
Example 5
Source File: VerifyAcroFormSignatures.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
void verify(PdfReader reader, PdfObject object, String baseName, PdfName baseType, PdfObject baseValue, boolean processed) throws GeneralSecurityException
{
    if (object == null)
    {
        System.out.println("* A null entry.");
    }
    else if (!object.isDictionary())
    {
        System.out.println("* A non-dictionary entry.");
    }
    else
    {
        PdfDictionary field = (PdfDictionary) object;
        
        String name;
        PdfName type;
        PdfObject value;

        PdfString partialObject = field.getAsString(PdfName.T);
        if (partialObject == null)
        {
            System.out.println("* An anonymous entry.");
            name = baseName;
        }
        else
        {
            String partial = partialObject.toString();
            System.out.printf("* A named entry: %s\n", partial);
            name = baseName == null ? partial : String.format("%s:%s", baseName, partial);
        }
        System.out.printf("  FQP: %s\n", name == null ? "" : name);

        PdfName typeHere = field.getAsName(PdfName.FT);
        if (typeHere != null)
        {
            type = typeHere;
            System.out.printf("  Type: %s\n", type);
        }
        else
        {
            type = baseType;
            if (type == null)
            {
                System.out.printf("  Type: -\n");
            }
            else
            {
                System.out.printf("  Type: %s (inherited)\n", type);
            }
        }

        PdfObject valueHere = field.getDirectObject(PdfName.V);
        if (valueHere != null)
        {
            value = valueHere;
            processed = false;
            System.out.printf("  Value: present\n");
        }
        else
        {
            value = baseValue;
            if (value == null)
            {
                System.out.printf("  Value: -\n");
            }
            else
            {
                System.out.printf("  Value: inherited\n");
            }
        }

        if (PdfName.SIG.equals(type) && value instanceof PdfDictionary && !processed)
        {
            processed = verify(reader, (PdfDictionary) value);
        }

        PdfArray kids = field.getAsArray(PdfName.KIDS);
        if (kids != null)
        {
            for (PdfObject kid : kids)
            {
                verify(reader, kid, name, type, value, processed);
            }
        }
    }
}