| PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
| MigraDoc Section.AddImage w/ PDF https://forum.pdfsharp.net/viewtopic.php?f=2&t=2733 |
Page 1 of 1 |
| Author: | curtis.watson [ Thu Feb 13, 2014 7:25 pm ] |
| Post subject: | MigraDoc Section.AddImage w/ PDF |
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: 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() |
|
| Author: | Thomas Hoevel [ Fri Feb 14, 2014 9:20 am ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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. |
|
| Author: | curtis.watson [ Fri Feb 14, 2014 1:19 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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? |
|
| Author: | Thomas Hoevel [ Fri Feb 14, 2014 1:31 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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:
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); } |
|
| Author: | curtis.watson [ Fri Feb 14, 2014 2:10 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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. |
|
| Author: | Thomas Hoevel [ Fri Feb 14, 2014 3:09 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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. |
|
| Author: | curtis.watson [ Fri Feb 14, 2014 3:41 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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") |
|
| Author: | Thomas Hoevel [ Fri Feb 14, 2014 3:54 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
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. |
|
| Author: | curtis.watson [ Fri Feb 14, 2014 4:00 pm ] |
| Post subject: | Re: MigraDoc Section.AddImage w/ PDF |
Amazing, thank you. Very pleased with the support here. You have yourself a great weekend. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|