Hi Simon,
thanks for the prompt reply. I've implemented your code and it does indeed apply the PDFs in the background. I noticed, however, that the backgrounds were partially opaque and rather pixelated.
A little research on the forums threw up an article to solve the transparency issue. Unfortunately this forum won't let me post the URL to it (WHY????), so if anyone wants to see it, replace the underscores in the following with dots:
https://www_e-iceblue_com/forum/transpa ... 46-15_htmlSorting out the quality of the background image was as simple as modifying the parameters for the save method.
My updated C# code is as follows:
- Code: Select all
public static void ApplyBackgrounds(string sourcePdfPath, string firstPagePdfPath, string otherPagePdfPath, string outputPdfPath)
{
var first = LoadPdfAsImage(firstPagePdfPath, 300);
var other = LoadPdfAsImage(otherPagePdfPath, 300);
using (var pdf = new PdfDocument(sourcePdfPath))
{
for (var i = 0; i < pdf.Pages.Count; i++)
{
var page = pdf.Pages[i];
page.DefaultLayerIndex = -1;
page.BackgroundImage = i == 0 ? first : other;
page.BackgroudOpacity = 1.0f;
}
pdf.SaveToFile(outputPdfPath, FileFormat.PDF);
}
}
private static System.Drawing.Image LoadPdfAsImage(string pdfPath, int dpi)
{
using (var doc = new PdfDocument(pdfPath))
return doc.SaveAsImage(0,PdfImageType.Metafile, dpi, dpi);
}
The only downside with this apprach is that it significantly increases the size of the generated PDF. In my test case the three PDFs that go into the final result are less than 200k in total, yet the generated output is 1.2MB. I presume that this is because I'm placing a 300dpi image on each page, rather than the text elements from the letterhead templates. (NOTE: I've selected 300pdi as the minimum acceptable print resolution for the background - any less does not print sharply enough)
As an alternative approach, is there any way to enumerate the elements in the letterhead PDFs and apply them to a PdfTemplate on each page of the source PDF? In addition to reducing the output file size, this will also make the text in the letterhead (such as the address, contact email, etc) selectable for copy & paste.