【问题标题】:Pdf Merge/Overlap with iTextPdf 与 iText 合并/重叠
【发布时间】:2012-04-23 22:02:13
【问题描述】:

我已经将 iText 用于一些不同的实用程序,例如我们成功地合并和编辑 pdf 文件。现在我需要重叠 2 个 pdf 页面:

例如: 输入: PDF#1(1 页) PDF#2(1 页)

输出: PDF#3(1 页:这是 2 个输入页重叠的结果)

我不知道 iText 最新版本是否可以做到这一点。我也在考虑使用 2 个输入 PDF 文件之一作为 PDF 输出文件的背景。

提前谢谢你。

【问题讨论】:

    标签: c# .net pdf itextsharp itext


    【解决方案1】:

    这实际上很容易做到。 PdfWriter 对象有一个名为 GetImportedPage() 的实例方法,它返回一个 PdfImportedPage 对象。此对象可以传递给PdfContentByteAddTemplate() 方法。

    GetImportedPage() 接受 PdfReader 对象和您想要获取的页码。您可以从PdfWriterDirectContent 属性的实例中获取PdfContentByte

    下面的代码是一个完整的工作 C# 2010 WinForms 应用程序,目标是 iTextSharp 5.1.2.0,它显示了这一切。它首先在桌面上创建两个文件,第一个文件只有纯红色背景,第二个文件只有一个段落。然后它将这两个重叠的文件合并到第三个文档中。有关其他 cmets,请参见代码。

    using System;
    using System.IO;
    using System.Windows.Forms;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    namespace WindowsFormsApplication1 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e) {
                //Folder that we'll work from
                string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string pdf1 = Path.Combine(workingFolder, "pdf1.pdf");//PDF with solid red background color
                string pdf2 = Path.Combine(workingFolder, "pdf2.pdf");//PDF with text
                string pdf3 = Path.Combine(workingFolder, "pdf3.pdf");//Merged PDF
    
                //Create a basic PDF filled with red, nothing special
                using (FileStream fs = new FileStream(pdf1, FileMode.Create, FileAccess.Write, FileShare.None)) {
                    using (Document doc = new Document(PageSize.LETTER)) {
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                            doc.Open();
                            PdfContentByte cb = writer.DirectContent;
                            cb.SetColorFill(BaseColor.RED);
                            cb.Rectangle(0, 0, doc.PageSize.Width, doc.PageSize.Height);
                            cb.Fill();
                            doc.Close();
                        }
                    }
                }
    
                //Create a basic PDF with a single line of text, nothing special
                using (FileStream fs = new FileStream(pdf2, FileMode.Create, FileAccess.Write, FileShare.None)) {
                    using (Document doc = new Document(PageSize.LETTER)) {
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                            doc.Open();
                            doc.Add(new Paragraph("This is a test"));
                            doc.Close();
                        }
                    }
                }
    
                //Create a basic PDF
                using (FileStream fs = new FileStream(pdf3, FileMode.Create, FileAccess.Write, FileShare.None)) {
                    using (Document doc = new Document(PageSize.LETTER)) {
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                            doc.Open();
    
                            //Get page 1 of the first file
                            PdfImportedPage imp1 = writer.GetImportedPage(new PdfReader(pdf1), 1);
                            //Get page 2 of the second file
                            PdfImportedPage imp2 = writer.GetImportedPage(new PdfReader(pdf2), 1);
                            //Add the first file to coordinates 0,0
                            writer.DirectContent.AddTemplate(imp1, 0, 0);
                            //Since we don't call NewPage the next call will operate on the same page
                            writer.DirectContent.AddTemplate(imp2, 0, 0);
                            doc.Close();
                        }
                    }
                }
    
                this.Close();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2012-04-09
      相关资源
      最近更新 更多