开发者

Need to create a PDF file from C# with another PDF file as the background watermark

开发者 https://www.devze.com 2023-01-26 03:38 出处:网络
I am looking for a solution that will allow me to create a PDF outfile from C# that also merges in a seperate, static PDF file as the background watermark.

I am looking for a solution that will allow me to create a PDF outfile from C# that also merges in a seperate, static PDF file as the background watermark.

I'm working on a system that will allow users to create a PDF version of their invoice. Instead of trying to re开发者_运维百科create all of the invoice features within C# I think the easiest solution would be to use the PDF version fo the blank invoice (created from Adobe Illustrator) as a background watermark and simply overlay the dynamic invoice details on top.

I was looking at Active Reports from Data Dynamics, but it dow not appear they they have the capibility to overlay, or merge, a report onto an existing PDF file.

Is there any other .NET PDF report product that has this capibilty?


Thank you bhavinp. iText seems to do the trick and work exactly as I was hoping for.

For anyone else trying to merge to PDF files and overlay them the following example code based on using the the iTextPDF library might help.

The Result file is a combination of Original and Background

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();
        }


    }
}


I came across this question and couldn't use the iTextSharp library due to the license on the free version

The iText AGPL license is for developers who wish to share their entire application source code with the open-source community as free software under the AGPL “copyleft” terms.

However I found PDFSharp to work using the below code.

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");
            }
        }
    }
}


Use this. http://itextpdf.com/ It works with both Java and .NET

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号