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 recreate 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");
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With