同一页面上的多个模板

Multiple Templates on same page

我正在为同一个客户创建各种各样的可变数据报表 (90+)。我们每个工作日每份报表打印 10K 左右。有些声明在页面的左上角有一个特定的标志,有些则故意省略。有些陈述在底部附近有图像 and/or 文本,有些则有意没有。这些以及许多其他的每个语句 added/omitted。

我不确定是否可行,但是否可以 'layer' PDF 相互叠加?如果是这样,我想我可以根据每个语句的需要添加 'layers' 。然后生成的 'layered' PDF 将成为我的基础文档,然后我可以继续向其中添加数据。

否则我想我需要为每个可能的场景创建一个 PDF。

(顺便说一句,我曾尝试有条件地将这些项目作为图像直接添加,但它会减慢速度。)

编辑 我尝试使用此代码,但最终输出仅显示最后添加的模板。

string[] templates = new string[]{
            pdfTemplate
            ,thingsTemplate
        };

        using (FileStream filestream = new FileStream(finalTemplate, FileMode.OpenOrCreate, FileAccess.Write))
        using (iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER))
        using (PdfWriter writer = PdfWriter.GetInstance(document, filestream))
        {
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page;
            for (int i = 0; i < templates.Length; i++)
            {
                page = writer.GetImportedPage(new PdfReader(templates[i]), 1);
                cb.AddTemplate(page, 1f, 0, 0, 1, 0, 0);
            }
            document.Close();
        } 

是的,可以将不同的 PDF 层叠在一起。

看看Layers example. We have a file with 4 pages: layers_orig.pdf, then we add the 4 pages to a single page: layers.pdf

好吧,不完全是你添加它们的方式,但你所描述的被称为superimposing and the Stationery示例或多或少地做了你所要求的。

我们创建一个 PDF,但在创建它的同时,我们添加了一些公司信纸。这是信纸:stationery.pdf. This is the final result: text_on_stationery.pdf.

您可以在此过程中添加任意数量的信纸模板。