PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Put an image into pdf file
https://forum.pdfsharp.net/viewtopic.php?f=2&t=186
Page 1 of 1

Author:  njuliard [ Tue Jul 03, 2007 12:43 pm ]
Post subject:  Put an image into pdf file

Hi everybody,

I would like to create pdf files with JPEG images inside. How do I need to proceed ?
For more informations, my pdf file is automatically generated from a website thanks to a web form.

Thanks in advance,

Nicolas

Author:  njuliard [ Wed Jul 04, 2007 8:23 am ]
Post subject:  Finally I've found the answer by myself

Code:
PdfSharp.Drawing.XImage image = PdfSharp.Drawing.XImage.FromFile(@".\images\image.jpg");
double width = image.Width;
double height = image.Height;
width.GetType();
height.GetType();
gfx.DrawImage(image, 100, 100);


Have a nice day,

Nicolas

Author:  edmicman [ Wed Mar 05, 2008 5:06 pm ]
Post subject: 

Can you post your full code to add a jpg image to create a PDF? I have 1 or more JPG images that I would like to "output" to a single PDF in memory, that can be viewed on the web. I don't want to save the PDF, just want to create it on the fly if a user clicks a link.

I tried something like this:
Code:
PdfSharp.Drawing.XImage image = PdfSharp.Drawing.XImage.FromFile(FullFilename);
                            double width = image.Width;
                            double height = image.Height;
                            width.GetType();
                            height.GetType();
                            PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
                            gfx.DrawImage(image, width, height);


but that didn't work. I had been working with the FileStream to load my image, then loading that into a MemoryStream to try and get the PDF document to save it, but I'm just getting a blank PDF now. Any help would be greatly appreciated!

Author:  Thomas Hoevel [ Thu Mar 06, 2008 8:03 am ]
Post subject: 

edmicman wrote:
[...] but I'm just getting a blank PDF now.

Without checking your code, I'd say that your variables width and height contain the image dimensions in Pixel, while the DrawImage method requires the position to draw at in Points.
Small images will be drawn on the page, larger images will be drawn outside the page (DIN A4 is 595 by 842 points in size).
You'll get better results if you stick with the position (100, 100) from the sample above.

There should be versions of DrawImage that take both position and size - I guess this is what you're looking for.

BTW: the calls to GetType() are not required - they're only good for breakpoints ...

Author:  edmicman [ Thu Mar 06, 2008 2:47 pm ]
Post subject: 

Thank you! I did end up getting it working....my images are displaying, and I can get them to display in the size I want. Now I just have to work on getting a better source image so it doesn't lose as much quality when I increase the size :-)

Great job on the library!

Author:  dalebert [ Fri Apr 03, 2009 5:02 pm ]
Post subject:  clarification

Hello,

I was hoping I could get some clarification on the code "edmicman" provided.

In the last line (gfx.DrawImage(image, width, height);), what does the "gfx" refer to?

I'm trying to use this as a guideline for adding a graphic to my PDF's and in my .net web form (in VB) it needs to be declared and I'm not sure what data type it's supposed to be.

Can someone enlighten me?

Thanks.

Author:  dalebert [ Fri Apr 03, 2009 5:17 pm ]
Post subject: 

Nevermind, I got it figured out.

Thanks anyway...

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/