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

Drawing images in MigraDoc using PdfSharp
https://forum.pdfsharp.net/viewtopic.php?f=2&t=944
Page 1 of 1

Author:  VibhoreBenjamin [ Thu Nov 19, 2009 6:28 am ]
Post subject:  Drawing images in MigraDoc using PdfSharp

Hi, I am new to PdfSharp and MigraDoc

I have a requirement where I have to create a Pdf document which has a table (which may spread across multiple pages).
e.g.

Expense date Description Category Amount spent Receipt
-------------------------------------------------------------------------------------
01-01-2009 Lunch at McD Food $50 View
01-01-2009 Taxi to station Travel $30 View
- - - - View
--------------------------------------------------------------------------------------
Below the table i need to put some images. For each row in the table above there is an image at the bottom. The last cell of each row in the table contains a hyperlink to the associated image at the bottom (basically a hyperlink). So clicking on View will take to the respective image.

This can be easily done through MigraDoc :D . But only if the images to be inserted in the Pdf are available in form of file because MigraDoc use file based image. My images are in memory and not as file.

PdfSharp has the capability to add image from memory, but the rest of the things are not as easy as they are in MigraDoc (automatic page break, Bookmarks, etc). However i read somewhere that we can use both PdfSharp and MigraDoc to do this. It was suggested to create document using MigraDoc and place some place holders where ever image is required. Then using PdfSharp replace those placeholders with images.

But i cant figure out how to do it. I searched for some samples , to how to find the required placeholders and replace it with image but could not find any.
Can you please help me (with some sample code), for how to insert placeholders from MigraDoc and then how to replace it with images using PdfSharp.

Hope to hear soon.

Thanks in advance

Author:  Thomas Hoevel [ Thu Nov 19, 2009 9:08 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Hi!

The placeholders are on our MigraDoc TODO list, but are not yet implemented.

To use placeholders you need access to some internal structures as described here.
We don't have sample code for this.

Author:  enzocontini [ Fri May 14, 2010 4:12 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Benjamin said that "PdfSharp has the capability to add image from memory": without using MigraDoc (that seems not to have that features, someone can explain how to do that with PdfSharp API? If I have a byte[] , how can I display that in memory image in a pdf document with the PdfSharp classes???

Thank you
Enzo

Author:  Thomas Hoevel [ Mon May 17, 2010 7:34 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

You can create an XImage from an Image:
Code:
public static XImage FromGdiPlusImage(
   Image image
)


You can create an Image from a Stream:
Code:
public static Image FromStream(
   Stream stream
)


And you can create a MemoryStream from a byte[].

Author:  enzocontini [ Mon May 17, 2010 11:35 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thank you very much!! It works!

... I wonder if you could insert your code in some of the examples (i.e. the web one): very often it happens to have in memory images and people using the library could need it!

Kind regards
Enzo

Author:  Westy [ Wed May 26, 2010 10:43 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Ahh, this exactly what I needed.

Been wondering why my MigraDoc generated PDFs don't have images embedded, and how to get around it.
Looks like this may sort it, although quite a lot of hassle.

I find myself wondering why there isn't an PdfImageEmbedding parameter to PdfDocumentRenderer?!

Cheers,
Westy

Author:  Thomas Hoevel [ Wed May 26, 2010 11:09 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Westy wrote:
I find myself wondering why there isn't an PdfImageEmbedding parameter to PdfDocumentRenderer?!

Coz all images are embedded by default. You can't use non-embedded images with PDFsharp.

Author:  Westy [ Wed May 26, 2010 11:58 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

If that's the case them I'm now very confused!

Got code that creates a PDF, putting an image into it that is extracted from the assembly resources to a temporary location.

If I delete the temporary file in a finally block, as I want to, then when the PDF is opened the image renders as "Image not found". If I comment out the file deletion then the image renders as expected...


This has seriously scuppered my plan to use MigraDoc, although am looking into the solution above.

Cheers,
Westy

Author:  Thomas Hoevel [ Wed May 26, 2010 12:15 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Westy wrote:
"Image not found"

This message (along with a red rectangle with a red "X" as an image placeholder) comes from MigraDoc if an image cannot be found during PDF creation.
Sounds as if the image is deleted too early.

Author:  Westy [ Wed May 26, 2010 12:27 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thomas Hoevel wrote:
This message (along with a red rectangle with a red "X" as an image placeholder) comes from MigraDoc if an image cannot be found during PDF creation.
Sounds as if the image is deleted too early.


Yes, this just occurred to me on the way back from getting my lunch.
Will tweak the cleanup.

Ideally I'd like the flatten Acroforms thing, but doubt that'll be coming anytime soon...

Thanks for your help.
Westy

Author:  tulips [ Thu Nov 10, 2011 12:52 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Hello,

I need to do exactly this. Use Migradoc to create the document and then create some chart using pdfsharp. Any example code I can see to for pdfsharp being used in migradoc? Or just tell me how to start, like how do I make pdfsharp created graph as image, right now which is in pdf format? How to put a image placeholder in migradoc?

Thank you!

-tulips

Author:  Thomas Hoevel [ Thu Nov 10, 2011 12:58 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Create a PDF with PDFsharp, then use AddImage() to add this PDF to your MigraDoc document. No placeholder needed.

Author:  tulips [ Thu Nov 10, 2011 1:38 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thank you Thomas! That worked!

The only thing is it added it in next page. May be it can be compressed to add it on the same page.

Author:  Thomas Hoevel [ Thu Nov 10, 2011 2:18 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

tulips wrote:
May be it can be compressed to add it on the same page.
If you want a chart of size e.g. 10 cm by 5 cm, then create a PDF page of exactly that size (i. e. don't draw a small chart on a DIN A 4 page).

Author:  tulips [ Tue Nov 15, 2011 9:56 am ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thank you! Very helpful.

Can I save this pdfsharp chart file somewhere on the server and not on C: ? It's a web project so may be in Images folder or somewhere. It takes the C: pathfile and works fine but doesn't take a server path. I don't care to save the intermediate graph anywhere and it should just get deleted once it's used in the Migadoc. Thoughts anyone? OR this is a wrong forum for this question and there are no limitations on where we can store pdfsharp/migradocs?

-tulips

Author:  Thomas Hoevel [ Tue Nov 15, 2011 12:43 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

tulips wrote:
OR this is a wrong forum for this question and there are no limitations on where we can store pdfsharp/migradocs?
There are no limitations coming from PDFsharp/MigraDoc - but the libraries are unaware of IIS and do not support paths starting with "~/" (it's up to you to get the absolute path from IIS).

You can place the files in the TEMP directory (get TEMP from the Environment). I'd use the TempFileCollection class to have .NET remove the files automatically.

Author:  C.Bienek [ Tue Nov 22, 2011 4:37 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thomas Hoevel wrote:
Create a PDF with PDFsharp, then use AddImage() to add this PDF to your MigraDoc document. No placeholder needed.


Hi there,

Hi there, im not quite sure if I understood right, but the way I tried did not work.

Code:
           PdfDocument pdfDoc = new PdfDocument();
            PdfPage pdfPage = pdfDoc.AddPage();
            pdfPage.Width = new XUnit(25, XGraphicsUnit.Centimeter);
            pdfPage.Height = new XUnit(16.5, XGraphicsUnit.Centimeter);

            XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
            XGraphicsState state = gfx.Save();
            gfx.ScaleTransform(72f / 25.4);         // scale to millimeter

             // ...
             // draw some line and stuff
             // ...

             gfx.Restore(state);

            // Create a MigraDoc document
            Document document = CreateDocument();
            Section content = document.AddSection();
            content.PageSetup.Orientation = Orientation.Landscape;
            content.PageSetup.PageFormat = PageFormat.A4;

            content.AddImage(pdfDoc);  // AddImage seems to except a string only

            // ...


Did you meen to save as file and then add it?
Or is there a method which can add a PdfPage or PdfDocument as Image that Im not aware of?

Author:  Thomas Hoevel [ Wed Nov 23, 2011 1:28 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

C.Bienek wrote:
Did you meen to save as file and then add it?
Yep.

Author:  C.Bienek [ Wed Nov 30, 2011 2:24 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thank you.
I just wondered if it is neccesary to add a pdf as file and not a page as object. But anyway this way it works.
Beside the fact that the image is positioned slightly to far right.
It seems like there is a padding rule or any distance set.
I checked my tables lefpadding as well as the rows and the cells DistanceFromLeft properties which are all 0.
Its pretty much the same distance some text is placed.
How can I control this distance for my specific cell to be 0 as desired?

Author:  Thomas Hoevel [ Wed Nov 30, 2011 2:43 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

The table is by default moved to the left to have text inside and outside of the table aligned at the same position.

Setting table.Rows.LeftIndent to 0 should fix that.

Author:  C.Bienek [ Wed Nov 30, 2011 4:26 pm ]
Post subject:  Re: Drawing images in MigraDoc using PdfSharp

Thanks for quick answer.

although table.Rows.LeftIndent did not help.
But after realizing that the values are not all zero but most of them not initialized at all I set:

Code:
table.Columns[0].LeftPadding = new Unit(0, UnitType.Millimeter);


which does the job for me.

If now my table would take the width of the image (not only the height) I would be fine.

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