This is a migrated thread and some comments may be shown as answers.

Watermark

2 Answers 31 Views
This is a migrated thread and some comments may be shown as answers.
Fadi
Top achievements
Rank 1
Veteran
Fadi asked on 16 Dec 2020, 07:20 PM

In the process of evaluating the PDF functionality provided by Telerik so we can switch components (hopefully).

Is there a way to add a watermark to an existing PDF using another PDF as that watermark?

Thanks.

2 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 17 Dec 2020, 09:51 AM

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/.

0
Fadi
Top achievements
Rank 1
Veteran
answered on 17 Dec 2020, 01:08 PM

Excellent, that worked just like we needed. One step closer.

Thank you!

Asked by
Fadi
Top achievements
Rank 1
Veteran
Answers by
Dimitar
Telerik team
Fadi
Top achievements
Rank 1
Veteran
Share this question
or