首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存2份PDF模板,2份文件损坏- iTextSharp

保存2份PDF模板,2份文件损坏- iTextSharp
EN

Stack Overflow用户
提问于 2013-09-25 08:22:54
回答 1查看 290关注 0票数 0

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

问题是,无论日期如何,第二份PDF文件的副本都是腐败的。

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

2013年9月23日至9月25日有数据。二十五号只有三十八个订单,所以这是按计划进行的。在2013年9月24日,有60多个订单,第一页工作,但第二页是腐败。

代码语言:javascript
复制
    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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-25 13:06:46

你为什么要预先分配你的文件?我猜那是你的问题。您将一个PdfStamper绑定到一个PdfReader作为输入,并将同一个pdf的确切副本绑定到一个FileStream对象以供输出。PdfStamper将为您生成输出文件,您不需要帮助它。您正在尝试将新数据添加到现有的文件中,而我不太确定在这种情况下会发生什么(因为我从未见过任何人这样做)。

因此,删除整个File.Copy预分配,并将FileStream声明更改为:

代码语言:javascript
复制
using (FileStream stream = new FileStream(path + pCount.ToString() + fName, FileMode.Create, FileAccess.Write, FileShare.None))

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18999769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档