Watermark

3 posts, 0 answers
  1. Fadi
    Fadi avatar
    10 posts
    Member since:
    Jul 2013

    Posted 16 Dec 2020 Link to this post

    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. Dimitar
    Admin
    Dimitar avatar
    2983 posts

    Posted 17 Dec 2020 Link to this post

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

  3. Fadi
    Fadi avatar
    10 posts
    Member since:
    Jul 2013

    Posted 17 Dec 2020 Link to this post

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

    Thank you!

Back to Top