【问题标题】:Need to create a PDF file from C# with another PDF file as the background watermark需要从 C# 创建一个 PDF 文件,并将另一个 PDF 文件作为背景水印
【发布时间】:2011-05-12 17:20:07
【问题描述】:

我正在寻找一种解决方案,它允许我从 C# 创建一个 PDF 输出文件,该文件还合并为一个单独的静态 PDF 文件作为背景水印。

我正在开发一个允许用户创建其发票的 PDF 版本的系统。与其尝试在 C# 中重新创建所有发票功能,我认为最简单的解决方案是使用空白发票的 PDF 版本(由 Adob​​e Illustrator 创建)作为背景水印,并简单地将动态发票详细信息覆盖在顶部。

我正在查看来自 Data Dynamics 的 Active Reports,但似乎它们没有能力将报告覆盖或合并到现有 PDF 文件上。

是否还有其他具有此功能的 .NET PDF 报告产品?

【问题讨论】:

    标签: c# pdf merge


    【解决方案1】:

    谢谢你。 iText 似乎做到了这一点,并且完全按照我的期望工作。

    对于尝试合并到 PDF 文件并覆盖它们的其他人,以下基于使用 iTextPDF 库的示例代码可能会有所帮助。

    结果文件是原始文件和背景文件的组合

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    
    namespace iTextTest
    {
        class Program
        {
                            /** The original PDF file. */
            const String Original = @"C:\Jobs\InvoiceSource.pdf";
            const String Background = @"C:\Jobs\InvoiceTemplate.pdf";
            const String Result = @"C:\Jobs\InvoiceOutput.pdf";
    
            static void Main(string[] args)
            {
                ManipulatePdf(Original, Background, Result);
            }
    
            static void ManipulatePdf(String src, String stationery, String dest)
            {
                // Create readers
                PdfReader reader = new PdfReader(src);
                PdfReader sReader = new PdfReader(stationery);
                // Create the stamper
                PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
                // Add the stationery to each page
                PdfImportedPage page = stamper.GetImportedPage(sReader, 1);
                int n = reader.NumberOfPages;
                PdfContentByte background;
                for (int i = 1; i <= n; i++)
                {
                    background = stamper.GetUnderContent(i);
                    background.AddTemplate(page, 0, 0);
                }
                // CLose the stamper
                stamper.Close();
            }
    
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了这个问题,由于免费版本的许可,我无法使用 iTextSharp 库

      iText AGPL 许可适用于希望根据 AGPL “copyleft”条款将其整个应用程序源代码作为免费软件与开源社区共享的开发人员。

      但是我发现 PDFSharp 可以使用以下代码工作。

      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.IO;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using PdfSharp.Drawing;
      using PdfSharp.Pdf;
      using PdfSharp.Pdf.IO;
      
      namespace PDFTest
      {
          class Program
          {
              static Stream Main(string[] args)
              {
                  using (PdfDocument originalDocument= PdfReader.Open("C:\\MainDocument.pdf", PdfDocumentOpenMode.Import))
                  using (PdfDocument outputPdf = new PdfDocument())
                  {
                      foreach (PdfPage page in originalDocument.Pages)
                      {
                          outputPdf.AddPage(page);
                      }
                      var background = XImage.FromFile("C:\\Watermark.pdf");
                      foreach (PdfPage page in outputPdf.Pages)
                      {
                          XGraphics graphics = XGraphics.FromPdfPage(page);
                          graphics.DrawImage(background, 1, 1);
      
                      }
                      MemoryStream stream = new MemoryStream();
                      outputPdf.Save("C:\\OutputFile.pdf");
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用这个。 http://itextpdf.com/ 它适用于 Java 和 .NET

        【讨论】:

          猜你喜欢
          • 2011-12-02
          • 1970-01-01
          • 1970-01-01
          • 2017-03-17
          • 2023-03-20
          • 1970-01-01
          • 2013-06-10
          • 2010-09-23
          • 1970-01-01
          相关资源
          最近更新 更多