Search in sources :

Example 16 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class COSUtils method getAsString.

/**
 * Return the COSBase object as String if the COSBase object is an instance of COSString or
 * COSName or a reference to it.
 *
 * @param cbase the object to get.
 * @param cDoc the document.
 * @return the object as String if the object is a COSString or COSName or reference to it.
 * Returns null otherwise.
 */
public static String getAsString(COSBase cbase, COSDocument cDoc) {
    if (cbase instanceof COSObject) {
        COSObjectKey key = new COSObjectKey((COSObject) cbase);
        COSObject obj = cDoc.getObjectFromPool(key);
        if (obj != null && obj.getObject() instanceof COSString) {
            return ((COSString) obj.getObject()).getString();
        } else if (obj != null && obj.getObject() instanceof COSName) {
            return ((COSName) obj.getObject()).getName();
        } else {
            return null;
        }
    } else if (cbase instanceof COSString) {
        return ((COSString) cbase).getString();
    } else if (cbase instanceof COSName) {
        return ((COSName) cbase).getName();
    } else {
        return null;
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSName(org.apache.pdfbox.cos.COSName) COSObject(org.apache.pdfbox.cos.COSObject) COSString(org.apache.pdfbox.cos.COSString)

Example 17 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class TrailerValidationProcess method checkTrailersForLinearizedPDF15.

/**
 * Accesses and compares First and Last trailers for a PDF version higher than 1.4.
 *
 * @param ctx the preflight context.
 */
protected void checkTrailersForLinearizedPDF15(PreflightContext ctx) {
    PDDocument pdfDoc = ctx.getDocument();
    COSDocument cosDocument = pdfDoc.getDocument();
    List<COSObject> xrefs = cosDocument.getObjectsByType(COSName.XREF);
    if (xrefs.isEmpty()) {
        // no XRef CosObject, may by this pdf file used the PDF 1.4 syntaxe
        checkTrailersForLinearizedPDF14(ctx);
    } else {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        COSDictionary firstTrailer = null;
        COSDictionary lastTrailer = null;
        // Search First and Last trailers according to offset position.
        for (COSObject co : xrefs) {
            long offset = cosDocument.getXrefTable().get(new COSObjectKey(co));
            if (offset < min) {
                min = offset;
                firstTrailer = (COSDictionary) co.getObject();
            }
            if (offset > max) {
                max = offset;
                lastTrailer = (COSDictionary) co.getObject();
            }
        }
        checkMainTrailer(ctx, firstTrailer);
        if (!compareIds(firstTrailer, lastTrailer, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_ID_CONSISTENCY, "ID is different in the first and the last trailer"));
        }
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSObject(org.apache.pdfbox.cos.COSObject) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 18 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class TestCOSUtils method testIsStream.

@Test
public void testIsStream() {
    try {
        COSObject co = new COSObject(new COSStream());
        co.setGenerationNumber(0);
        co.setObjectNumber(10);
        assertFalse(COSUtils.isStream(co, new IOCOSDocument()));
        COSDocument doc = new COSDocument();
        addToXref(doc, new COSObjectKey(co), 1000);
        COSUtils.isStream(co, doc);
        doc.close();
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) COSObject(org.apache.pdfbox.cos.COSObject) COSDocument(org.apache.pdfbox.cos.COSDocument) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class TestCOSUtils method testIsString.

@Test
public void testIsString() {
    try {
        COSObject co = new COSObject(new COSString(""));
        co.setGenerationNumber(0);
        co.setObjectNumber(10);
        assertFalse(COSUtils.isString(co, new IOCOSDocument()));
        COSDocument doc = new COSDocument();
        addToXref(doc, new COSObjectKey(co), 1000);
        COSUtils.isString(co, doc);
        doc.close();
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSObject(org.apache.pdfbox.cos.COSObject) COSDocument(org.apache.pdfbox.cos.COSDocument) IOException(java.io.IOException) COSString(org.apache.pdfbox.cos.COSString) Test(org.junit.Test)

Example 20 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class DecompressObjectstreams method main.

/**
 * This is a very simple program, so everything is in the main method.
 * @param args arguments to the program
 */
public static void main(String[] args) {
    // suppress the Dock icon on OS X
    System.setProperty("apple.awt.UIElement", "true");
    if (args.length < 1) {
        usage();
    }
    String inputFilename = args[0];
    String outputFilename;
    if (args.length > 1) {
        outputFilename = args[1];
    } else {
        if (inputFilename.matches(".*\\.[pP][dD][fF]$")) {
            outputFilename = inputFilename.replaceAll("\\.[pP][dD][fF]$", ".unc.pdf");
        } else {
            outputFilename = inputFilename + ".unc.pdf";
        }
    }
    PDDocument doc = null;
    try {
        doc = PDDocument.load(new File(inputFilename));
        for (COSObject objStream : doc.getDocument().getObjectsByType(COSName.OBJ_STM)) {
            COSStream stream = (COSStream) objStream.getObject();
            PDFObjectStreamParser sp = new PDFObjectStreamParser(stream, doc.getDocument());
            sp.parse();
            for (COSObject next : sp.getObjects()) {
                COSObjectKey key = new COSObjectKey(next);
                COSObject obj = doc.getDocument().getObjectFromPool(key);
                obj.setObject(next.getObject());
            }
            doc.getDocument().removeObject(new COSObjectKey(objStream));
        }
        doc.save(outputFilename);
    } catch (Exception e) {
        System.err.println("Error processing file: " + e.getMessage());
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) COSObject(org.apache.pdfbox.cos.COSObject) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) File(java.io.File) PDFObjectStreamParser(org.apache.pdfbox.pdfparser.PDFObjectStreamParser)

Aggregations

COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)39 COSObject (org.apache.pdfbox.cos.COSObject)25 IOException (java.io.IOException)16 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSBase (org.apache.pdfbox.cos.COSBase)12 COSDictionary (org.apache.pdfbox.cos.COSDictionary)8 COSStream (org.apache.pdfbox.cos.COSStream)7 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)7 COSArray (org.apache.pdfbox.cos.COSArray)6 COSString (org.apache.pdfbox.cos.COSString)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 COSNumber (org.apache.pdfbox.cos.COSNumber)4 COSInteger (org.apache.pdfbox.cos.COSInteger)3 COSName (org.apache.pdfbox.cos.COSName)3 InputStream (java.io.InputStream)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 PDFObjectStreamParser (org.apache.pdfbox.pdfparser.PDFObjectStreamParser)2