4

使用如何将 PDF 合并到 PDF 包中的答案?,我已经能够使用 iTextSharp 创建 PDF 作品集。但是,使用 Adob​​e Acrobat,我可以创建文件夹,并且可以将文件放入这些文件夹中。

如何创建文件夹以及如何使用 iTextSharp 将文件放入 PDF 组合中的这些文件夹中?

我尝试使用 pdf 检查器程序来查看带有和不带有文件夹的投资组合之间的差异,但我没有看到任何东西。我想我找错地方了

编辑

对于我的这个特定用例,实际上可以在前面创建带有文件夹的 PDF 组合。因此,与实际创建文件夹本身相比,能够在现有 PDF 组合的文件夹中插入文件更为重要。

4

2 回答 2

3

这是一些概念验证代码,它创建一个带有几个文件夹的新投资组合 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;
    }
}
于 2012-11-07T07:07:31.267 回答
1

iTextsharp 无法创建文件夹,但您可以创建和查找 PDF 书签。

看:

于 2012-11-04T14:31:29.073 回答