PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Jun 14, 2022 12:31 am 
Offline

Joined: Tue Jun 14, 2022 12:19 am
Posts: 2
We have some code that has been running for years that creates PDFs from a SSRS Report. We've created literally millions of documents thru this code.

Recently on our development server, we started getting sporadic "System.IO.IOException: The process cannot access the file 'xx786801.pdf' because it is being used by another process" messages on PdfSharp.Pdf.PdfDocument.Save Yes! We are using a unique file name, and this process has been working for years. No code changes; no hardware changes; and this is sporadic. Tried a reboot. The process will create 50-75 documents and then get this exception. we can continue with another 50-70 and another exception.

The code creating the document looks like this:
Code:
Dim stream As System.IO.FileStream = System.IO.File.Create(OutDir + FileName, result.Length)
        stream.Write(result, 0, result.Length)
        stream.Close()
        stream.Dispose()



And then we run a function that locks the PDF that looks like this:
Code:
 
        Dim document As PdfSharp.Pdf.PdfDocument = PdfReader.Open(Filename, MyPW)
        Dim SecuritySettings As PdfSharp.Pdf.Security.PdfSecuritySettings = document.SecuritySettings

        If PasswordRequiredOnOpen = True Then
            SecuritySettings.UserPassword = MyPW
        End If

        ' setting the owner password will restrict things like editing and printing
        SecuritySettings.OwnerPassword = MyPW
        SecuritySettings.PermitAccessibilityExtractContent = False
        SecuritySettings.PermitAnnotations = False
        SecuritySettings.PermitAssembleDocument = False
        SecuritySettings.PermitExtractContent = False
        SecuritySettings.PermitFormsFill = False
        SecuritySettings.PermitFullQualityPrint = True
        SecuritySettings.PermitModifyDocument = False
        SecuritySettings.PermitPrint = True

        document.Save(Filename)
        document.Close()
        document.Dispose()





Just not too sure where to even start on this. Any ideas?

Dan


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 14, 2022 8:23 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!

Why not use a MemoryStream to save the "file", then read the PDF from the MemoryStream, lock it, then create a file on disk.

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 14, 2022 9:45 am 
Offline

Joined: Tue Jun 14, 2022 12:19 am
Posts: 2
I’m not soliciting for new ways to write the code. I’m trying to figure out why, after a million documents, I’ve started throwing sporadic exceptions.


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 14, 2022 11:02 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
BigDanSRQ wrote:
I’m trying to figure out why, after a million documents, I’ve started throwing sporadic exceptions.
I can only speculate about that.
Check what has changed at the time when exceptions started to appear.

New anti-virus software?
New .NET version?
Windows updates?

You write a file, close it, open it again, write it again.
AV software will check every newly created file. This could be the problem. And it is slow.
Maybe garbage collection or background tasks/clean-up tasks have changed in .NET or Windows.

You can invoke Save with a stream. Before calling Save, you can try to open the file in a loop so you can re-try when the exception occurs.

The redundant Open is suddenly causing problems. Best to phase out the redundant Open and speed up the process IMHO.

_________________
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  [ 4 posts ] 

All times are UTC


Who is online

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