PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jan 20, 2025 7:55 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Dec 18, 2024 5:03 pm 
Offline

Joined: Tue Dec 17, 2024 11:56 pm
Posts: 8
With version 6.1.1, PdfPage.CropBox is defined like this:

Code:
    //
    // Summary:
    //     Gets or sets the crop box.
    public PdfRectangle CropBox
    {
        get
        {
            return base.Elements.GetRectangle("/CropBox", create: true);
        }
        set
        {
            base.Elements.SetRectangle("/CropBox", value);
        }
    }


This means that if the page does not specify a CropBox (a perfectly legal situation), the code incorrectly returns [0 0 0 0] which is not the correct value. According to the PDF specification CropBox is optional and if not specified, it defaults to the value of MediaBox.

There are 3 ways to fix this:
1. Have CropBox return null if the value isn't specified. This is the most correct, but would undoubtedly break much existing code.
2. Have CropBox return MediaBox if the value isn't specified. Probably same problem as fix #1
3. Update the documentation to explain what's happening and that code can call
Code:
page.Elements["/CropBox"]
to get the correct null value if CropBox isn't specified

The same things apply to BleedBox, ArtBox, and TrimBox.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 19, 2024 2:35 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3118
Location: Cologne, Germany
We will think about it.
In some cases you need the actual value of CropBox, in some cases you need the effective value which is inherited from MediaBox.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 19, 2024 3:20 pm 
Offline

Joined: Tue Dec 17, 2024 11:56 pm
Posts: 8
Yes, but what's being returned now is neither of those. Returning [0 0 0 0] is neither the actual value nor the effective value.

Also notice that the current implementation of CropBox.get does an implicit CropBox.set which is a coding mistake called "getter-setter confusion" or "implicit modification error".

Code like this is not at all rare and will cause different results in debug vs. release:

Code:
#if DEBUG
Debug.WriteLine("CropBox is currently " + page.CropBox);
#endif
if (page.Elements["/CropBox"] is null)
   page.CropBox = page.MediaBox;


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

All times are UTC


Who is online

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