Search in sources :

Example 41 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDStandardAttributeObject method setArrayOfString.

/**
 * Sets an array of strings.
 *
 * @param name the attribute name
 * @param values the array of strings
 */
protected void setArrayOfString(String name, String[] values) {
    COSBase oldBase = this.getCOSObject().getDictionaryObject(name);
    COSArray array = new COSArray();
    for (String value : values) {
        array.add(new COSString(value));
    }
    this.getCOSObject().setItem(name, array);
    COSBase newBase = this.getCOSObject().getDictionaryObject(name);
    this.potentiallyNotifyChanged(oldBase, newBase);
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 42 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class COSWriter method write.

/**
 * This will write the pdf document. If signature should be created externally,
 * {@link #writeExternalSignature(byte[])} should be invoked to set signature after calling this
 * method.
 *
 * @param doc The document to write.
 * @param signInterface class to be used for signing; {@code null} if external signing would be
 * performed or there will be no signing at all
 *
 * @throws IOException If an error occurs while generating the data.
 * @throws IllegalStateException If the document has an encryption dictionary but no protection
 * policy.
 */
public void write(PDDocument doc, SignatureInterface signInterface) throws IOException {
    Long idTime = doc.getDocumentId() == null ? System.currentTimeMillis() : doc.getDocumentId();
    pdDocument = doc;
    signatureInterface = signInterface;
    if (incrementalUpdate) {
        prepareIncrement(doc);
    }
    // if the document says we should remove encryption, then we shouldn't encrypt
    if (doc.isAllSecurityToBeRemoved()) {
        willEncrypt = false;
        // also need to get rid of the "Encrypt" in the trailer so readers
        // don't try to decrypt a document which is not encrypted
        COSDocument cosDoc = doc.getDocument();
        COSDictionary trailer = cosDoc.getTrailer();
        trailer.removeItem(COSName.ENCRYPT);
    } else {
        if (pdDocument.getEncryption() != null) {
            if (!incrementalUpdate) {
                SecurityHandler securityHandler = pdDocument.getEncryption().getSecurityHandler();
                if (!securityHandler.hasProtectionPolicy()) {
                    throw new IllegalStateException("PDF contains an encryption dictionary, please remove it with " + "setAllSecurityToBeRemoved() or set a protection policy with protect()");
                }
                securityHandler.prepareDocumentForEncryption(pdDocument);
            }
            willEncrypt = true;
        } else {
            willEncrypt = false;
        }
    }
    COSDocument cosDoc = pdDocument.getDocument();
    COSDictionary trailer = cosDoc.getTrailer();
    COSArray idArray = null;
    boolean missingID = true;
    COSBase base = trailer.getDictionaryObject(COSName.ID);
    if (base instanceof COSArray) {
        idArray = (COSArray) base;
        if (idArray.size() == 2) {
            missingID = false;
        }
    }
    // check for an existing documentID
    if (idArray != null && idArray.size() == 2) {
        missingID = false;
    }
    if (missingID || incrementalUpdate) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            // should never happen
            throw new RuntimeException(e);
        }
        // algorithm says to use time/path/size/values in doc to generate the id.
        // we don't have path or size, so do the best we can
        md5.update(Long.toString(idTime).getBytes(Charsets.ISO_8859_1));
        COSDictionary info = trailer.getCOSDictionary(COSName.INFO);
        if (info != null) {
            for (COSBase cosBase : info.getValues()) {
                md5.update(cosBase.toString().getBytes(Charsets.ISO_8859_1));
            }
        }
        // reuse origin documentID if available as first value
        COSString firstID = missingID ? new COSString(md5.digest()) : (COSString) idArray.get(0);
        // it's ok to use the same ID for the second part if the ID is created for the first time
        COSString secondID = missingID ? firstID : new COSString(md5.digest());
        idArray = new COSArray();
        idArray.add(firstID);
        idArray.add(secondID);
        trailer.setItem(COSName.ID, idArray);
    }
    cosDoc.accept(this);
}
Also used : SecurityHandler(com.tom_roush.pdfbox.pdmodel.encryption.SecurityHandler) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDocument(com.tom_roush.pdfbox.cos.COSDocument) COSBase(com.tom_roush.pdfbox.cos.COSBase) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 43 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDActionURITest method testUTF16BEURI.

/**
 * PDFBOX-3913: Check that URIs encoded in UTF16 (BE) are also supported.
 *
 * @throws IOException
 */
@Test
public void testUTF16BEURI() throws IOException {
    PDActionURI actionURI = new PDActionURI();
    // found in govdocs file 534948.pdf
    COSString utf16URI = COSString.parseHex("FEFF0068007400740070003A002F002F00770077" + "0077002E006E00610070002E006500640075002F0063006100740061006C006F006700" + "2F00310031003100340030002E00680074006D006C");
    actionURI.getCOSObject().setItem(COSName.URI, utf16URI);
    assertEquals("http://www.nap.edu/catalog/11140.html", actionURI.getURI());
}
Also used : COSString(com.tom_roush.pdfbox.cos.COSString) Test(org.junit.Test)

Example 44 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDActionURITest method testUTF16LEURI.

/**
 * PDFBOX-3913: Check that URIs encoded in UTF16 (LE) are also supported.
 *
 * @throws IOException
 */
@Test
public void testUTF16LEURI() throws IOException {
    PDActionURI actionURI = new PDActionURI();
    COSString utf16URI = COSString.parseHex("FFFE68007400740070003A00");
    actionURI.getCOSObject().setItem(COSName.URI, utf16URI);
    assertEquals("http:", actionURI.getURI());
}
Also used : COSString(com.tom_roush.pdfbox.cos.COSString) Test(org.junit.Test)

Example 45 with COSString

use of com.tom_roush.pdfbox.cos.COSString in project PdfBox-Android by TomRoush.

the class PDChoiceTest method getOptionsFromCOSArray.

@Test
public void getOptionsFromCOSArray() {
    PDChoice choiceField = new PDComboBox(acroForm);
    COSArray choiceFieldOptions = new COSArray();
    // add entry to options
    COSArray entry = new COSArray();
    entry.add(new COSString(" "));
    choiceFieldOptions.add(entry);
    // add entry to options
    entry = new COSArray();
    entry.add(new COSString("A"));
    choiceFieldOptions.add(entry);
    // add entry to options
    entry = new COSArray();
    entry.add(new COSString("B"));
    choiceFieldOptions.add(entry);
    // add the options using the low level COS model as the PD model will
    // abstract the COSArray
    choiceField.getCOSObject().setItem(COSName.OPT, choiceFieldOptions);
    assertEquals(options, choiceField.getOptions());
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSString(com.tom_roush.pdfbox.cos.COSString) Test(org.junit.Test)

Aggregations

COSString (com.tom_roush.pdfbox.cos.COSString)65 COSArray (com.tom_roush.pdfbox.cos.COSArray)33 COSBase (com.tom_roush.pdfbox.cos.COSBase)24 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)5 COSName (com.tom_roush.pdfbox.cos.COSName)5 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)4 Map (java.util.Map)4 COSBoolean (com.tom_roush.pdfbox.cos.COSBoolean)2 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)2 MessageDigest (java.security.MessageDigest)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)1