PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 6:38 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: Sun Feb 04, 2024 12:13 am 
Offline

Joined: Sat Feb 03, 2024 11:37 pm
Posts: 2
Hello,

I need to read the metadata of PDF documents, it works fine with PdfSharp, but for some PDFs (see attached to reproduce the problem) the following warning is launched:
Quote:
Code:
warn: default category[0]
      Another instance of object 14532 0 R was found. Using previously encountered object instead.

with the number 14532 being "random" (it probably isn't that random, but by that I mean it changes with patological pdfs). This warning message always appears when at least two PDF files are involved, but for some PDFs their combination does not produce the message. Here's the code (F#) I use to produce this warning message (I use the NuGet version of PdfSharp 6.0.0):
Code:
open PdfSharp.Pdf.IO

let file1 = "Modern Computer Algebra.pdf"
let file2 = "Scientific Programming with Maxima.pdf"

let a = PdfReader.Open file1
let b = PdfReader.Open file2

Then, the terminal show this:
Quote:
Code:
warn: default category[0]
      Another instance of object 14532 0 R was found. Using previously encountered object instead.
warn: default category[0]
      Another instance of object 14532 0 R was found. Using previously encountered object instead.

If I only open one of the files, then no message appears. I tried to close (after use) the open pdfs (a.Close() and b.Close()) thinking it might cause the error, but apparently not.

I haven't found a way to silence this warning, nor to correct the problem it seems to raise, I need to read thousands of PDFs this way and the terminal displays hundreds of such warnings, which is quite annoying. What can I do to solve this "problem"?

PS: Here's a WeTransfer link for the files in question (they're too large for the forum)


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 05, 2024 10:00 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Simple answer: File "a" generates no warnings, file "b" generates two warnings.
Those are warnings, no error messages.

If you do not want to see warnings, configure the logger accordingly.

Code:
// Set a logger factory.
using var loggerFactory_ = LoggerFactory.Create(builder =>
{
    builder
        .AddFilter("PDFsharp", LogLevel.Error) // Suppress warnings.
        .AddConsole();
});
LogHost.Factory = loggerFactory_;

var a = PdfReader.Open(@"D:\Computer Algebra\Modern Computer Algebra.pdf");
var b = PdfReader.Open(@"D:\Computer Algebra\Scientific Programming with Maxima.pdf");


Sample code is C#, not F#.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 05, 2024 4:32 pm 
Offline

Joined: Sat Feb 03, 2024 11:37 pm
Posts: 2
Thank you for your reply, I was inattentive to the fact that this is indeed a double warning generated by a single PDF file.
I didn't know that PDFsharp used Microsoft.Extensions.Logging (actually, I didn't even know it existed). After some research, it turns out that the code you provided should instead be changed to this for the fix to be effective:
Code:
using var loggerFactory_ = LoggerFactory.Create(builder =>
{
    builder
        .AddFilter("default category", LogLevel.Error)
        .AddConsole();
});

Here's the F# version for those interested:
Code:
let loggerFactory = LoggerFactory.Create(fun builder -> builder.AddFilter("default category", LogLevel.Error).AddConsole() |> ignore)
LogHost.Factory <- loggerFactory


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