可以将它们写入文件然后合并它们但这样做会很糟糕,这也会导致一些问题,所以我想避免这种情况.
但是,我找不到将MemoryStreams与iText5 for .NET合并的方法.
现在,这是我用文件做的方式:
private static voID Concatenatedocuments() { var stream = new MemoryStream(); var readerFrontPage = new pdfReader(Folder + FrontPagename); var readerDocA = new pdfReader(Folder + docA); var readerDocB = new pdfReader(Folder + DocB); var readerAppendix = new pdfReader(Folder + Appendix); var pdfcopyFIElds = new pdfcopyFIElds(stream); pdfcopyFIElds.Adddocument(readerFrontPage); pdfcopyFIElds.Adddocument(readerDocA ); pdfcopyFIElds.Adddocument(readerDocB); pdfcopyFIElds.Adddocument(readerAppendix); pdfcopyFIElds.Close(); Savepdf(stream,filenameReport); }
由于我需要删除文件的使用,因此我保留了MemoryStream,因为不同的部分是从不同的资源构建的.所以我引用了这些内存流.
如何才能做到这一点?
解决方法 在这种情况下,通过将流的位置设置回0,可以修复未找到的错误pdf标题签名.因为您没有收到错误无法访问已关闭的流我假设您已经正确地将pdfWriter的CloseStream设置为false .下面是一个完整的C#2010 WinForm应用程序,目标是iTextSharp 5.1.1.0,它在MemoryStreams中创建三个pdf并将它们组合在一起.由于我没有方便的Web服务器,我正在将它们写入磁盘.
using System;using System.Text;using System.@R_419_5087@.Forms;using System.IO;using iTextSharp.text;using iTextSharp.text.pdf;namespace @R_419_5087@FormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private voID Form1_Load(object sender,EventArgs e) { //Create three MemoryStreams MemoryStream[] streams = { CreateDoc("Page 1"),CreateDoc("Page 2"),CreateDoc("Page 3") }; //I don't have a web server handy so I'm going to write my final MemoryStream to a byte array and then to disk byte[] bytes; //Create our final combined MemoryStream using (MemoryStream finalStream = new MemoryStream()) { //Create our copy object pdfcopyFIElds copy = new pdfcopyFIElds(finalStream); //Loop through each MemoryStream foreach (MemoryStream ms in streams) { //reset the position back to zero ms.position = 0; //Add it to the copy object copy.Adddocument(new pdfReader(ms)); //Clean up ms.dispose(); } //Close the copy object copy.Close(); //Get the raw bytes to save to disk bytes = finalStream.ToArray(); } //Write out the file to the desktop string outputfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"Combined.pdf"); using (fileStream fs = new fileStream(outputfile,fileMode.Create,fileAccess.Write,fileShare.None)) { fs.Write(bytes,bytes.Length); } this.Close(); } /// <summary> /// Helper method to create temporary documents /// </summary> private MemoryStream CreateDoc(string name) { MemoryStream ms = new MemoryStream(); using (document doc = new document(PageSize.LETTER)) { using (pdfWriter writer = pdfWriter.GetInstance(doc,ms)) { writer.CloseStream = false; doc.open(); doc.Add(new Paragraph(name)); doc.Close(); } } return ms; } }}总结
以上是内存溢出为你收集整理的c#-4.0 – 将内存流合并到一个iText文档全部内容,希望文章能够帮你解决c#-4.0 – 将内存流合并到一个iText文档所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)