2010-11-19 8 views
5

C#에서 PDF 출력 파일을 만들 수있는 솔루션을 찾고 있는데,이 파일은 별도의 정적 PDF 파일로 백그라운드 워터 마크로 병합됩니다 .다른 PDF 파일과 함께 C#에서 배경 워터 마크로 PDF 파일을 생성해야합니다.

나는 사용자가 자신의 인보이스 PDF 버전을 만들 수있는 시스템 작업을하고 있습니다. C#에서 모든 인보이스 기능을 다시 만들지 않고 배경 인 워터 마크로 빈 인보이스 (Adobe Illustrator에서 작성) 용 PDF 버전을 사용하고 단순히 동적 인보이스 세부 정보를 맨 위에 오버레이하는 것이 가장 쉬운 해결책이라고 생각합니다.

데이터 다이내믹스의 액티브 리포트를보고 있었지만 기존 PDF 파일에 보고서를 오버레이하거나 병합 할 수있는 능력이없는 것으로 나타났습니다.

다른 .NET PDF 보고서 제품에이 기능이 있습니까?

감사합니다.

답변

13

감사합니다. bhavinp. 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(); 
     } 


    } 
} 
+0

+1 자신의 질문에 대한 후속 조치. –

+0

감사합니다. – Ello

0

나는이 질문을 가로 질러 와서 때문에 무료 버전에서 라이센스에 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 , Point.Empty); 

       } 
       MemoryStream stream = new MemoryStream(); 
       outputPdf.Save("C:\\OutputFile.pdf"); 
      } 
     } 
    } 
} 
관련 문제