PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Tue Nov 04, 2025 9:35 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Thu Feb 13, 2014 7:25 pm 
Offline

Joined: Thu Feb 13, 2014 7:09 pm
Posts: 13
Hey there,

I have spent a few hours trying to resolve this problem, however I cannot find anything wrong with what I have done in contrast to what has been suggested. I've found a few different forum posts and different websites, but the following basically sums everything up (viewtopic.php?f=2&t=2306).

From my understanding, we can utilize the Section.AddImage() method for adding an existing PDF file into a MigraDoc document section. I have wrestled and searched for an answer, but when I do this, all that appears is a red rectangle with an X through it:
Image

From reading, I found people were experiencing this (along with some text saying "Image not found.") when the PDF file or image file could not be found. I determined that this was not the case by providing a fake path to the AddImage() method. When doing so, I did not receive a red X and simply received the "Image not found." error message on the generated PDF.

I further read that you can define the page you want to add as an image by appending "#" followed by the page number in the path passed to AddImage(). I have tested with a path of "C:\test.pdf" and "C:\test.pdf#1" with no difference in the result.

Here is the code I have so far. I can confirm 100% that i.DrawingFilePath exists and that C:\test.pdf exists. Technically in the following I am not using the first bit of code (the PDFsharp code), it is remnant from trying to solve this problem. Perhaps I am going down the wrong path with the code, so, my only requirement is to be able to take an existing PDF file and insert it into a MigraDoc - I need this existing PDF file added so I can later overlay a text box on it.

Any help is greatly appreciated and thank you in advance for any assistance.

Code:
            Dim pdfDocument As PdfSharp.Pdf.PdfDocument = PdfSharp.Pdf.IO.PdfReader.Open(i.DrawingFilePath)
            Dim pdfPage As PdfSharp.Pdf.PdfPage = pdfDocument.Pages(0)
            Dim gfx As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(pdfPage)

            gfx.MUH = PdfSharp.Pdf.PdfFontEncoding.Unicode
            gfx.MFEH = PdfSharp.Pdf.PdfFontEmbedding.Default

            Dim doc As MigraDoc.DocumentObjectModel.Document = New MigraDoc.DocumentObjectModel.Document
            Dim sec As MigraDoc.DocumentObjectModel.Section = doc.AddSection

            Dim img As MigraDoc.DocumentObjectModel.Shapes.Image = sec.AddImage("C:\test.pdf#1")
            img.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Page
            img.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page
            img.WrapFormat.Style = MigraDoc.DocumentObjectModel.Shapes.WrapStyle.Through

            Dim docRenderer As MigraDoc.Rendering.DocumentRenderer = New MigraDoc.Rendering.DocumentRenderer(doc)
            docRenderer.PrepareDocument()

            Dim pageCount As Integer = docRenderer.FormattedDocument.PageCount
            Dim printDoc As MigraDoc.Rendering.Printing.MigraDocPrintDocument = New MigraDoc.Rendering.Printing.MigraDocPrintDocument

            printDoc.PrinterSettings = printerDialog.PrinterSettings
            printDoc.Renderer = docRenderer

            printDoc.Print()


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 9:20 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Hi!
curtis.watson wrote:
I have wrestled and searched for an answer, but when I do this, all that appears is a red rectangle with an X through it
PDFsharp cannot render PDF files, it only creates PDF files.

The red X is a placeholder that appears when you use PDF "images" for screen preview or for print.
Use MigraDoc to create a PDF file and the PDF "image" will be there. You can then print the PDF file (using e.g. Adobe Reader) and the images will be there.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 1:19 pm 
Offline

Joined: Thu Feb 13, 2014 7:09 pm
Posts: 13
Hey there,

Thanks for the reply. I've collected some of the forum posts I had looked at where moderators suggested using MigraDoc's Section.AddImage for adding a PDF to a MigraDoc.

http://forum.pdfsharp.net/viewtopic.php?f=2&t=2402&p=6907&hilit=addimage#p6907
http://forum.pdfsharp.net/viewtopic.php?f=2&t=2306&hilit=addimage
http://forum.pdfsharp.net/viewtopic.php?f=2&t=944&p=5306&hilit=addimage#p5306
http://forum.pdfsharp.net/viewtopic.php?p=3133#p3133

So all in all, I have a PDF file pre-generated in which I need to open, write some text on and print or save. With MigraDoc, is there any way to import an existing PDF document into the MigraDoc document?

I don't quite understand your suggestion - "Use MigraDoc to create a PDF file and the PDF "image" will be there". I am very sorry, am new to this and trying to grasp the full system. Is there a chance to get some sample code or an idea of the steps I need to perform? Will this solve my above listed requirement?


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 1:31 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Create a PDF file like the samples show - and instead of the red X you will see your PDF.
See also:
http://www.pdfsharp.net/wiki/MigraDocHe ... ample.ashx

When adding PDF pages to a MigraDoc document, there are two cases:
  • MigraDoc is used to create a PDF file from the MigraDoc document => PDF pages are showing
  • MigraDoc is used to create screen preview, RTF, or print directly => placeholder (red X) instead of PDF pages

Code:
  // Create a renderer for the MigraDoc document.
  PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);

  // Associate the MigraDoc document with a renderer
  pdfRenderer.Document = document;

  // Layout and render document to PDF
  pdfRenderer.RenderDocument();

  // Save the document...
  const string filename = "HelloWorld.pdf";
  pdfRenderer.PdfDocument.Save(filename);
  // ...and start a viewer.
  Process.Start(filename);
}

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 2:10 pm 
Offline

Joined: Thu Feb 13, 2014 7:09 pm
Posts: 13
Well hey look at that. I did the exact same steps I did previously, except rather than using a DocumentRenderer and passing it to a MigraDocPrintDocument I used the PdfDocumentRenderer and saved to disk. I have a nice looking PDF file saved to disk now.

So, if I am understanding correctly MigraDoc is unable to render the PDF documents as an image on-the-fly - the only way AddImage(SomePdfFile.PDF) will work is if the document is being saved to disk? Your previous comments are starting to make more sense now. Is there any way to achieve printing an existing PDF document with MigraDoc? I find the printing nicer with MigraDoc as it doesn't launch Adobe Reader and makes distribution to client machines easier.

If there is no other way to print using MigraDoc, I think I will be okay using PDFsharp for the time being. I've been able to print using PDFsharp without issue. If this is the approach I need to take - is there absolutely any way to suppress the Adobe Reader launch? I came across PDFsharp/MigraDoc from this post:

http://www.scottlogic.com/blog/2012/10/05/pdf-generation-and-printing-in-net-2.html

Right before the "Limitations" section, you will see a bit on customizing how the PDF is being printed. Essentially it is trying to take the graphics from the PDF document and draw it on the print page graphic. The example is incomplete and I had trouble getting it to work. Has anyone come across this solution before and got it working? Any suggestions on if its viable and how to get a PdfDocument represented by an Image, and write that Image to the print page graphic?

Thank you very much for your help so far.


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 3:09 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
I'll begin with PDFsharp: it can create or modify PDF files.
It uses an XGraphics class to draw on PDF pages. The same class can be used to draw on the screen or on a printer. But it cannot print any PDF objects.

MigraDoc can be used to create device-independent documents that can be rendered to PDF, RTF, or screen preview (or HTML if you go back to an ancient MigraDoc version).
MigraDoc uses PDFsharp to create PDF files.

When you add PDF pages to a MigraDoc document, the document is no longer device-independent and you will have to use PDF (or see the red X).
If you take this approach, you will need a third party tool (e.g. Adobe Reader) to print the PDF file. Or give the user a PDF file he/she has to print himself/herself.

Or use a third party tool (e.g. GhostScript) to create a TIFF or JPEG from the PDF page and use that - at least for printing.

Maybe provide both options: use PDF if the user wants a PDF file, use TIFF if the user wants to print.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 3:41 pm 
Offline

Joined: Thu Feb 13, 2014 7:09 pm
Posts: 13
Awesome, thank you very much for the explanation.

One last question - I was going to create a new post but I guess its somewhat related...

I am following your advice and using the PdfDocumentRenderer to generate a PdfDocument from the MigraDoc document. The PDF I am adding to the MigraDoc via. AddImage() has a page size of letter. I found that I am not supposed to modify MigraDoc's Document.DefaultPageSetup (I tried modifying Document.DefaultPageSetup.PageFormat to legal, but received some errors around that). So, rather I modified the PdfDocumentRenderer.PdfDocument.Pages(0).Size value, setting it to legal.

This works and the PDF is saved with a legal page size, however the positioning of the original PDF document is unideal (see screenshot of PDF). I would like to get it so the original PDF is positioned at the very top of the document. I achieve this when I add the PDF to the MigraDoc document by using relative positioning; however when I increase the page size on the PdfDocument I get a bunch of white space at the top of the page.

Thanks again for your help.

Screenshot: http://postimg.org/image/4gjn90gsl/

Source Code:
Code:
            Dim doc As MigraDoc.DocumentObjectModel.Document = New MigraDoc.DocumentObjectModel.Document
            Dim sec As MigraDoc.DocumentObjectModel.Section = doc.AddSection

            Dim img As MigraDoc.DocumentObjectModel.Shapes.Image = sec.AddImage(i.DrawingFilePath)
            img.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Page
            img.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page
            img.WrapFormat.Style = MigraDoc.DocumentObjectModel.Shapes.WrapStyle.Through

            Dim pdfRend As MigraDoc.Rendering.PdfDocumentRenderer = New MigraDoc.Rendering.PdfDocumentRenderer(True, PdfSharp.Pdf.PdfFontEmbedding.Default)
            pdfRend.PdfDocument = New PdfSharp.Pdf.PdfDocument()

            pdfRend.Document = doc
            pdfRend.RenderDocument()
            pdfRend.PdfDocument.Pages(0).Size = PdfSharp.PageSize.Legal

            pdfRend.PdfDocument.Save("C:\finalTest.pdf")


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 3:54 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Assign DefaultPageSetup.Clone() to the PageFormat of your section and modify that.

You can set height and/or width for your "img" to fill the entire page.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 14, 2014 4:00 pm 
Offline

Joined: Thu Feb 13, 2014 7:09 pm
Posts: 13
Amazing, thank you. Very pleased with the support here. You have yourself a great weekend.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 246 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