gpt4 book ai didi

c#-4.0 - 将内存流合并到一个 iText 文档

转载 作者:行者123 更新时间:2023-12-02 21:07:43 26 4
gpt4 key购买 nike

我有四个 MemoryStreams 数据,我想要合并它们,然后打开 pdfDocument,而不创建单个文件。

可以将它们写到文件中然后合并它们,但这将是不好的做法,并且还会导致一些问题,所以我想避免这种情况。

但是,我找不到将 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 header 签名。由于您没有收到错误无法访问关闭的流,我假设您已经将PdfWriterCloseStream正确设置为

下面是一个针对 iTextSharp 5.1.1.0 的完整工作 C# 2010 WinForm 应用程序,它在 MemoryStreams 中创建三个 PDF 并将它们组合起来。由于我手边没有网络服务器,所以我将它们写入磁盘。

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
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, 0, 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 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8385690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com