First of all, thanks for your great tool.
In my recent project I need to add images to PDF file pages. So, how can i achieve this, i mean, reading the image in a stream and creating an XImage from it? Is it possible? Sorry, if this sounds silly, but I am a newbie for such c# programming...is there anything that I can do? Any help would be appreciated.
This article on XsPDF.com is what you looking for, check out: Load and add image to PDF in C#.
Answers
It’s no big deal. We all are ever newbies. For you question, of course yes! You can use the method XImage.FromFile.
Thank you for contacting us. XsPDF.com provides the library and method for creating an XImage from an existing image. Simple steps are included.
- Firstly, use XGraphics.FromPdfPage to get the PDF page where you want to place an embedded image.
- And then, get the filestream from your assembly.
- Finally, use XImage.FromFile to create an XImage from loading a local existing image.
For C# example code, please see as below.
// Create a new PDF document. PdfDocument document = new PdfDocument(); // Create an empty page in this document. PdfPage page = document.AddPage(); // Obtain an XGraphics object to render to XGraphics g = XGraphics.FromPdfPage(page); // Load a local image file XImage importImg = XImage.FromFile("image.jpg"); // Insert an image to pdf page, position and size can be modified g.DrawImage(importImg, 50, 50, 200, 200); // Save and show the document document.Save("AddImage.pdf"); Process.Start("AddImage.pdf");