在创建投资组合pdf时,如果任何pdf大小很大或者如果页面中包含大量没有,那么组合pdf就不会被创建。它也不会抛出任何错误。请找到以下代码:
public byte[] createPortFolio(String directoryName) throws DocumentException, IOException {
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("This document contains a collection of PDFs, one per Stanley Kubrick movie."));
PdfIndirectReference parentFolderObjectReference = writer.getPdfIndirectReference();
PdfDictionary parentFolderObject = GetFolderDictionary(folderCount);
parentFolderObject.put(PdfName.NAME, new PdfString("Root"));
PdfCollection collection = new PdfCollection(PdfCollection.CUSTOM);
PdfCollectionSchema schema = getCollectionSchema();
collection.setSchema(schema);
PdfCollectionSort sort = new PdfCollectionSort("File");
sort.setSortOrder(true);
collection.setSort(sort);
collection.put(new PdfName("Folders"), parentFolderObjectReference);
writer.setCollection(collection);
listFilesAndFilesSubDirectories(writer, collection, schema, directoryName, parentFolderObject, parentFolderObjectReference);
document.close();
return baos.toByteArray();
}
public void listFilesAndFilesSubDirectories(PdfWriter writer, PdfCollection collection, PdfCollectionSchema schema, String directoryName, PdfDictionary parentFolderObject, PdfIndirectReference parentIndirectReference) throws DocumentException, IOException {
File directory = new File(directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
String sStatus = "";
PdfIndirectReference siblingFolderObjectReference = null;
PdfDictionary siblingFolderObject = null;
PdfIndirectReference childFolderObjectReference = null;
PdfDictionary childFolderObject = null;
for (File file : fList) {
if (file.isFile()) {
createPdf(writer, schema, file, parentFolderObject);//adding PDF in Portfolio
} else if (file.isDirectory()) {
childFolderObjectReference = writer.getPdfIndirectReference();
folderCount++;
if (siblingFolderObject != null && parentIndirectReference.equals(siblingFolderObject.get(PdfName.PARENT))) {
siblingFolderObject.put(PdfName.NEXT, childFolderObjectReference);
writer.addToBody(siblingFolderObject, siblingFolderObjectReference);
} else {
parentFolderObject.put(new PdfName("Child"), childFolderObjectReference);
}
childFolderObject = GetFolderDictionary(folderCount);
childFolderObject.put(PdfName.NAME, new PdfString(file.getName()));
childFolderObject.put(PdfName.PARENT, parentIndirectReference);
siblingFolderObjectReference = childFolderObjectReference;
siblingFolderObject = childFolderObject;
writer.addToBody(parentFolderObject, parentIndirectReference);
writer.addToBody(childFolderObject, childFolderObjectReference);
listFilesAndFilesSubDirectories(writer, collection, schema, file.getAbsolutePath(), childFolderObject, childFolderObjectReference);
}
}
}
private void createPdf(PdfWriter writer, PdfCollectionSchema schema, File fileObj, PdfDictionary parentFolderObject) {
PdfFileSpecification fs=null;
PdfCollectionItem item;
//Adding first File
try{
fs = PdfFileSpecification.fileEmbedded(writer, fileObj.getPath(), fileObj.getName(), null, true, null, null);
//fs=fileEmbedded(writer, fileObj.getPath(), fileObj.getName(), null, true, null, null);
}
catch(Exception ex)
{
System.out.println("in Exception "+ex.getMessage());
ex.printStackTrace();
}
finally
{
System.out.println("in finally block");
}
item = new PdfCollectionItem(schema);
item.addItem("Type", "pdf");
fs.addCollectionItem(item);
try{
writer.addFileAttachment(fs);
}
catch(Exception ex)
{
System.out.println("in Exception2 "+ex.getMessage());
ex.printStackTrace();
}
finally{
System.out.println("in finally 2");
}
}
在createPdf()函数中,首次尝试块代码后既不运行也不抛出任何异常。