PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 1:16 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 21 posts ] 
Author Message
PostPosted: Thu Nov 19, 2009 6:28 am 
Offline

Joined: Wed Nov 18, 2009 5:37 pm
Posts: 7
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 19, 2009 9:08 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri May 14, 2010 4:12 pm 
Offline

Joined: Fri May 14, 2010 4:04 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Mon May 17, 2010 7:34 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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[].

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon May 17, 2010 11:35 am 
Offline

Joined: Fri May 14, 2010 4:04 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Wed May 26, 2010 10:43 am 
Offline

Joined: Mon Sep 21, 2009 3:03 pm
Posts: 4
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


Top
 Profile  
Reply with quote  
PostPosted: Wed May 26, 2010 11:09 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 26, 2010 11:58 am 
Offline

Joined: Mon Sep 21, 2009 3:03 pm
Posts: 4
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


Top
 Profile  
Reply with quote  
PostPosted: Wed May 26, 2010 12:15 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed May 26, 2010 12:27 pm 
Offline

Joined: Mon Sep 21, 2009 3:03 pm
Posts: 4
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 10, 2011 12:52 pm 
Offline

Joined: Tue Nov 08, 2011 12:24 pm
Posts: 22
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 10, 2011 12:58 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Create a PDF with PDFsharp, then use AddImage() to add this PDF to your MigraDoc document. No placeholder needed.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 10, 2011 1:38 pm 
Offline

Joined: Tue Nov 08, 2011 12:24 pm
Posts: 22
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Nov 10, 2011 2:18 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 15, 2011 9:56 am 
Offline

Joined: Tue Nov 08, 2011 12:24 pm
Posts: 22
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 15, 2011 12:43 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 22, 2011 4:37 pm 
Offline

Joined: Tue Nov 22, 2011 3:24 pm
Posts: 5
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 23, 2011 1:28 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
C.Bienek wrote:
Did you meen to save as file and then add it?
Yep.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 30, 2011 2:24 pm 
Offline

Joined: Tue Nov 22, 2011 3:24 pm
Posts: 5
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 30, 2011 2:43 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Nov 30, 2011 4:26 pm 
Offline

Joined: Tue Nov 22, 2011 3:24 pm
Posts: 5
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 145 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group