Search in sources :

Example 11 with COSDictionary

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

the class PDFMergerUtility method acroFormJoinFieldsMode.

/*
     * Merge the contents of the source form into the destination form for the
     * destination file.
     *
     * @param cloner the object cloner for the destination document
     * @param destAcroForm the destination form
     * @param srcAcroForm the source form
     * @throws IOException If an error occurs while adding the field.
     */
private void acroFormJoinFieldsMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException {
    List<PDField> srcFields = srcAcroForm.getFields();
    COSArray destFields;
    if (srcFields != null && !srcFields.isEmpty()) {
        // get the destinations root fields. Could be that the entry doesn't exist
        // or is of wrong type
        COSBase base = destAcroForm.getCOSObject().getItem(COSName.FIELDS);
        if (base instanceof COSArray) {
            destFields = (COSArray) base;
        } else {
            destFields = new COSArray();
        }
        for (PDField srcField : srcAcroForm.getFieldTree()) {
            // if the form already has a field with this name then we need to rename this field
            // to prevent merge conflicts.
            PDField destinationField = destAcroForm.getField(srcField.getFullyQualifiedName());
            if (destinationField == null) {
                // field doesn't exist - can safely add it
                COSDictionary importedField = (COSDictionary) cloner.cloneForNewDocument(srcField.getCOSObject());
                destFields.add(importedField);
            } else {
                mergeFields(cloner, destinationField, srcField);
            }
        }
        destAcroForm.getCOSObject().setItem(COSName.FIELDS, destFields);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 12 with COSDictionary

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

the class PDFMergerUtility method mergeOutputIntents.

// copy outputIntents to destination, but avoid duplicate OutputConditionIdentifier,
// except when it is missing or is named "Custom".
private void mergeOutputIntents(PDFCloneUtility cloner, PDDocumentCatalog srcCatalog, PDDocumentCatalog destCatalog) throws IOException {
    List<PDOutputIntent> srcOutputIntents = srcCatalog.getOutputIntents();
    List<PDOutputIntent> dstOutputIntents = destCatalog.getOutputIntents();
    for (PDOutputIntent srcOI : srcOutputIntents) {
        String srcOCI = srcOI.getOutputConditionIdentifier();
        if (srcOCI != null && !"Custom".equals(srcOCI)) {
            // is that identifier already there?
            boolean skip = false;
            for (PDOutputIntent dstOI : dstOutputIntents) {
                if (dstOI.getOutputConditionIdentifier().equals(srcOCI)) {
                    skip = true;
                    break;
                }
            }
            if (skip) {
                continue;
            }
        }
        destCatalog.addOutputIntent(new PDOutputIntent((COSDictionary) cloner.cloneForNewDocument(srcOI)));
        dstOutputIntents.add(srcOI);
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDOutputIntent(com.tom_roush.pdfbox.pdmodel.graphics.color.PDOutputIntent)

Example 13 with COSDictionary

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

the class PDFMergerUtility method optimizedMergeDocuments.

private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException {
    PDDocument destination = null;
    try {
        destination = new PDDocument(memUsageSetting);
        PDFCloneUtility cloner = new PDFCloneUtility(destination);
        for (Object sourceObject : sources) {
            PDDocument sourceDoc = null;
            try {
                if (sourceObject instanceof File) {
                    sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting);
                } else {
                    sourceDoc = PDDocument.load((InputStream) sourceObject, memUsageSetting);
                }
                for (PDPage page : sourceDoc.getPages()) {
                    PDPage newPage = new PDPage((COSDictionary) cloner.cloneForNewDocument(page.getCOSObject()));
                    newPage.setCropBox(page.getCropBox());
                    newPage.setMediaBox(page.getMediaBox());
                    newPage.setRotation(page.getRotation());
                    PDResources resources = page.getResources();
                    if (resources != null) {
                        // this is smart enough to just create references for resources that are used on multiple pages
                        newPage.setResources(new PDResources((COSDictionary) cloner.cloneForNewDocument(resources)));
                    } else {
                        newPage.setResources(new PDResources());
                    }
                    destination.addPage(newPage);
                }
            } finally {
                IOUtils.closeQuietly(sourceDoc);
            }
        }
        if (destinationStream == null) {
            destination.save(destinationFileName);
        } else {
            destination.save(destinationStream);
        }
    } finally {
        IOUtils.closeQuietly(destination);
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) COSObject(com.tom_roush.pdfbox.cos.COSObject) File(java.io.File)

Example 14 with COSDictionary

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

the class PDFMergerUtility method mergeViewerPreferences.

private void mergeViewerPreferences(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) {
    PDViewerPreferences srcViewerPreferences = srcCatalog.getViewerPreferences();
    if (srcViewerPreferences == null) {
        return;
    }
    PDViewerPreferences destViewerPreferences = destCatalog.getViewerPreferences();
    if (destViewerPreferences == null) {
        destViewerPreferences = new PDViewerPreferences(new COSDictionary());
        destCatalog.setViewerPreferences(destViewerPreferences);
    }
    mergeInto(srcViewerPreferences.getCOSObject(), destViewerPreferences.getCOSObject(), Collections.<COSName>emptySet());
    // check the booleans - set to true if one is set and true
    if (srcViewerPreferences.hideToolbar() || destViewerPreferences.hideToolbar()) {
        destViewerPreferences.setHideToolbar(true);
    }
    if (srcViewerPreferences.hideMenubar() || destViewerPreferences.hideMenubar()) {
        destViewerPreferences.setHideMenubar(true);
    }
    if (srcViewerPreferences.hideWindowUI() || destViewerPreferences.hideWindowUI()) {
        destViewerPreferences.setHideWindowUI(true);
    }
    if (srcViewerPreferences.fitWindow() || destViewerPreferences.fitWindow()) {
        destViewerPreferences.setFitWindow(true);
    }
    if (srcViewerPreferences.centerWindow() || destViewerPreferences.centerWindow()) {
        destViewerPreferences.setCenterWindow(true);
    }
    if (srcViewerPreferences.displayDocTitle() || destViewerPreferences.displayDocTitle()) {
        destViewerPreferences.setDisplayDocTitle(true);
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDViewerPreferences(com.tom_roush.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences)

Example 15 with COSDictionary

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

the class XrefTrailerResolver method setStartxref.

/**
 * Sets the byte position of the first XRef
 * (has to be called after very last startxref was read).
 * This is used to resolve chain of active XRef/trailer.
 *
 * In case startxref position is not found we output a
 * warning and use all XRef/trailer objects combined
 * in byte position order.
 * Thus for incomplete PDF documents with missing
 * startxref one could call this method with parameter value -1.
 *
 * @param startxrefBytePosValue starting position of the first XRef
 */
public void setStartxref(long startxrefBytePosValue) {
    if (resolvedXrefTrailer != null) {
        Log.w("PdfBox-Android", "Method must be called only ones with last startxref value.");
        return;
    }
    resolvedXrefTrailer = new XrefTrailerObj();
    resolvedXrefTrailer.trailer = new COSDictionary();
    XrefTrailerObj curObj = bytePosToXrefMap.get(startxrefBytePosValue);
    List<Long> xrefSeqBytePos = new ArrayList<Long>();
    if (curObj == null) {
        // no XRef at given position
        Log.w("PdfBox-Android", "Did not found XRef object at specified startxref position " + startxrefBytePosValue);
        // use all objects in byte position order (last entries overwrite previous ones)
        xrefSeqBytePos.addAll(bytePosToXrefMap.keySet());
        Collections.sort(xrefSeqBytePos);
    } else {
        // copy xref type
        resolvedXrefTrailer.xrefType = curObj.xrefType;
        // found starting Xref object
        // add this and follow chain defined by 'Prev' keys
        xrefSeqBytePos.add(startxrefBytePosValue);
        while (curObj.trailer != null) {
            long prevBytePos = curObj.trailer.getLong(COSName.PREV, -1L);
            if (prevBytePos == -1) {
                break;
            }
            curObj = bytePosToXrefMap.get(prevBytePos);
            if (curObj == null) {
                Log.w("PdfBox-Android", "Did not found XRef object pointed to by 'Prev' key at position " + prevBytePos);
                break;
            }
            xrefSeqBytePos.add(prevBytePos);
            // sanity check to prevent infinite loops
            if (xrefSeqBytePos.size() >= bytePosToXrefMap.size()) {
                break;
            }
        }
        // have to reverse order so that later XRefs will overwrite previous ones
        Collections.reverse(xrefSeqBytePos);
    }
    // merge used and sorted XRef/trailer
    for (Long bPos : xrefSeqBytePos) {
        curObj = bytePosToXrefMap.get(bPos);
        if (curObj.trailer != null) {
            resolvedXrefTrailer.trailer.addAll(curObj.trailer);
        }
        resolvedXrefTrailer.xrefTable.putAll(curObj.xrefTable);
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) ArrayList(java.util.ArrayList)

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