如何将文件插入现有的PDF组合的文件夹使用iTextSharp的文件夹 [英] How to insert files into folders of an existing PDF portfolio with folders using iTextsharp

查看:225
本文介绍了如何将文件插入现有的PDF组合的文件夹使用iTextSharp的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用合并PDF使用答案iTextSharp的;成PDF包?,我已经能够创建使用iTextSharp的一个PDF包。然而,使用Adobe Acrobat我能够创建文件夹,我能够把文件中的这些文件夹中。

Using the answers in How to merge PDF using itextsharp; into a PDF Portfolio?, I've been able to create an PDF portfolio using iTextSharp. However, using Adobe Acrobat I'm able to create folders, and I'm able to put files in those folders.

如何创建文件夹,我如何把在使用iTextSharp的PDF包这些文件夹中的文件?

How do I create folders and how do I put files in those folders in a PDF portfolio using iTextSharp?

我已经使用PDF检查程序,看看有和没有文件夹的投资组合之间的差异试过,但我避风港'T能看到任何。我想我找错了地方。

I've tried using a pdf inspector program to see the differences between a portfolio with and without folders, but I haven't been able to see any. I guess I'm looking in the wrong places

修改

有关此我的具体使用情况下,它实际上是可以用文件夹前面创建PDF包。所以它的方式更重要的是能够插入现有的PDF包文件夹中的文件,而不是实际创建文件夹本身。

For this specific use case of mine it is actually possible to create the PDF portfolio with folder up front. So it's way more important to be able to insert files in folders in an existing PDF portfolio as opposed to actually creating the folders themselves.

推荐答案

下面是概念代码一些证据创建具有几个文件夹的新组合的PDF和插入现有的PDF文件到每个这些文件夹中。这种方法是基于使用文本编辑器来看看使用在原岗位链接到示例代码创建一个PDF文件,然后使用Acrobat创建一个文件夹,移动嵌入的文件到该文件夹​​,保存PDF,然后看着更改使用文本编辑器。在代码中,我重新在比较投资组合PDF的两个版本,因此,虽然它的工作原理(至少在我的机器上),它可能无法完成任务的最佳途径发现的变化。

Here is some proof of concept code that creates a new portfolio pdf with couple of folders and inserts an existing pdf file into each of those folders. This approach is based on using a text editor to look at a pdf file created using the sample code linked to in the original post, then using Acrobat to create a folder and move the embedded file into that folder, saving the pdf, then looking at the changes with a text editor. In the code I'm recreating the changes found while comparing the two versions of the portfolio pdf, so while it works (at least on my machine), it may not be the best way to accomplish the task.

如果你想之前查看/文件后,像我一样,创建一个使用iTextSharp的投资组合,然后用Acrobat打开并新建一个文件夹,移动嵌入的文件(S )进入该文件夹,然后只保存文件再次使用工具栏上的保存图标。不要使用文件 - >另存为...选项,将文件保存为PDF格式组合。 Acrobat中重新组织文件和压缩或以其他方式转换了很多文件,二进制数据,你不会是在文本编辑器能够读取。我发现,删除二进制流数据进行的结构更容易跟踪。刚刚摆脱一切的每对 / endstream 关键字之间。

If you want to view the before/after files like I did, create the portfolio using iTextsharp, then open with Acrobat and create a folder and move the embedded file(s) into the folder, then just save the file again using the save icon on the toolbar. Do not use the File -> Save As... option to save the file as a Portfolio PDF. Acrobat reorganizes the file and compresses or in some other way converts a lot of the file to binary data that you won't be able read in a text editor. I found that deleting the binary stream data made the structure much easier to follow. Just get rid of everything between each pair of stream/endstream keywords.

一件事,Acrobat无法在投资组合中的PDF文件中嵌入一个Flash文件,它提供的动画和投资组合更具吸引力的主题。我无法弄清楚如何做到这一点,所以从这个代码生成的文件是有点平淡寻找。

One thing that Acrobat does in portfolio pdfs is embed a flash file that provides animation and a more attractive theme for the portfolio. I wasn't able to figure out how to do that, so the resulting file from this code is a bit plain looking.

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;
    }
}

这篇关于如何将文件插入现有的PDF组合的文件夹使用iTextSharp的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆