itextsharp multipage pdf格式

时间:2013-04-22 08:39:28

标签: c# itextsharp

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

读取它
    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();
    }

1 个答案:

答案 0 :(得分:1)

我通过在字节数组中转换内存流来解决,现在它可以正常工作。

坦克为你的支持

布鲁诺

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();


}