diff --git a/utils/pdfunite.cc b/utils/pdfunite.cc index 787b1f0..6785cec 100644 --- a/utils/pdfunite.cc +++ b/utils/pdfunite.cc @@ -109,6 +109,85 @@ int main (int argc, char *argv[]) yRef->add(0, 65535, 0, gFalse); PDFDoc::writeHeader(outStr, majorVersion, minorVersion); + // handle OutputIntents + Object intents; + if (docs.size() >= 1) { + Object catObj; + docs[0]->getXRef()->getCatalog(&catObj); + Dict *catDict = catObj.getDict(); + catDict->lookup("OutputIntents", &intents); + if (intents.isArray() && intents.arrayGetLength() > 0) { + for (i = 1; i < (int) docs.size(); i++) { + Object pagecatObj, pageintents; + docs[i]->getXRef()->getCatalog(&pagecatObj); + Dict *pagecatDict = pagecatObj.getDict(); + pagecatDict->lookup("OutputIntents", &pageintents); + if (pageintents.isArray() && pageintents.arrayGetLength() > 0) { + for (j = intents.arrayGetLength() - 1; j >= 0; j--) { + Object intent; + intents.arrayGet(j, &intent, 0); + if (intent.isDict()) { + Object idf; + intent.dictLookup("OutputConditionIdentifier", &idf); + if (idf.isString()) { + GooString *gidf = idf.getString(); + GBool removeIntent = gTrue; + for (int k = 0; k < pageintents.arrayGetLength(); k++) { + Object pgintent; + pageintents.arrayGet(k, &pgintent, 0); + if (pgintent.isDict()) { + Object pgidf; + pgintent.dictLookup("OutputConditionIdentifier", &pgidf); + if (pgidf.isString()) { + GooString *gpgidf = pgidf.getString(); + if (gpgidf->cmp(gidf) == 0) { + pgidf.free(); + removeIntent = gFalse; + break; + } + } + pgidf.free(); + } + } + if (removeIntent) { + intents.arrayRemove(j); + error(errSyntaxWarning, -1, "Output intent {0:s} missing in pdf {1:s}, removed", + gidf->getCString(), docs[i]->getFileName()->getCString()); + } + } else { + intents.arrayRemove(j); + error(errSyntaxWarning, -1, "Invalid output intent dict, missing required OutputConditionIdentifier"); + } + idf.free(); + } else { + intents.arrayRemove(j); + } + intent.free(); + } + } else { + error(errSyntaxWarning, -1, "Output intents differs, remove them all"); + intents.free(); + break; + } + pagecatObj.free(); + pageintents.free(); + } + } + if (intents.isArray() && intents.arrayGetLength() > 0) { + for (j = intents.arrayGetLength() - 1; j >= 0; j--) { + Object intent; + intents.arrayGet(j, &intent, 0); + if (intent.isDict()) { + docs[0]->markPageObjects(intent.getDict(), yRef, countRef, numOffset, 0, 0); + } else { + intents.arrayRemove(j); + } + intent.free(); + } + } + catObj.free(); + } + for (i = 0; i < (int) docs.size(); i++) { for (j = 1; j <= docs[i]->getNumPages(); j++) { PDFRectangle *cropBox = NULL; @@ -145,6 +224,19 @@ int main (int argc, char *argv[]) yRef->add(rootNum, 0, outStr->getPos(), gTrue); outStr->printf("%d 0 obj\n", rootNum); outStr->printf("<< /Type /Catalog /Pages %d 0 R", rootNum + 1); + // insert OutputIntents + if (intents.isArray() && intents.arrayGetLength() > 0) { + outStr->printf(" /OutputIntents ["); + for (j = 0; j < intents.arrayGetLength(); j++) { + Object intent; + intents.arrayGet(j, &intent, 0); + if (intent.isDict()) { + PDFDoc::writeObject(&intent, outStr, yRef, 0, NULL, cryptRC4, 0, 0, 0); + } + } + outStr->printf("]"); + } + intents.free(); outStr->printf(">>\nendobj\n"); objectsCount++;