1. Minor improvement to exception handling
2. Changes to adding a file to the document catalog if the AF item is
not directly a COSArray.
This commit is contained in:
Ivan Vaklinov
2019-08-06 14:26:34 +03:00
parent 2cf5af1415
commit a264538d85

View File

@@ -33,6 +33,7 @@ import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSDictionary; import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.pdmodel.*; import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.common.PDMetadata; import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification; import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
@@ -478,17 +479,27 @@ public class ZUGFeRDExporter implements Closeable {
doc.getDocumentCatalog().setNames(names); doc.getDocumentCatalog().setNames(names);
// AF entry (Array) in catalog with the FileSpec // AF entry (Array) in catalog with the FileSpec
COSArray cosArray = (COSArray) doc.getDocumentCatalog().getCOSObject().getItem("AF"); COSBase AFEntry = (COSBase)doc.getDocumentCatalog().getCOSObject().getItem("AF");
if (cosArray == null) { if ((AFEntry == null))
cosArray = new COSArray(); {
} COSArray cosArray = new COSArray();
cosArray.add(fs); cosArray.add(fs);
COSDictionary dict2 = doc.getDocumentCatalog().getCOSObject(); doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);
COSArray array = new COSArray(); } else if (AFEntry instanceof COSArray)
array.add(fs.getCOSObject()); // see below {
dict2.setItem("AF", array); COSArray cosArray = (COSArray)AFEntry;
doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray); cosArray.add(fs);
} doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray);
} else if ((AFEntry instanceof COSObject) &&
((COSObject)AFEntry).getObject() instanceof COSArray)
{
COSArray cosArray = (COSArray)((COSObject)AFEntry).getObject();
cosArray.add(fs);
} else
{
throw new IOException("Unexpected object type for PDFDocument/Catalog/COSDictionary/Item(AF)");
}
}
/** /**
* Sets the ZUGFeRD XML data to be attached as a single byte array. This is * Sets the ZUGFeRD XML data to be attached as a single byte array. This is
@@ -575,12 +586,12 @@ public class ZUGFeRDExporter implements Closeable {
buffer.write(prefix.getBytes("UTF-8")); // see https://github.com/ZUGFeRD/mustangproject/issues/44 buffer.write(prefix.getBytes("UTF-8")); // see https://github.com/ZUGFeRD/mustangproject/issues/44
serializer.serialize(xmpMetadata, buffer, false); serializer.serialize(xmpMetadata, buffer, false);
buffer.write(suffix.getBytes("UTF-8")); buffer.write(suffix.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e)
// TODO Auto-generated catch block {
e.printStackTrace(); throw new TransformerException(e);
} catch (IOException e) { } catch (IOException e)
// TODO Auto-generated catch block {
e.printStackTrace(); throw new TransformerException(e);
} }
return buffer.toByteArray(); return buffer.toByteArray();
} }