PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Dec 01, 2021 6:43 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
 Post subject: Get content Bounding Box
PostPosted: Mon Oct 18, 2021 3:29 pm 
Offline

Joined: Mon Oct 18, 2021 3:20 pm
Posts: 5
Hi, I have recently started using PDFSharp, previously I used iTextSharp to manage my PDF files and I'm having trouble trying to get the Bounding Box surrounding only the content inside a pdf page, I need to get the height in point units of said content.

With iTextSharp this is the code used:
Code:
private iTextSharp.text.pdf.PdfRectangle GetContentBoundingBox(iTextSharp.text.pdf.PdfReader reader)
    {
      PdfReaderContentParser parser = new PdfReaderContentParser(reader);
      TextMarginFinder finder = parser.ProcessContent(1, new TextMarginFinder());
      return new iTextSharp.text.pdf.PdfRectangle(finder.GetLlx(), finder.GetLly(), finder.GetUrx(), finder.GetUry());
    }
var contentSize = GetContentBoundingBox(readerHeaderFooter);

var neededHeight = contentSize.Height;


Is there any similar functionality on PDFsharp? or a way to achieve the same or similar results.
Please help.


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 20, 2021 6:51 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 683
Location: CCAA
PDFsharp does not render PDF. To find the positions of content on the page it is necessary to render PDF.

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 20, 2021 3:14 pm 
Offline

Joined: Mon Oct 18, 2021 3:20 pm
Posts: 5
TH-Soft wrote:
PDFsharp does not render PDF. To find the positions of content on the page it is necessary to render PDF.


Thx for your answer Thomas, can you please elaborate a little bit more on how to find the positions and bounding boxes of the contents on the page, and since you are saying we need to render to PDF I guess I will need to use MigraDoc for this?.

It would be enough if you mention some of the main classes that are required for this purpose, a little sample snippet would be ideal.


Regards

Chris.


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 20, 2021 6:09 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 683
Location: CCAA
ChrisysZero wrote:
It would be enough if you mention some of the main classes that are required for this purpose, a little sample snippet would be ideal.
These classes do not exist within PDFsharp and there is no sample code at hand.

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 20, 2021 6:39 pm 
Offline

Joined: Mon Oct 18, 2021 3:20 pm
Posts: 5
TH-Soft wrote:
ChrisysZero wrote:
It would be enough if you mention some of the main classes that are required for this purpose, a little sample snippet would be ideal.
These classes do not exist within PDFsharp and there is no sample code at hand.


I undestand that, thats why I said that "guess I will need to use MigraDoc for this" since MigraDoc uses a render for its documents, so I was reffering to the classes or an example snippet using MigraDoc, sorry for any confusion Thomas, so now the question would be: is it possible to achieve this using MigraDoc?


Regards
Chris.


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 20, 2021 9:31 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 683
Location: CCAA
With MigraDoc you can "prepare" a document and then access the render information for all objects of the document. PDF pages imported into MigraDoc will be treated like rectangles and empty regions of those PDF pages will not be taken into the account.

But since you set left and right margins, top and bottom margins, it may not even be necessary to access the render information.

I assumed you were managing existing PDF files. This approach will not work for those.
It will work for PDF documents you create using the MigraDoc document object model.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 21, 2021 2:40 pm 
Offline

Joined: Mon Oct 18, 2021 3:20 pm
Posts: 5
TH-Soft wrote:
With MigraDoc you can "prepare" a document and then access the render information for all objects of the document. PDF pages imported into MigraDoc will be treated like rectangles and empty regions of those PDF pages will not be taken into the account.

But since you set left and right margins, top and bottom margins, it may not even be necessary to access the render information.

I assumed you were managing existing PDF files. This approach will not work for those.
It will work for PDF documents you create using the MigraDoc document object model.


Thx for your answer Thomas what I'm trying to achieve is the following:

I Have a single page pdf with little content, then using MigraDoc read/import the page into it and get the bounding box surrounding the content in that page, since I need to use the width and height of said content.

Here is a sample pdf page attached, I need to get the red box/bounding box surrounding the content, this pdf is created externally not with MigraDoc or PDFsharp. So what I'm trying to achieve is to read/import the pdf with a single page and get the bounding box of its contents. Is this possible with MigraDoc?


Regards
Chris.


Attachments:
Screenshot 2021-10-21 092552.png
Screenshot 2021-10-21 092552.png [ 15.65 KiB | Viewed 301 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 21, 2021 6:04 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 683
Location: CCAA
ChrisysZero wrote:
Here is a sample pdf page attached, I need to get the red box/bounding box surrounding the content, this pdf is created externally not with MigraDoc or PDFsharp. So what I'm trying to achieve is to read/import the pdf with a single page and get the bounding box of its contents. Is this possible with MigraDoc?
As I wrote a few times in this thread: Cannot be done with MigraDoc or PDFsharp out of the box.

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 21, 2021 6:10 pm 
Offline

Joined: Mon Oct 18, 2021 3:20 pm
Posts: 5
TH-Soft wrote:
ChrisysZero wrote:
Here is a sample pdf page attached, I need to get the red box/bounding box surrounding the content, this pdf is created externally not with MigraDoc or PDFsharp. So what I'm trying to achieve is to read/import the pdf with a single page and get the bounding box of its contents. Is this possible with MigraDoc?
As I wrote a few times in this thread: Cannot be done with MigraDoc or PDFsharp out of the box.


OK thx so much for your patience and time Thomas.

Regards

Chris.


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