Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存2份PDF模板副本,第二个文件已损坏-iTextSharp_C#_Pdf_Itextsharp - Fatal编程技术网

C# 保存2份PDF模板副本,第二个文件已损坏-iTextSharp

C# 保存2份PDF模板副本,第二个文件已损坏-iTextSharp,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我有一个PDF模板文件,每页60个标签。我的目标是根据需要复制模板,填写表单数据,然后将文件合并到一个PDF中(或者提供单个文件的链接…要么有效) 问题是,第二份PDF副本无论日期如何都已损坏 工作流是由用户选择日期。当天的午餐订单被收集到一个通用列表中,该列表用于填写模板上的表单字段。在60时,文件保存为临时文件,模板的新副本用于接下来的60个名称,等等 2013年9月23日至2015年9月25日期间有数据。25号的订单只有38份,所以这是按计划进行的。2013年9月24日,订单超过60份,第

我有一个PDF模板文件,每页60个标签。我的目标是根据需要复制模板,填写表单数据,然后将文件合并到一个PDF中(或者提供单个文件的链接…要么有效)

问题是,第二份PDF副本无论日期如何都已损坏

工作流是由用户选择日期。当天的午餐订单被收集到一个通用列表中,该列表用于填写模板上的表单字段。在60时,文件保存为临时文件,模板的新副本用于接下来的60个名称,等等

2013年9月23日至2015年9月25日期间有数据。25号的订单只有38份,所以这是按计划进行的。2013年9月24日,订单超过60份,第一页正常,但第二页已损坏

    private List<string> CreateLabels(DateTime orderDate)
{

    // create file name to save
    string fName = ConvertDateToStringName(orderDate) + ".pdf"; // example 09242013.pdf

    // to hold Temp File Names
    List<string> tempFNames = new List<string>(); 

    // Get path to template/save directory
    string path = Server.MapPath("~/admin/labels/");
    string pdfPath = path + "8195a.pdf"; // template file

    // Get the students and their lunch orders
    List<StudentLabel> labels = DalStudentLabel.GetStudentLabels(orderDate);


    // Get number of template pages needed
    decimal recCount = Convert.ToDecimal(labels.Count);
    decimal pages = Decimal.Divide(recCount, 60);
    int pagesNeeded = Convert.ToInt32(Math.Ceiling(pages));


    // Make the temp names
    for (int c = 0; c < pagesNeeded; c++)
    {
        tempFNames.Add(c.ToString() + fName); //just prepend a digit to the date string
    }

    //Create copies of the empty templates
    foreach (string tName in tempFNames)
    {
        try
        { File.Delete(path + tName); }
        catch { }

        File.Copy(pdfPath, path + tName);
    }

    // we know we need X pages and there is 60 per page
    int x = 0;

    // foreach page needed
    for (int pCount = 0; pCount < pagesNeeded; pCount++)
    {
        // Make a new page
        PdfReader newReader = new PdfReader(pdfPath);

        // pCount.ToString replicates temp names
        using (FileStream stream = new FileStream(path + pCount.ToString() + fName, FileMode.Open))
        {
            PdfStamper stamper = new PdfStamper(newReader, stream); 

            var form = stamper.AcroFields;
            var fieldKeys = form.Fields.Keys;

            StudentLabel lbl = null;

            string lblInfo = "";

            // fill in acro fields with lunch data
            foreach (string fieldKey in fieldKeys)
            {
                try
                {
                    lbl = labels[x];
                }
                catch 
                { 
                    break; 
                } // if we're out of labels, then we're done

                lblInfo = lbl.StudentName + "\n";
                lblInfo += lbl.Teacher + "\n";
                lblInfo += lbl.MenuItem;

                form.SetField(fieldKey, lblInfo);

                x++;

                if (x % 60 == 0)  // reached 60, time for new page
                {
                    break;
                }
            }

            stamper.Writer.CloseStream = false;
            stamper.FormFlattening = true;
            stamper.Close();
            newReader.Close();

            stream.Flush();
            stream.Close();
        }        
    }

    return tempFNames;
}
private List CreateLabels(DateTime orderDate)
{
//创建要保存的文件名
字符串fName=convertDateToString名称(orderDate)+“.pdf”;//示例09242013.pdf
//保存临时文件名的步骤
List tempFNames=新列表();
//获取模板/保存目录的路径
字符串路径=Server.MapPath(“~/admin/labels/”);
字符串pdfPath=path+“8195a.pdf”;//模板文件
//让学生和他们点的午餐
列表标签=DalStudentLabel.GetStudentLabels(orderDate);
//获取所需的模板页面数
decimal recCount=Convert.ToDecimal(labels.Count);
小数页=小数。除(recCount,60);
int pagesneed=Convert.ToInt32(数学上限(页数));
//做临时工的名字
对于(int c=0;c
为什么要预先分配文件?我猜这是你的问题。您正在将一个
PdfStamper
绑定到一个
PdfReader
进行输入,将同一pdf的精确副本绑定到一个
FileStream
对象进行输出。
PdfStamper
将为您生成输出文件,您无需帮助。您试图将新数据附加到现有文件中,我不太确定在这种情况下会发生什么(因为我从未见过有人这样做)

因此,删除整个
文件。复制
预分配,并将
文件流
声明更改为:

using (FileStream stream = new FileStream(path + pCount.ToString() + fName, FileMode.Create, FileAccess.Write, FileShare.None))

显然,您还需要调整返回数组的填充方式。

我不理解您的代码:为什么不允许stamper关闭流?看起来是文件的预分配导致了这个问题。删除File.Copy行以创建模板的副本,并使用建议的“使用(FileStream…”后,它会像一个符咒一样工作。