Search in sources :

Example 16 with COSDictionary

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

the class COSWriter method visitFromArray.

@Override
public Object visitFromArray(COSArray obj) throws IOException {
    int count = 0;
    getStandardOutput().write(ARRAY_OPEN);
    for (Iterator<COSBase> i = obj.iterator(); i.hasNext(); ) {
        COSBase current = i.next();
        if (current instanceof COSDictionary) {
            if (current.isDirect()) {
                visitFromDictionary((COSDictionary) current);
            } else {
                addObjectToWrite(current);
                writeReference(current);
            }
        } else if (current instanceof COSObject) {
            COSBase subValue = ((COSObject) current).getObject();
            if (willEncrypt || incrementalUpdate || subValue instanceof COSDictionary || subValue == null) {
                // PDFBOX-4308: added willEncrypt to prevent an object
                // that is referenced several times from being written
                // direct and indirect, thus getting encrypted
                // with wrong object number or getting encrypted twice
                addObjectToWrite(current);
                writeReference(current);
            } else {
                subValue.accept(this);
            }
        } else if (current == null) {
            COSNull.NULL.accept(this);
        } else {
            current.accept(this);
        }
        count++;
        if (i.hasNext()) {
            if (count % 10 == 0) {
                getStandardOutput().writeEOL();
            } else {
                getStandardOutput().write(SPACE);
            }
        }
    }
    getStandardOutput().write(ARRAY_CLOSE);
    getStandardOutput().writeEOL();
    return null;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 17 with COSDictionary

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

the class COSWriter method doWriteBody.

/**
 * This will write the body of the document.
 *
 * @param doc The document to write the body for.
 *
 * @throws IOException If there is an error writing the data.
 */
protected void doWriteBody(COSDocument doc) throws IOException {
    COSDictionary trailer = doc.getTrailer();
    COSDictionary root = trailer.getCOSDictionary(COSName.ROOT);
    COSDictionary info = trailer.getCOSDictionary(COSName.INFO);
    COSDictionary encrypt = trailer.getCOSDictionary(COSName.ENCRYPT);
    if (root != null) {
        addObjectToWrite(root);
    }
    if (info != null) {
        addObjectToWrite(info);
    }
    doWriteObjects();
    willEncrypt = false;
    if (encrypt != null) {
        addObjectToWrite(encrypt);
    }
    doWriteObjects();
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary)

Example 18 with COSDictionary

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

the class COSWriter method visitFromDocument.

@Override
public Object visitFromDocument(COSDocument doc) throws IOException {
    if (!incrementalUpdate) {
        doWriteHeader(doc);
    } else {
        // Sometimes the original file will be missing a newline at the end
        // In order to avoid having %%EOF the first object on the same line
        // as the %%EOF, we put a newline here. If there's already one at
        // the end of the file, an extra one won't hurt. PDFBOX-1051
        getStandardOutput().writeCRLF();
    }
    doWriteBody(doc);
    // get the previous trailer
    COSDictionary trailer = doc.getTrailer();
    long hybridPrev = -1;
    if (trailer != null) {
        hybridPrev = trailer.getLong(COSName.XREF_STM);
    }
    if (incrementalUpdate || doc.isXRefStream()) {
        doWriteXRefInc(doc, hybridPrev);
    } else {
        doWriteXRefTable();
        doWriteTrailer(doc);
    }
    // write endof
    getStandardOutput().write(STARTXREF);
    getStandardOutput().writeEOL();
    getStandardOutput().write(String.valueOf(getStartxref()).getBytes(Charsets.ISO_8859_1));
    getStandardOutput().writeEOL();
    getStandardOutput().write(EOF);
    getStandardOutput().writeEOL();
    if (incrementalUpdate) {
        if (signatureOffset == 0 || byteRangeOffset == 0) {
            doWriteIncrement();
        } else {
            doWriteSignature();
        }
    }
    return null;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary)

Example 19 with COSDictionary

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

the class COSWriter method doWriteTrailer.

/**
 * This will write the trailer to the PDF document.
 *
 * @param doc The document to create the trailer for.
 *
 * @throws IOException If there is an IOError while writing the document.
 */
protected void doWriteTrailer(COSDocument doc) throws IOException {
    getStandardOutput().write(TRAILER);
    getStandardOutput().writeEOL();
    COSDictionary trailer = doc.getTrailer();
    // sort xref, needed only if object keys not regenerated
    Collections.sort(getXRefEntries());
    COSWriterXRefEntry lastEntry = getXRefEntries().get(getXRefEntries().size() - 1);
    trailer.setLong(COSName.SIZE, lastEntry.getKey().getNumber() + 1);
    // Only need to stay, if an incremental update will be performed
    if (!incrementalUpdate) {
        trailer.removeItem(COSName.PREV);
    }
    if (!doc.isXRefStream()) {
        trailer.removeItem(COSName.XREF_STM);
    }
    // Remove a checksum if present
    trailer.removeItem(COSName.DOC_CHECKSUM);
    COSArray idArray = trailer.getCOSArray(COSName.ID);
    if (idArray != null) {
        idArray.setDirect(true);
    }
    trailer.accept(this);
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray)

Example 20 with COSDictionary

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

the class ContentStreamWriter method writeObject.

private void writeObject(Object o) throws IOException {
    if (o instanceof COSString) {
        COSWriter.writeString((COSString) o, output);
        output.write(SPACE);
    } else if (o instanceof COSFloat) {
        ((COSFloat) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSInteger) {
        ((COSInteger) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSBoolean) {
        ((COSBoolean) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSName) {
        ((COSName) o).writePDF(output);
        output.write(SPACE);
    } else if (o instanceof COSArray) {
        COSArray array = (COSArray) o;
        output.write(COSWriter.ARRAY_OPEN);
        for (int i = 0; i < array.size(); i++) {
            writeObject(array.get(i));
            output.write(SPACE);
        }
        output.write(COSWriter.ARRAY_CLOSE);
    } else if (o instanceof COSDictionary) {
        COSDictionary obj = (COSDictionary) o;
        output.write(COSWriter.DICT_OPEN);
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
            if (entry.getValue() != null) {
                writeObject(entry.getKey());
                output.write(SPACE);
                writeObject(entry.getValue());
                output.write(SPACE);
            }
        }
        output.write(COSWriter.DICT_CLOSE);
        output.write(SPACE);
    } else if (o instanceof Operator) {
        Operator op = (Operator) o;
        if (op.getName().equals(OperatorName.BEGIN_INLINE_IMAGE)) {
            output.write(OperatorName.BEGIN_INLINE_IMAGE.getBytes(Charsets.ISO_8859_1));
            COSDictionary dic = op.getImageParameters();
            for (COSName key : dic.keySet()) {
                Object value = dic.getDictionaryObject(key);
                key.writePDF(output);
                output.write(SPACE);
                writeObject(value);
                output.write(EOL);
            }
            output.write(OperatorName.BEGIN_INLINE_IMAGE_DATA.getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
            output.write(op.getImageData());
            output.write(EOL);
            output.write(OperatorName.END_INLINE_IMAGE.getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
        } else {
            output.write(op.getName().getBytes(Charsets.ISO_8859_1));
            output.write(EOL);
        }
    } else {
        throw new IOException("Error:Unknown type in content stream:" + o);
    }
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) IOException(java.io.IOException) COSBoolean(com.tom_roush.pdfbox.cos.COSBoolean) COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) Map(java.util.Map) COSFloat(com.tom_roush.pdfbox.cos.COSFloat)

Aggregations

COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)221 COSArray (com.tom_roush.pdfbox.cos.COSArray)68 COSBase (com.tom_roush.pdfbox.cos.COSBase)68 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)24 COSName (com.tom_roush.pdfbox.cos.COSName)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)22 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)15 HashMap (java.util.HashMap)14 COSStream (com.tom_roush.pdfbox.cos.COSStream)13 Map (java.util.Map)12 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 COSString (com.tom_roush.pdfbox.cos.COSString)7 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)6 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 List (java.util.List)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Test (org.junit.Test)5