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

Put image on existing PDF

1 Answer 1175 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Veteran
Ben asked on 18 Feb 2021, 10:42 AM

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!

1 Answer, 1 is accepted

Sort by
1
Martin
Telerik team
answered on 19 Feb 2021, 03:37 PM

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

Tags
PdfProcessing
Asked by
Ben
Top achievements
Rank 1
Veteran
Answers by
Martin
Telerik team
Share this question
or