Saving PDF Files in WPF Pdf Viewer

17 Dec 2020 / 4 minutes to read

The PDF Viewer allows you to save the PDF document that is being displayed. It helps you to keep the file up to date with any modifications and prevent your work from being lost. You can save the changes in the PDF document using the “Save” and “Save As” options available in the toolbar.

Save PDF files

NOTE

The “Save” option in the toolbar will not be enabled if the file is not edited, or if the file is loaded using the Stream and PdfLoadedDocument objects. In these cases, you can use the “Save As” option.

You can also call the Save method to silently save the modified PDF file to a specific path in the local disk. Refer to the following code to perform the same from the application level.

Private void SavePDF()
{
    //Save the PDF file to a specific file path after doing any modifications by passing the file name as a parameter to the `Save` method.
    pdfViewer.Save("Saved.pdf");
}
Private Sub SavePDF()
    'Save the PDF file to a specific file path after doing any modifications by passing the file name as a parameter to the Save method.
    pdfViewer.Save("Saved.pdf")
End Sub

Events

The PdfViewerControl notifies you at the start and end of the save operation using the BeginSave and EndSave events respectively.

Begin Save

The BeginSave event occurs before initiating the save operation of the PDF file. It also allows you to cancel the save operation using the Cancel property of BeginSaveEventArgs. The following code shows how to wire the event in the PdfViewerControl.

namespace SaveEvents
{
	public partial class MainWindow : Window
	{
		#region Constructor
		public MainWindow()
		{
			InitializeComponent();

			//Wire the `BeginSave` event.
			PdfViewer.BeginSave += PdfViewer_BeginSave;

			//Load the PDF file
			PdfViewer.Load("../../Data/Windows Store Apps Succinctly.pdf");
		}
		#endregion

		#region Events
		private void PdfViewer_BeginSave(object sender, BeginSaveEventArgs e)
		{
			//Insert your code here
		}
		#endregion
	}
}

End Save

The EndSave event occurs after the completion of the save operation. The IsCanceled property of the EndSaveEventArgs helps you to know whether the save operation is canceled or not. The following code shows how to wire the event in the PdfViewerControl.

namespace SaveEvents
{
    public partial class MainWindow : Window
    {
        #region Constructor
        public MainWindow()
        {
            InitializeComponent();

            //Wire the `EndSave` event.
            PdfViewer.EndSave += PdfViewer_EndSave;

            //Load the PDF file
            PdfViewer.Load("../../Data/Windows Store Apps Succinctly.pdf");
        }
        #endregion

        #region Events
        private void PdfViewer_EndSave(object sender, EndSaveEventArgs e)
        {
            //Insert your code here
        }
        #endregion
    }
}

Keyboard shortcuts

The following keyboard shortcuts can be used to save the files when the toolbar is enabled:

  • Ctrl + S: If the “Save” option is enabled in the PDF Viewer, it performs “Save” in the PDF document. Else, it performs “Save As” in the PDF document.
  • Shift + Ctrl + S: Performs “Save As” in the PDF document.