這裏的概念代碼一些證據表明有幾個文件夾中創建一個新的組合的PDF和插入現有的PDF文件到每個那些文件夾。此方法基於使用文本編輯器查看使用原始文章中鏈接的示例代碼創建的pdf文件,然後使用Acrobat創建文件夾並將嵌入文件移動到該文件夾中,保存pdf,然後查看使用文本編輯器進行更改。在代碼中,我重新創建了比較投資組合pdf的兩個版本時發現的更改,因此儘管它起作用(至少在我的機器上),但它可能不是完成任務的最佳方式。
如果您想像我一樣查看前/後文件,請使用iTextsharp創建組合,然後使用Acrobat打開並創建文件夾並將嵌入文件移入文件夾,然後再次保存文件使用工具欄上的保存圖標。不要使用「文件」 - >「另存爲...」選項將文件另存爲投資組合PDF。 Acrobat重新整理文件並進行壓縮,或者以其他方式將大量文件轉換爲二進制數據,而這些數據在文本編輯器中無法讀取。我發現刪除二進制流數據使結構更容易遵循。只要擺脫每一對stream
/endstream
關鍵字之間的所有內容即可。
Acrobat在投資組合pdf中所做的一件事就是嵌入一個Flash文件,爲投資組合提供動畫和更有吸引力的主題。我無法弄清楚如何做到這一點,所以從這段代碼生成的文件看起來很平實。
using System;
using System.IO;
using System.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.collection;
public class FolderWriter {
private const string Folder = @"C:\Path\to\your\pdf\files";
private const string File1 = @"Pdf File 1.pdf";
private const string File2 = @"Pdf File 2.pdf";
private readonly string file1Path = Path.Combine(Folder, File1);
private readonly string file2Path = Path.Combine(Folder, File2);
private readonly string[] keys = new[] {
"Type",
"File"
};
public void Write(Stream stream) {
using (Document document = new Document()) {
PdfWriter writer = PdfWriter.GetInstance(document, stream);
document.Open();
document.Add(new Paragraph("This document contains a collection of PDFs"));
PdfIndirectReference parentFolderObjectReference = writer.PdfIndirectReference;
PdfIndirectReference childFolder1ObjectReference = writer.PdfIndirectReference;
PdfIndirectReference childFolder2ObjectReference = writer.PdfIndirectReference;
PdfDictionary parentFolderObject = GetFolderDictionary(0);
parentFolderObject.Put(new PdfName("Child"), childFolder1ObjectReference);
parentFolderObject.Put(PdfName.NAME, new PdfString());
PdfDictionary childFolder1Object = GetFolderDictionary(1);
childFolder1Object.Put(PdfName.NAME, new PdfString("Folder 1"));
childFolder1Object.Put(PdfName.PARENT, parentFolderObjectReference);
childFolder1Object.Put(PdfName.NEXT, childFolder2ObjectReference);
PdfDictionary childFolder2Object = GetFolderDictionary(2);
childFolder2Object.Put(PdfName.NAME, new PdfString("Folder 2"));
childFolder2Object.Put(PdfName.PARENT, parentFolderObjectReference);
PdfCollection collection = new PdfCollection(PdfCollection.DETAILS);
PdfCollectionSchema schema = CollectionSchema();
collection.Schema = schema;
collection.Sort = new PdfCollectionSort(keys);
collection.Put(new PdfName("Folders"), parentFolderObjectReference);
writer.Collection = collection;
PdfFileSpecification fs;
PdfCollectionItem item;
fs = PdfFileSpecification.FileEmbedded(writer, file1Path, File1, null);
item = new PdfCollectionItem(schema);
item.AddItem("Type", "pdf");
fs.AddCollectionItem(item);
// the description is apparently used to place the
// file in a particular folder. The number between the <and>
// is used to put the file in the folder that has the matching id
fs.AddDescription(GetDescription(1, File1), false);
writer.AddFileAttachment(fs);
fs = PdfFileSpecification.FileEmbedded(writer, file2Path, File2, null);
item = new PdfCollectionItem(schema);
item.AddItem("Type", "pdf");
fs.AddCollectionItem(item);
fs.AddDescription(GetDescription(2, File2), false);
writer.AddFileAttachment(fs);
writer.AddToBody(parentFolderObject, parentFolderObjectReference);
writer.AddToBody(childFolder1Object, childFolder1ObjectReference);
writer.AddToBody(childFolder2Object, childFolder2ObjectReference);
document.Close();
}
}
private static string GetDescription(int id, string fileName) {
return string.Format("<{0}>{1}", id, fileName);
}
private static PdfDictionary GetFolderDictionary(int id) {
PdfDictionary dic = new PdfDictionary(new PdfName("Folder"));
dic.Put(PdfName.CREATIONDATE, new PdfDate(DateTime.Now));
dic.Put(PdfName.MODDATE, new PdfDate(DateTime.Now));
dic.Put(PdfName.ID, new PdfNumber(id));
return dic;
}
private static PdfCollectionSchema CollectionSchema() {
PdfCollectionSchema schema = new PdfCollectionSchema();
PdfCollectionField type = new PdfCollectionField("File type", PdfCollectionField.TEXT);
type.Order = 0;
schema.AddField("Type", type);
PdfCollectionField filename = new PdfCollectionField("File", PdfCollectionField.FILENAME);
filename.Order = 1;
schema.AddField("File", filename);
return schema;
}
}
如果絕對不能做,我也想聽聽。 – dvdvorle
如果包含示例文件,它可能會有所幫助 – yms