PDF修改需要时间在C#

时间:2018-05-31 15:09:15

标签: c# pdf itext

我使用iTextSharp修改pdf文件并在其中添加特定数据。

这是我的场景,我有一个包含数千行的DataTable,每行代表一个客户或客户,我有一个pdf模板。

我必须为每一行(客户或客户)修改pdf模板,以便除了其他数据之外还将其ID添加到文件中,然后将其添加到该客户端的DataTable中。

我使用此代码执行所需的工作,但是在处理大量行时超时或者在我的情况下需要至少15分钟,4k行 - 因为这意味着打开pdf文件4k次并根据需要进行修改。

// file is the pdf tmeplate, id is the customer's id - represens 1 row in the DataTable, landingPage: is client's specific page should be added to the file
private static byte[] GeneratePdfFromPdfFile(byte[] file, int id, string landingPage)
{

    try
    {
        using (var ms = new MemoryStream())
        {
            //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
            var doc = new iTextSharp.text.Document();
            //Create a writer that's bound to our PDF abstraction and our stream
            var writer = PdfWriter.GetInstance(doc, ms);
            //Open the document for writing
            doc.Open();
            PdfContentByte cb = writer.DirectContent;

            ////parse html code to xml 
            //iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
            PdfReader reader = new PdfReader(file);
            for (int pageNumber = 1; pageNumber < reader.NumberOfPages + 1; pageNumber++)
            {
                doc.SetPageSize(reader.GetPageSizeWithRotation(1));
                doc.NewPage();
                //Insert to Destination on the first page
                PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
                int rotation = reader.GetPageRotation(pageNumber);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageNumber).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }

            // Add a new page to the pdf file
            doc.NewPage();
            // set pdf open action to open the link embedded in the file.
            string _embeddedURL = "http://" + landingPage + "/Default.aspx?code=" + id;
            PdfAction act = new PdfAction(_embeddedURL);
            writer.SetOpenAction(act);
            doc.Close();
            return ms.ToArray();
        }
    }
    catch { return null; }
}

注意:我使用ForEach循环遍历DataTable行

1 个答案:

答案 0 :(得分:0)

您的问题(文字)与您的代码(C#部分)不符。您正在问一件事(向现有PDF添加内容),以及做另一件事(向现有PDF添加选项操作),但不管您想要做什么,都不应该使用{{1}制作现有PDF的每个页面的浅表副本(丢弃所有交互性)。

您可以使用PdfWriter

显着减少您编写的代码行数
PdfStamper

请阅读我的图书的chapter 6,了解为什么PdfReader reader = new PdfReader(file); MemoryStream ms = new MemoryStream(); PdfStamper stamper = new PdfStamper(reader, ms); string _embeddedURL = "http://" + landingPage + "/Default.aspx?code=" + id; PdfAction act = new PdfAction(_embeddedURL); stamper.Writer.SetOpenAction(act); stamper.Close(); reader.Close(); return ms.ToArray(); 是错误的课程。 PdfWriter旨在操纵现有的PDF文档,保持其所有现有功能的完整性。

另请注意,您使用的是旧版iText。最新版本为iText 7。有关使用最新iText版本的详细信息,请参阅Jump-Start tutorial