Draw PDF Image in WPF

Drawing PDF image in WPF is not as easy as most of developers imagine. Getting a beautifully  matched PDF text and image requires them to properly set both the image size and image position in the PDF document. Any unsuitably handled in each aspect may lead the text covered by the PDF image. This solution is offered for you to draw PDF image in WPF applications without the worry above.

 

Spire.PDF for WPF is a WPF PDF component which provides great convenience in reading, writing and modifying PDF documents without using Adobe Acrobat or any third party component library. Spire.PDF for WPF contains a namespace “Spire.Pdf.Graphics” in which there are four versions of DrawImage method that can be applied to insert PdfImage in PDF document.

 

Please feel free to Download Spire.PDF for WPF and draw PDF Image in WPF, Below is an effective screenshot of the PDF image.

 

Draw Image in PDF

 

Now this is the key procedure of PDF image in WPF.

 

1, Load PDF and image file from system.
Two files are demanded in this step: a PDF file in which the image will be inserted and an image file.

 

[C#]
doc.LoadFromFile(@"..\Sample.pdf");
System.Drawing.Image image = System.Drawing.Image.FromFile(@"..\MS.jpg");
[VB.NET]
doc.LoadFromFile("..\Sample.pdf")
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("..\MS.jpg")

 

2,Adjust image size and draw image in PDF file.
In this step, one way to use DrawImage method is demonstrated with two parameters: the source image and its position.

 

[C#]
//adjust image size
int width = image.Width;
int height = image.Height;
float schale = 0.7f;
System.Drawing.Size size = new System.Drawing.Size((int)(width * schale), (int)(height * schale));
Bitmap schaleImage = new Bitmap(image, size);
//draw image into the first PDF page at specific position 
PdfImage pdfImage = PdfImage.FromImage(schaleImage);
PdfPageBase page0 = doc.Pages[0];
PointF position = new PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10);
page0.Canvas.DrawImage(pdfImage, position);

[VB.NET]
'adjust image size
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim schale As Single = 0.7F
Dim size As New System.Drawing.Size(CInt(Math.Truncate(width * schale)), CInt(Math.Truncate(height * schale)))
Dim schaleImage As New Bitmap(image, size)
'draw image into the first PDF page at specific position 
Dim pdfImage__1 As PdfImage = PdfImage.FromImage(schaleImage)
Dim page0 As PdfPageBase = doc.Pages(0)
Dim position As New PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10)
page0.Canvas.DrawImage(pdfImage__1, position)

 

It is obvious that Spire.PDF for WPF can realize PDF image task in minutes. Besides, Spire.PDF for WPF also has the functions of PDF protect, PDF conversion, PDF drawing, click to know more about this Professional WPF PDF Components.