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;
}
}
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"));
}
}
}
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());
}
}
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());
}
}
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) {
}
}
}
}
Aggregations