Put image on existing PDF

2 posts, 0 answers
  1. Ben
    Ben avatar
    3 posts
    Member since:
    Sep 2020

    Posted 18 Feb Link to this post

    Hi there,

    I have an existing PDF and want to put an image at a certain x and y position and save the result into a new PDF. Is there someone who can provide a sample on how to achieve this?

    Thanks in advance!

  2. Martin
    Admin
    Martin avatar
    140 posts

    Posted 19 Feb Link to this post

    Hi Ben,

    You can achieve the desired functionality by importing the existing PDF document into a RadFixedDocument using the PdfFormatProvider:

    RadFixedDocument document = new RadFixedDocument();
    
    PdfFormatProvider provider = new PdfFormatProvider();
    using (Stream input = File.OpenRead("SampleDocument.pdf"))
    {
    	document = provider.Import(input);
    }

    Then you can insert an image on a specific Position using the FixedContentEditor and ImageSource object:

    ImageSource imageSource;
    using (FileStream source = File.Open("image.png", FileMode.Open))
    {
    	imageSource = new ImageSource(source);
    }
    
    RadFixedPage firstPage = document.Pages.First();
    FixedContentEditor editor = new FixedContentEditor(firstPage);
    
    editor.Position.Translate(offsetX: 150, offsetY: 200);
    editor.DrawImage(imageSource);
     And finally to export the RadFixedDocument to a new PDF file:
    string exportedDocument = "Exported.pdf";
    
    using (Stream output = File.OpenWrite(exportedDocument))
    {
    	provider.Export(document, output);
    }

    I am attaching the project I created in order to test this scenario. Please, feel free to modify it in a way closer to your scenario.

    I hope this helps. Please, let me know if there is anything else I can assist you with.

    Regards,
    Martin
    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/.

Back to Top