Hi Fadi,
Yes, this is possible, to achieve it you can use the PdfStreamWriter which allows you to add the watermark content to an existing file. Here is the sample code that can be used for this:
public static readonly string InputFileMultipageDocument = @"..\..\Lorem.pdf";
public static readonly string InputFileWatermark = @"..\..\Watermark.pdf";
private void RadButton_Click(object sender, RoutedEventArgs e)
{
PrependAndAppendPageContent("Result.pdf");
}
private static void PrependAndAppendPageContent(string resultFileName)
{
string initialDocument = InputFileMultipageDocument;
string resultDocument = Path.Combine(@"..\..\", resultFileName);
using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(resultDocument)))
{
using (PdfFileSource fileSource = new PdfFileSource(File.OpenRead(initialDocument)))
{
RadFixedPage backgroundContentOwner = GenerateWatermark(InputFileWatermark);
foreach (PdfPageSource pageSource in fileSource.Pages)
{
using (PdfPageStreamWriter pageWriter = fileWriter.BeginPage(pageSource.Size, pageSource.Rotation))
{
using (pageWriter.SaveContentPosition())
{
double xCenteringTranslation = (pageSource.Size.Width - backgroundContentOwner.Size.Width) / 2;
double yCenteringTranslation = (pageSource.Size.Height - backgroundContentOwner.Size.Height) / 2;
pageWriter.ContentPosition.Translate(xCenteringTranslation, yCenteringTranslation);
pageWriter.WriteContent(backgroundContentOwner);
}
pageWriter.WriteContent(pageSource);
}
}
}
}
}
private static RadFixedPage GenerateWatermark(string watermark)
{
using (Stream imageStream = File.OpenRead(watermark))
{
PdfFormatProvider provider = new PdfFormatProvider();
var docuemnt = provider.Import(imageStream);
return docuemnt.Pages.First();
}
}
I have attached a complete project that shows this as well.
Regards,
Dimitar
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.