简体   繁体   English

将2 pdf与itextsharp压模合并

[英]merge 2 pdf with itextsharp stamper

I want to take 2 pdf files and merge them together. 我想将2个pdf文件合并在一起。

each file is one page long. 每个文件长一页。 the reason to merge them is that one file is simply a footer. 合并它们的原因是一个文件只是页脚。 The footer needs to be attached to the existing file. 页脚需要附加到现有文件。

I'm using a stamper to try and merge the 2 files. 我正在使用一个压模尝试合并两个文件。

I successfully create the output file, but it doesn't have the footer. 我成功创建了输出文件,但是没有页脚。 It's just a copy of the original input file. 这只是原始输入文件的副本。 Any idea why they aren't merging? 知道为什么他们不合并吗?

using (Stream inputPdfStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputPdfFooterStream = new FileStream(footerPdf, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
    var reader = new PdfReader(inputPdfStream);
    var stamper = new PdfStamper(reader, outputPdfStream);
    var pdfContentByte = stamper.GetOverContent(1);
    stamper.FormFlattening = true;
    stamper.Close();
}

There are different problems with your question. 您的问题有不同的问题。

Problem #1: why did you add the line stamper.FormFlattening = true; 问题#1:为什么要添加行stamper.FormFlattening = true; ? Are you working with a form? 您正在使用表格吗? I don't see you do anything with forms, so why would you flatten the document? 我看不到您对表单执行任何操作,那么为什么要展平文档?

Problem #2: You say you want to merge two documents with PdfStamper . 问题2:您说要合并两个文档与PdfStamper That is misleading. 那是误导。 Merging documents is done with PdfCopy . 合并文档是通过PdfCopy完成的。 From your explanation, I gather that you want to superimpose two documents. 根据您的解释,我认为您想叠加两个文档。 You are right that you need PdfStamper to do so. 没错,您需要PdfStamper这样做。

Problem #3: You want to use a specific document containing a footer as company stationery. 问题3:您想使用包含页脚的特定文档作为公司信纸。 In that case, you want to add the content of the stationery under the actual content. 在这种情况下,您想在实际内容添加文具的内容。 Then why are you using stamper.GetOverContent(1); 那你为什么要使用stamper.GetOverContent(1); ? Use stamper.GetUnderContent(1); 使用stamper.GetUnderContent(1); instead. 代替。

Problem #4: You are creating an inputPdfFooterStream to read the document with the footer, but I don't see you using that stream anywhere. 问题4:您正在创建一个inputPdfFooterStream来读取带有页脚的文档,但是我看不到您在任何地方使用该流。 What do you expect? 你能指望什么?

Problem #5: You didn't read the documentation. 问题5:您没有阅读文档。 This is your main problem. 这是您的主要问题。 Download chapter 6 of my book (it's available for free, and I've been referring to it in dozens of answers on StackOverflow). 下载我的书的第6章 (它是免费提供的,我在StackOverflow上的许多答案中都提到了它)。 Go to page 176 where it says "Adding company stationery to an existing document". 转到第176页,其中显示“在现有文档中添加公司信纸”。 That example meets your requirement completely! 该示例完全满足您的要求!

// Create readers
PdfReader reader = new PdfReader(src);
PdfReader s_reader = new PdfReader(stationery);
using (MemoryStream ms = new MemoryStream()) {
  // Create the stamper
  using (PdfStamper stamper = new PdfStamper(reader, ms)) {
    // Add the stationery to each page
    PdfImportedPage page = stamper.GetImportedPage(s_reader, 1);
    int n = reader.NumberOfPages;
    PdfContentByte background;
    for (int i = 1; i <= n; i++) {
      background = stamper.GetUnderContent(i);
      background.AddTemplate(page, 0, 0);
    }
  } 
  return ms.ToArray();   
}

In your code, you only have one reader. 在您的代码中,您只有一个阅读器。 In my code, I also have an object called s_reader that takes the footerPdf document and allows you to created a PdfImportedPage : 在我的代码中,我还有一个名为s_reader的对象,该对象接受footerPdf文档,并允许您创建PdfImportedPage

PdfImportedPage page = stamper.GetImportedPage(s_reader, 1);

This page is then added under the existing content of the actual document: 然后将该页面添加到实际文档的现有内容下:

background = stamper.GetUnderContent(i);
background.AddTemplate(page, 0, 0);

Note that this example assumes that both documents have the same page size and that the origin of the coordinate system of the document with the actual content coincided with the lower-left corner. 请注意,此示例假定两个文档的页面大小相同,并且具有实际内容的文档坐标系的原点与左下角重合。 If that isn't the case with your PDFs, you can have a situation where the footer isn't visible or is only partly visible. 如果您的PDF并非如此,则可能会出现页脚不可见或仅部分可见的情况。 Also: if the document with the actual content is opaque, it will also make the footer invisible. 另外:如果具有实际内容的文档是不透明的,也会使页脚不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM