PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Mon Feb 13, 2023 2:13 pm 
Offline

Joined: Thu May 24, 2018 8:05 pm
Posts: 9
I am trying to print some pdf documents using the MigraDocPrintDocument class I found on GitHub (https://github.com/empira/MigraDoc/blob/master/MigraDoc/src/MigraDoc.Rendering/Rendering.Printing/MigraDocPrintDocument.cs). The class accepts an input argument of a MigraDoc Document object. I have a bunch of pdf files stored somewhere that I want to print using automation and no human intervention.

My thought process is this:
1) Get a PDFSharp PdfDocument object by using PdfDocument PDFDocObject = PdfReader.Open(FileToPrint, PdfDocumentOpenMode.Import);
2) Create a new instance of a MigraDoc Document and get the contents of PDFDocObject into it.
3) Create an instance of the class, MigraDocPrintDocument, and pass it the MigraDoc Document object so I can print it.

The problem is that I do not know how to accomplish step #2. I know how to create a MigraDoc Document and turn it into a PDFSharp PdfDocument but I cannot figure out how to reverse the process.

Can anyone help me with this? It would be much appreciated.

Thanks in advance!

Darren Haverstick
Paul Mueller Company


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 13, 2023 2:27 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
dhaverstick wrote:
The problem is that I do not know how to accomplish step #2.
Better do not attempt that, it is very complicated.

MigraDoc is a Word processor, PDF is a graphics tool. From Word to Graphics is simple, from Graphics to Word is complicated.

You can try printing using Adobe Reader command line options.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 13, 2023 2:55 pm 
Offline

Joined: Thu May 24, 2018 8:05 pm
Posts: 9
Yes, the Acrobat Reader option would work, but that opens up a whole other can of worms that I would just as soon not have to deal with.

My company has many pdf-manipulating programs written using a variety of pdf software tools. For those programs that fall under my purview, I am trying to replace all the pdf software tools with just the MigraDoc/PDFSharp solution. Adding Acrobat Reader to the mix complicates matters instead of simplifying them.

Thanks anyway!

Darren


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 13, 2023 7:37 pm 
Offline

Joined: Thu May 24, 2018 8:05 pm
Posts: 9
Okay, so after doing some digging, I have found a solution to this problem. I will post it here in hopes that it might help someone else.

I create an instance of a PdfDocument by reading the contents of a pdf file.

PdfDocument PDFDocObject = PdfReader.Open(FileToPrint, PdfDocumentOpenMode.Import); //FileToPrint is the path to the pdf file

I then create a new instance of a Document object.

Document DocObject = new();

I now iterate through the pages of the PdfDocument and add the contents to the Document instance.

for (int x = 1; x <= PDFDocObject.PageCount; x++)
{
//Get the PdfPage object
PdfPage PageObject = PDFDocObject.Pages[x - 1];
//Create a section in the Document object for each page
Section DocSection = DocObject.AddSection();
//Here is the trick to the whole thing. It turns out that you can add
//a page from a pdf file as an image to a Document object. Just supply
//the path to the pdf file and the page number you want. The counter is 1-based.
DocSection.AddImage(FileToPrint & "#" & x.ToString);
//Now I need to set up the page of the Document object to match the PdfPage setup
PageSetup PageSetupObj = DocObject.DefaultPageSetup.Clone();

if (PageObject.Orientation == PdfSharp.PageOrientation.Landscape)
{
PageSetupObj.Orientation = Orientation.Landscape;
}
else
{
PageSetupObj.Orientation = Orientation.Portrait;
}

PageSetupObj.PageHeight = PageObject.Height.Point;
PageSetupObj.PageWidth = PageObject.Width.Point;

//I set all the page margins to zero since I know that those values will work for
//the pdf files that I'm interested in. Your situation may be different.
PageSetupObj.BottomMargin = 0;
PageSetupObj.LeftMargin = 0;
PageSetupObj.RightMargin = 0;
PageSetupObj.TopMargin = 0;

DocSection.PageSetup = PageSetupObj;
}

Now my Document instance should have all the content of my pdf file. I tested this by rendering the document and saving the resulting pdf file.

PdfDocumentRenderer PDFDocRenderer = new();
PDFDocRenderer.Document = DocObject;
PDFDocRenderer.RenderDocument();
PDFDocRenderer.PdfDocument.Save("C:\\Temp\\Test.pdf");

The saved pdf file matched the original so I'm calling it a win!

Darren


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 13, 2023 7:59 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
dhaverstick wrote:
The saved pdf file matched the original so I'm calling it a win!
Great. But you cannot print this file with the class you mention.
If it would be that simple, then I would have told you to do so.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 14, 2023 4:36 pm 
Offline

Joined: Thu May 24, 2018 8:05 pm
Posts: 9
Yeah, I figured that out the hard way. Bums me out that the MigraDocPrintDocument class out on GitHub doesn't actually work, or if it does, I cannot figure out how to use it.

Any advice, Thomas? Or should I just punt the whole project?

Darren


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 14, 2023 6:13 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
dhaverstick wrote:
Bums me out that the MigraDocPrintDocument class out on GitHub doesn't actually work, or if it does, I cannot figure out how to use it.
It works. It can print anything that MigraDoc shows in the print preview.
But PDFsharp cannot render PDF files (see FAQ), so you only get a placeholder for any PDF objects contained in the MigraDoc document.

Reading PDF files is very complicated. Rendering PDF files is much more complicated - and PDFsharp does not attempt to do that and never did, nor ever claimed anything along that line.

So if you take a tool that converts PDF to JPEG, BMP, PNG, TIFF, or other image formats, then you will be able to use MigraDocPrintDocument to print them.
So maybe you have to add another tool to your rooster.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 14, 2023 8:01 pm 
Offline

Joined: Thu May 24, 2018 8:05 pm
Posts: 9
Thanks for the explanation, Thomas. Sorry that I wasted your time.

Darren


Top
 Profile  
Reply with quote  
PostPosted: Wed Feb 15, 2023 8:38 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
dhaverstick wrote:
Sorry that I wasted your time.
You're welcome. I wouldn't call it wasted time. You think out of the box. I'm sorry that your plan was stopped by an implementation limitation of MigraDoc.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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 134 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