当前位置: 首页 > 编程语言 > C#

如何使用iTextsharp将文件插入到现有PDF包中包含文件夹的文件夹Share

时间:2023-04-11 01:39:36 C#

如何使用iTextsharp将文件插入到现有PDF包中包含文件夹AnswerinPDF包的文件夹中?,我已经能够使用iTextSharp创建PDF文件夹。但是,使用Adob??eAcrobat我可以创建文件夹,并且可以将文件放在这些文件夹中。如何创建文件夹以及如何使用iTextSharp将文件放入PDF文件夹中的这些文件夹中?我试过使用pdf检查程序来查看有文件夹和没有文件夹的投资组合之间的区别,但我还没有看到任何东西。我想我找错了地方编辑对于我的这个特定用例,实际上可以预先创建包含文件夹的PDF文件夹。因此,能够在现有PDF文件夹中的文件夹中插入文件比实际创建文件夹本身更为重要。这是一些概念验证代码,它创建一个新的包含多个文件夹的组合pdf,并将现有的pdf插入每个文件夹。这种方法基于使用文本编辑器查看使用原始帖子中链接的示例代码创建的pdf文件,然后使用Acrobat创建文件夹并将嵌入文件移动到该文件夹??中,保存pdf,然后使用文本查看编辑器进行更改。在代码中,我正在重新创建我在比较合并后的pdf的两个版本时发现的更改,因此虽然它有效(至少在我的机器上),但它可能不是完成任务的最佳方法。如果你想像我一样看到之前/之后的文件,使用iTextsharp创建组合,然后使用Acrobat打开并创建一个文件夹并将嵌入的文件移动到文件夹中,然后使用工具栏上的保存图标再次保存文件.不要使用文件->另存为...选项将文件另存为PDF包。Acrobat重组文件并压缩或以其他方式将大文件转换为您无法在文本编辑器中读取的二进制数据。我发现删除二进制流数据会使结构更容易理解。只需删除每个流/结束流关键字之间的所有内容。Acrobat在作品集pdf中所做的其中一件事是嵌入一个flash文件,为作品集提供动画效果和更具吸引力的主题。我不知道该怎么做,所以这段代码生成的文件看起来有点简单。使用系统;使用System.IO;使用System.Linq;使用iTextSharp.text;使用iTextSharp.text.pdf;使用iTextSharp.text.pdf.collection;publicclassFolderWriter{privateconststringFolder=@"C:Pathtoyourpdffiles";privateconststringFile1=@"Pdf文件1.pdf";privateconststringFile2=@"Pdf文件2.pdf";privatereadonlystringfile1Path=Path.Combine(Folder,File1);privatereadonlystringfile2Path=Path.Combine(Folder,File2);privatereadonlystring[]keys=new[]{"Type","File"};publicvoidWrite(Streamstream){使用(Documentdocument=newDocument()){PdfWriterwriter=PdfWriter.GetInstance(document,stream);文档.Open();document.Add(newParagraph("本文档包含PDF的集合"));PdfIndirectReferenceparentFolderObjectReference=writer.PdfIndirectReference;PdfIndirectReferencechildFolder1ObjectReference=writer.PdfIndirectReference;PdfIndirectReferencechildFolder2ObjectReference=writer.Pdf间接引用;PdfDictionaryparentFolderObject=GetFolderDictionary(0);parentFolderObject.Put(newPdfName("Child"),childFolder1ObjectReference);parentFolderObject.Put(PdfName.NAME,newPdfString());PdfDictionarychildFolder1Object=GetFolderDictionary(1);childFolder1Object.Put(PdfName.NAME,newPdfString("文件夹1"));childFolder1Object.Put(PdfName.PARENT,parentFolderObjectReference);childFolder1Object.Put(PdfName.NEXT,childFolder2ObjectReference);PdfDictionarychildFolder2Object=GetFolderDictionary(2);childFolder2Object.Put(PdfName.NAME,newPdfString("文件夹2"));childFolder2Object.Put(PdfName.PARENT,parentFolderObjectReference);PdfCollectioncollection=newPdfCollection(PdfCollection.DETAILS);PdfCollectionSchema架构=CollectionSchema();collection.Schema=模式;collection.Sort=newPdfCollectionSort(keys);collection.Put(newPdfName("文件夹"),parentFolderObjectReference);writer.Collection=集合;PDF格式文件规范fs;PdfCollectionItem项目;fs=PdfFileSpecification.FileEmbedded(writer,file1Path,File1,null);item=newPdfCollectionItem(schema);item.AddItem("类型","pdf");fs.AddCollectionItem(项目);//描述显然用于将//文件放在特定文件夹中。//之间的数字用于将文件放入具有匹配id的文件夹中fs.AddDescription(GetDescription(1,File1),false);writer.AddFileAttachment(fs);fs=PdfFileSpecification.FileEmbedded(writer,file2Path,File2,null);item=newPdfCollectionItem(schema);item.AddItem("类型","pdf");fs.AddCollectionItem(项目);fs.AddDescription(GetDescription(2,File2),false);writer.AddFileAttachment(fs);writer.AddToBody(parentFolderObject,parentFolderObjectReference);writer.AddToBody(childFolder1Object,childFolder1ObjectReference);writer.AddToBody(childFolder2Object,childFolder2ObjectReference);文档.Close();}}私有静态字符串gGetDescription(intid,stringfileName){returnstring.Format("{1}",id,fileName);}privatestaticPdfDictionaryGetFolderDictionary(intid){PdfDictionarydic=newPdfDictionary(newPdfName("Folder"));dic.Put(PdfName.CREATIONDATE,newPdfDate(DateTime.Now));dic.Put(PdfName.MODDATE,newPdfDate(DateTime.Now));dic.Put(PdfName.ID,newPdfNumber(id));返回dic;}privatestaticPdfCollectionSchemaCollectionSchema(){PdfCollectionSchemaschema=newPdfCollectionSchema();PdfCollectionFieldtype=newPdfCollectionField("文件类型",PdfCollectionField.TEXT);类型.Order=0;schema.AddField("类型",类型);PdfCollectionField文件名=newPdfCollectionField("文件",PdfCollectionField.FILENAME);文件名.Order=1;schema.AddField("文件",文件名);返回模式;}}iTextsharp无法创建文件夹,但您可以创建和查找PDF书签参见:以上是C#学习教程:HowtouseiTextsharptoinsertfilesintothefolderoftheexistingPDFpackagecontainingthefolder分享的所有内容,如果对大家有用需要详细了解C#学习教程,希望大家多多关注~本文为网络收集,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: