PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 11:26 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Oct 18, 2023 11:02 am 
Offline

Joined: Wed Oct 18, 2023 10:54 am
Posts: 7
The moment I try to open a readonly pdf document with the following method:

sourceDocument = PdfReader.Open(file, PdfDocumentOpenMode.ReadOnly);

I get the error
System.InvalidOperationException: 'The document cannot be modified.'
This so far only occorured with 1 single pdf file

The pdf I'm trying to open is the following pdf
https://dlcdnets.asus.com/pub/ASUS/mb/L ... V2_WEB.pdf

Am I doing something wrong or..?

with regards SomePie


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 18, 2023 11:30 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
SomePie wrote:
Am I doing something wrong or..?
Well, the file is inconsistent: The PDF version is set to 1.7, but the encryption level is set to 5.
Encryption level 5 came with PDF 2.0, therefore PDFsharp tries to change the version to 2.0 - and this fails.

I think I'll change PDFsharp to not adjust the incorrect version number for files opened in ReadOnly mode. Will discuss this with my fellow worker.

Thanks for your feedback.
As a workaround, you could open the file in "modify" mode until the next release comes.

Update: File cannot be opened in Import mode, this leads to a different error message. I'll discuss this with my fellow worker, too.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 18, 2023 11:36 am 
Offline

Joined: Wed Oct 18, 2023 10:54 am
Posts: 7
Thomas Hoevel wrote:
SomePie wrote:
Am I doing something wrong or..?
Well, the file is inconsistent: The PDF version is set to 1.7, but the encryption level is set to 5.
Encryption level 5 came with PDF 2.0, therefore PDFsharp tries to change the version to 2.0 - and this fails.

I think I'll change PDFsharp to not adjust the incorrect version number for files opened in ReadOnly mode.

Thanks for your feedback.
As a workaround, you could open the file in "modify" mode until the next release comes.


Thanks for your quick reply, though the modify mode would only be possible if I had the actual password for this one

with regards,
SomePie


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 18, 2023 11:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
SomePie wrote:
Thanks for your quick reply, though the modify mode would only be possible if I had the actual password for this one
I already noticed that.
Will discuss this with my fellow worker. Encryption is not my area of expertise.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 18, 2023 12:00 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
I made a change to SetRequiredVersion in PdfDocument.
Here's the change if you want to try it out:

Code:
        /// <summary>
        /// Adjusts the version if the current version is lower than the required version.
        /// Version is not adjusted for inconsistent files in ReadOnly mode.
        /// </summary>
        /// <param name="requiredVersion">The minimum version number to set version to.</param>
        /// <returns>True, if Version was modified.</returns>
        public bool SetRequiredVersion(int requiredVersion)
        {
            if (requiredVersion > Version && CanModify)
            {
                Version = requiredVersion;
                return true;
            }

            return false;
        }

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Oct 18, 2023 1:26 pm 
Offline

Joined: Wed Oct 18, 2023 10:54 am
Posts: 7
Thomas Hoevel wrote:
I made a change to SetRequiredVersion in PdfDocument.
Here's the change if you want to try it out:

Code:
        /// <summary>
        /// Adjusts the version if the current version is lower than the required version.
        /// Version is not adjusted for inconsistent files in ReadOnly mode.
        /// </summary>
        /// <param name="requiredVersion">The minimum version number to set version to.</param>
        /// <returns>True, if Version was modified.</returns>
        public bool SetRequiredVersion(int requiredVersion)
        {
            if (requiredVersion > Version && CanModify)
            {
                Version = requiredVersion;
                return true;
            }

            return false;
        }


I'll pull the repo later on this week and try this change in it! thank you


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 20, 2023 10:33 am 
Offline

Joined: Wed Oct 18, 2023 10:54 am
Posts: 7
Thomas Hoevel wrote:
I made a change to SetRequiredVersion in PdfDocument.
Here's the change if you want to try it out:

Code:
        /// <summary>
        /// Adjusts the version if the current version is lower than the required version.
        /// Version is not adjusted for inconsistent files in ReadOnly mode.
        /// </summary>
        /// <param name="requiredVersion">The minimum version number to set version to.</param>
        /// <returns>True, if Version was modified.</returns>
        public bool SetRequiredVersion(int requiredVersion)
        {
            if (requiredVersion > Version && CanModify)
            {
                Version = requiredVersion;
                return true;
            }

            return false;
        }


I downloaded the repo, added the change in
'PdfSharp\src\foundation\src\PDFsharp\src\PdfSharp\Pdf\PdfDocument.cs'

built the dll, replaced it in the debug of my project after rebuilding, same error still comes up

did I miss a step?

[edit]
When I try
Code:
form = XPdfForm.FromFile(file);

and then read the form.PageCount the same error applies
[/edit]


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 414 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