use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDFMergerUtility method acroFormLegacyMode.
/*
* 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 acroFormLegacyMode(PDFCloneUtility cloner, PDAcroForm destAcroForm, PDAcroForm srcAcroForm) throws IOException {
List<PDField> srcFields = srcAcroForm.getFields();
COSArray destFields;
if (srcFields != null && !srcFields.isEmpty()) {
// if a form is merged multiple times using PDFBox the newly generated
// fields starting with dummyFieldName may already exist. We need to determine the last unique
// number used and increment that.
final String prefix = "dummyFieldName";
final int prefixLength = prefix.length();
for (PDField destField : destAcroForm.getFieldTree()) {
String fieldName = destField.getPartialName();
if (fieldName.startsWith(prefix)) {
nextFieldNum = Math.max(nextFieldNum, Integer.parseInt(fieldName.substring(prefixLength, fieldName.length())) + 1);
}
}
// 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.getFields()) {
COSDictionary dstField = (COSDictionary) cloner.cloneForNewDocument(srcField.getCOSObject());
// to prevent merge conflicts.
if (destAcroForm.getField(srcField.getFullyQualifiedName()) != null) {
dstField.setString(COSName.T, prefix + nextFieldNum++);
}
destFields.add(dstField);
}
destAcroForm.getCOSObject().setItem(COSName.FIELDS, destFields);
}
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDFMergerUtility method mergeFields.
private void mergeFields(PDFCloneUtility cloner, PDField destField, PDField srcField) {
if (destField instanceof PDNonTerminalField && srcField instanceof PDNonTerminalField) {
Log.i("PdfBox-Android", "Skipping non terminal field " + srcField.getFullyQualifiedName());
return;
}
if (destField.getFieldType() == "Tx" && destField.getFieldType() == "Tx") {
// if the field already has multiple widgets we can add to the array
if (destField.getCOSObject().containsKey(COSName.KIDS)) {
COSArray widgets = destField.getCOSObject().getCOSArray(COSName.KIDS);
for (PDAnnotationWidget srcWidget : srcField.getWidgets()) {
try {
widgets.add(cloner.cloneForNewDocument(srcWidget.getCOSObject()));
} catch (IOException ioe) {
Log.w("PdfBox-Android", "Unable to clone widget for source field " + srcField.getFullyQualifiedName());
}
}
} else {
COSArray widgets = new COSArray();
try {
COSDictionary widgetAsCOS = (COSDictionary) cloner.cloneForNewDocument(destField.getWidgets().get(0));
cleanupWidgetCOSDictionary(widgetAsCOS, true);
widgetAsCOS.setItem(COSName.PARENT, destField);
widgets.add(widgetAsCOS);
for (PDAnnotationWidget srcWidget : srcField.getWidgets()) {
try {
widgetAsCOS = (COSDictionary) cloner.cloneForNewDocument(srcWidget.getCOSObject());
cleanupWidgetCOSDictionary(widgetAsCOS, false);
widgetAsCOS.setItem(COSName.PARENT, destField);
widgets.add(widgetAsCOS);
} catch (IOException ioe) {
Log.w("PdfBox-Android", "Unable to clone widget for source field " + srcField.getFullyQualifiedName());
}
}
destField.getCOSObject().setItem(COSName.KIDS, widgets);
cleanupFieldCOSDictionary(destField.getCOSObject());
} catch (IOException ioe) {
Log.w("PdfBox-Android", "Unable to clone widget for destination field " + destField.getFullyQualifiedName());
}
}
} else {
Log.i("PdfBox-Android", "Only merging two text fields is currently supported");
Log.i("PdfBox-Android", "Skipping merging of " + srcField.getFullyQualifiedName() + " into " + destField.getFullyQualifiedName());
}
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class BaseParser method parseCOSDictionary.
/**
* This will parse a PDF dictionary.
*
* @return The parsed dictionary, never null.
*
* @throws IOException If there is an error reading the stream.
*/
protected COSDictionary parseCOSDictionary() throws IOException {
readExpectedChar('<');
readExpectedChar('<');
skipSpaces();
COSDictionary obj = new COSDictionary();
boolean done = false;
while (!done) {
skipSpaces();
char c = (char) seqSource.peek();
if (c == '>') {
done = true;
} else if (c == '/') {
// stop immediately and return everything read so far
if (!parseCOSDictionaryNameValuePair(obj)) {
return obj;
}
} else {
// invalid dictionary, we were expecting a /Name, read until the end or until we can recover
Log.w("PdfBox-Android", "Invalid dictionary, found: '" + c + "' but expected: '/' at offset " + seqSource.getPosition());
if (readUntilEndOfCOSDictionary()) {
// we couldn't recover
return obj;
}
}
}
readExpectedChar('>');
readExpectedChar('>');
return obj;
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class Overlay method processPages.
private void processPages(PDDocument document) throws IOException {
int pageCounter = 0;
for (PDPage page : document.getPages()) {
pageCounter++;
COSDictionary pageDictionary = page.getCOSObject();
COSBase originalContent = pageDictionary.getDictionaryObject(COSName.CONTENTS);
COSArray newContentArray = new COSArray();
LayoutPage layoutPage = getLayoutPage(pageCounter, document.getNumberOfPages());
if (layoutPage == null) {
continue;
}
switch(position) {
case FOREGROUND:
// save state
newContentArray.add(createStream("q\n"));
addOriginalContent(originalContent, newContentArray);
// restore state
newContentArray.add(createStream("Q\n"));
// overlay content last
overlayPage(page, layoutPage, newContentArray);
break;
case BACKGROUND:
// overlay content first
overlayPage(page, layoutPage, newContentArray);
addOriginalContent(originalContent, newContentArray);
break;
default:
throw new IOException("Unknown type of position:" + position);
}
pageDictionary.setItem(COSName.CONTENTS, newContentArray);
}
}
use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.
the class PDFCloneUtility method cloneMerge.
/**
* Merges two objects of the same type by deep-cloning its members.
* <br>
* Base and target must be instances of the same class.
* @param base the base object to be cloned
* @param target the merge target
* @throws IOException if an I/O error occurs
*/
public void cloneMerge(final COSObjectable base, COSObjectable target) throws IOException {
if (base == null) {
return;
}
COSBase retval = clonedVersion.get(base);
if (retval != null) {
return;
// we are done, it has already been converted. // ### Is that correct for cloneMerge???
}
// TODO what when clone-merging a clone? Does it ever happen?
if (!(base instanceof COSBase)) {
cloneMerge(base.getCOSObject(), target.getCOSObject());
} else if (base instanceof COSObject) {
if (target instanceof COSObject) {
cloneMerge(((COSObject) base).getObject(), ((COSObject) target).getObject());
} else if (target instanceof COSDictionary || target instanceof COSArray) {
cloneMerge(((COSObject) base).getObject(), target);
}
} else if (base instanceof COSArray) {
COSArray array = (COSArray) base;
for (int i = 0; i < array.size(); i++) {
((COSArray) target).add(cloneForNewDocument(array.get(i)));
}
} else if (base instanceof COSStream) {
// does that make sense???
COSStream originalStream = (COSStream) base;
COSStream stream = destination.getDocument().createCOSStream();
OutputStream output = stream.createOutputStream(originalStream.getFilters());
IOUtils.copy(originalStream.createInputStream(), output);
output.close();
clonedVersion.put(base, stream);
for (Map.Entry<COSName, COSBase> entry : originalStream.entrySet()) {
stream.setItem(entry.getKey(), cloneForNewDocument(entry.getValue()));
}
retval = stream;
} else if (base instanceof COSDictionary) {
COSDictionary dic = (COSDictionary) base;
clonedVersion.put(base, retval);
for (Map.Entry<COSName, COSBase> entry : dic.entrySet()) {
COSName key = entry.getKey();
COSBase value = entry.getValue();
if (((COSDictionary) target).getItem(key) != null) {
cloneMerge(value, ((COSDictionary) target).getItem(key));
} else {
((COSDictionary) target).setItem(key, cloneForNewDocument(value));
}
}
} else {
retval = (COSBase) base;
}
clonedVersion.put(base, retval);
clonedValues.add(retval);
}
Aggregations