Itextsharp多页PDF格式

本文关键字:格式 PDF 多页 Itextsharp | 更新日期: 2023-09-27 18:10:28

我有一个pdf模板,我想为数据库中的每条记录生成。我发现了这个,但它只适用于物理文件。如何使这只在内存工作?我试图使用内存流,但我无法读取此pdf阅读器

    using iTextSharp.text.pdf;
using System;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string uniqueidstr = Guid.NewGuid().ToString();
        string file = Server.MapPath("~/Reports/PrimaVisita.pdf");
        string MasterPdfFile = Server.MapPath("./MstrCopyFile" + uniqueidstr + ".pdf").ToString();
        PdfCopyFields copy = new PdfCopyFields(new FileStream(MasterPdfFile, FileMode.CreateNew));
        for (int i = 0; i < 10; i++)
        {
            PdfReader tmpReader = new PdfReader(file);
            string tmpfilepath = Server.MapPath("./tmpFile" + uniqueidstr + i.ToString() + ".pdf").ToString();
            FileStream tmpOutFileStream = new FileStream(tmpfilepath, FileMode.CreateNew);
            PdfStamper tmpOutStamper = new PdfStamper(tmpReader, tmpOutFileStream);
            AcroFields fields = tmpOutStamper.AcroFields;
            fields.SetField("Cognome", "pippo" + i.ToString());
            fields.SetField("Nome", "pluto" + i.ToString());
            fields.SetField("DatiNascita", "paperino" + i.ToString());
            fields.SetField("Residenza", "topolino" + i.ToString());
            tmpOutStamper.Writer.CloseStream = false;
            tmpOutStamper.FormFlattening = true;
            tmpOutStamper.Close();
            tmpOutFileStream.Close();
            PdfReader nwPgReader = new PdfReader(tmpfilepath);
            copy.AddDocument(nwPgReader);
        }
        copy.Close();
    }
}

错误是"PDF标头签名未找到"。下面是代码

protected void Button1_Click(object sender, EventArgs e)
    {
        string uniqueidstr = Guid.NewGuid().ToString();
        string file = Server.MapPath("~/Reports/PrimaVisita.pdf");
        MemoryStream masterStream = new MemoryStream();
        //string MasterPdfFile = Server.MapPath("./MstrCopyFile" + uniqueidstr + ".pdf").ToString();
        PdfCopyFields copy = new PdfCopyFields(masterStream);
        for (int i = 0; i < 10; i++)
        {
            PdfReader tmpReader = new PdfReader(file);
            string tmpfilepath = Server.MapPath("./tmpFile" + uniqueidstr + i.ToString() + ".pdf").ToString();
            MemoryStream tmpStream = new MemoryStream();
            //FileStream tmpOutFileStream = new FileStream(tmpfilepath, FileMode.CreateNew);
            PdfStamper tmpOutStamper = new PdfStamper(tmpReader, tmpStream);
            AcroFields fields = tmpOutStamper.AcroFields;
            fields.SetField("Cognome", "pippo" + i.ToString());
            fields.SetField("Nome", "pluto" + i.ToString());
            fields.SetField("DatiNascita", "paperino" + i.ToString());
            fields.SetField("Residenza", "topolino" + i.ToString());
            tmpOutStamper.Writer.CloseStream = false;
            tmpOutStamper.FormFlattening = true;
            tmpOutStamper.Close();
            //tmpOutFileStream.Close();
            PdfReader nwPgReader = new PdfReader(tmpStream);
            copy.AddDocument(nwPgReader);
        }
        copy.Close();
    }

Itextsharp多页PDF格式

我通过将内存流转换为字节数组来解决,现在它工作了。

感谢您的支持

布鲁诺

protected void Button1_Click(object sender, EventArgs e)
    {
        string uniqueidstr = Guid.NewGuid().ToString();
        string file = Server.MapPath("~/Reports/PrimaVisita.pdf");
        MemoryStream masterStream = new MemoryStream();
        PdfCopyFields copy = new PdfCopyFields(masterStream);
        for (int i = 0; i < 10; i++)
        {
            PdfReader tmpReader = new PdfReader(file);
            MemoryStream tmpStream = new MemoryStream();
            tmpStream.Position = 0;
            PdfStamper tmpOutStamper = new PdfStamper(tmpReader, tmpStream);
            AcroFields fields = tmpOutStamper.AcroFields;
            fields.SetField("Cognome", "pippo" + i.ToString());
            fields.SetField("Nome", "pluto" + i.ToString());
            fields.SetField("DatiNascita", "paperino" + i.ToString());
            fields.SetField("Residenza", "topolino" + i.ToString());
            tmpOutStamper.Writer.CloseStream = false;
            tmpOutStamper.FormFlattening = true;
            tmpOutStamper.Close();
            byte[] buf = tmpStream.ToArray();
            PdfReader nwPgReader = new PdfReader(buf);
            copy.AddDocument(nwPgReader);
        }
        copy.Close();

}