Open PDF with correct fileaccess and fileshare

Hi,

I am opening a PDF in the PdfViewer. I need the same filestream behaviour like Adobe Acrobat. Adobe Acrobat verwendet beim Öffnen von PDF-Dateien in der Regel FILE_SHARE_READ und FILE_SHARE_WRITE, aber sperrt die Datei, indem es das Flag FILE_SHARE_DELETE nicht setzt.

I tried to do the following in pdfViewer.DocumentLoaded, but it is locking the file completely, so I also cannot save changes, e.g. annotations, done in the PdfViewer. What I am doing wrong?

This is what I tried:

Dim fileStream = New FileStream(fileToLoad, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)


Kind regards, Marco


10 Replies

YM Yathavakrishnan Mohan Syncfusion Team January 23, 2025 12:02 PM UTC

 Hi Marco,

 

 

In the WPF PDFViewer, when the document is loaded as a stream, the "Save" button is disabled, and only the "Save As" option is available. This is the expected behavior.

 

 

 

To enable the "Save" option, the document must be loaded using the file path. If this solution meets your requirements, great. However, if your needs differ from our assumptions, please provide more detailshow you are trying to save the document. This will help us investigate the issue further and assist you more effectively.


Regards,
Yathavakrishnan M



MU Marco Uffelmann January 23, 2025 02:08 PM UTC

Hi Yathavakrishnan.

The problem is not that the SAVE BUTTON cannot be seen. It's about something completely different. I need to realize the following functionality:


A user exports a PDF document from a CRM to their local drive. This PDF document will now be opened and edited in a WPF application with the Syncfusion PdfViewer. The CRM checks every second whether the file (the PDF document) is still open by another program and therefore cannot be deleted. The check uses the Windows status flag.

This works well with Adobe Acrobat. Adobe Acrobat sets the Windows flag when opening the file and the file cannot be deleted by any other application. As soon as the file is closed in Adobe Acrobat, Adobe Acrobat removes the flag again. This is recognized by the CRM and the changed file (PDF document) is automatically imported.

With the PdfViewer the flag is not set when opening a PDF file. I can delete the PDF in the file system even though the file is open in PdfViewer.

My question now is: How can I open a PDF file in the PdfViewer so that no other application can delete or change the file as long as the file is open in the PdfViewer? BUT: In PdfViewer the user can edit the file, e.g. sign the file, and save the file using the save button. After he closed the changed document the CRM will recognize and import.

This is the usual idea behind opening a file with setting:

FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite

A file can be opened, with access to read and write. Fileshare is also read and write, but not delete.


Thank you for your help.

Best regards, Marco



KG Krithika Ganesan Syncfusion Team January 24, 2025 12:51 PM UTC

Hi Marco,

 

We have reproduced the reported behavior in the WPF PdfViewer. We will analyze this further and provide an update by January 28, 2025.

 

Regards,

Krithika



MU Marco Uffelmann January 29, 2025 08:55 AM UTC

Hi Krithika,


Any news on this?


Kind regards, Marco



KG Krithika Ganesan Syncfusion Team January 29, 2025 11:51 AM UTC

Hi Marco,

 

Currently in WPF PdfViewer, the PDF is loaded by creating a copy of the document. This approach ensures that even if the PdfViewer is not present in the UI, the PDF can still be printed or used for text extraction.

 

Regards,

Krithika



MU Marco Uffelmann January 29, 2025 11:56 AM UTC

Hi  Krithika,


ok. But the original document must be flagged correctly. Otherwise other applications, including virus scanners and security applications, are not working correctly.

Can you chnage that behaviour?


Regards, Marco



YM Yathavakrishnan Mohan Syncfusion Team January 30, 2025 07:55 AM UTC

Hi Marco,

In WPF, the PDFViewer has 'DocumentLoaded' and 'DocumentUnloaded' events. These events are triggered when a document is loaded or unloaded from the PDFViewer. Kindly check if it is possible to add a tag to the document during these events? Please try adding the tag in your sample and let us know the result of the process. We have attached the code snippets for the loaded and unloaded events.
 

    //Event trigger when load the document

    PDFViewer.DocumentLoaded += PDFViewer_DocumentLoaded;

 

    //Event trigger when unload the document

    PDFViewer.DocumentUnloaded += PDFViewer_DocumentUnloaded;

 

 

private void PDFViewer_DocumentLoaded(object sender, EventArgs e)

{

   //check the possibilty of add the tag here

}

 

private void PDFViewer_DocumentUnloaded(object sender, EventArgs args)

{

   //check the possiblity of remove the tag here

}


Regards,

Yathavakrishnan M



MU Marco Uffelmann January 31, 2025 10:17 AM UTC

Hi Yathavakrishnan,

No, this is not the solution. Using PDFViewer I need to open the file, edit the file, save the changes and close the file.

With your solution I cannot save the changed file.

The only solution is that PdfViewer opens a file correctly setting the tags in Windows. During closing the file the tags must be set also. This is how usually all Windows apllication should handle open files.


Any ideas?


Regards, Marco



MU Marco Uffelmann January 31, 2025 10:17 AM UTC

Hi Yathavakrishnan,

No, this is not the solution. Using PDFViewer I need to open the file, edit the file, save the changes and close the file.

With your solution I cannot save the changed file.

The only solution is that PdfViewer opens a file correctly setting the tags in Windows. During closing the file the tags must be set also. This is how usually all Windows apllication should handle open files.


Any ideas?


Regards, Marco



YM Yathavakrishnan Mohan Syncfusion Team February 3, 2025 12:54 PM UTC

Hi Marco,
 

When opening the PDF in the WPF PDFViewer, we create a stream for the file path obtained from the PDFViewer during the 'DocumentLoaded'event. This locks the file, preventing access by other applications. The stream is then disposed of when the document is unloaded during the  'DocumentUnloaded event. Similarly, we handle the save operation in the same way. Please refer to the code snippet below for your reference, and we have also attached the sample for your reference.
 

    //Event will trigger when load the document

    PDFViewer.DocumentLoaded += PDFViewer_DocumentLoaded;

    //Event will trigger when unload the documnet

    PDFViewer.DocumentUnloaded += PDFViewer_DocumentUnloaded;

 

    //Event will trigger before save operation

    PDFViewer.BeginSave += PDFViewer_BeginSave;

    //Event will trigger after save operation

    PDFViewer.EndSave += PDFViewer_EndSave;

}

 

private void PDFViewer_EndSave(object sender, EndSaveEventArgs e)

{

    LockFile();

}

 

private void PDFViewer_BeginSave(object sender, BeginSaveEventArgs e)

{

    UnLockFile();

}

 

private void PDFViewer_DocumentLoaded(object sender, EventArgs args)

{

    LockFile();

}

private void PDFViewer_DocumentUnloaded(object sender, EventArgs e)

{

    UnLockFile();

}

 

private void LockFile()

{

    //Get the filepath from PDFViewer

    string fullPath = PDFViewer.DocumentInfo.FilePath + PDFViewer.DocumentInfo.FileName;

    //Create a stream

    lockedFileStream = new FileStream(fullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);

}

 

private void UnLockFile()

{

    if (lockedFileStream != null)

    {

        //Close and dispose the stream

        lockedFileStream.Close();

        lockedFileStream.Dispose();

        lockedFileStream = null;

    }

}


Regards,
Yathavakrishnan M
 


Attachment: WPF_PDFViewer_Sample_efda01c7.zip

Loader.
Up arrow icon