We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Pdf when saving multiple times increase the size of the file

Hi all,

I'm trying to use the Pdfviewer and saving from the pdfviewer on a simple pdf without any fields, and every time the pdf is saved the size increased, we want to be able to when saving not increase the size the pdf unless there are some changes on the pdf, is that possible?

This code is the one we use just to save the same file multiples


Snippet
protected virtual async Task SaveDocumentAsync()
	{
		await PdfViewerElement.SaveAsync(FilePath);
		var filepath = FilePath.Replace(".pdf""99.pdf");
		for (int i = 0; i <= 2; i++)
		{
			await PdfViewerElement.SaveAsync(filepath);
			Task.Delay(100);
			await PdfViewerElement.LoadAsync(filepath);
			Task.Delay(100);
		}
 
		//await SaveDocumentSignaturesAsync(FilePath);
		//await PdfViewerElement.UnloadAsync();
 
		await Task.CompletedTask;
	}



2 Replies

VV Visvesvar Venkatesan Syncfusion Team February 17, 2023 12:32 PM UTC

We were able to reproduce the reported issue  “Saving multiple times increases the size of the file” and suspect this to be a defect. We will analyze further on this and update you with more details in Two business days.



VV Visvesvar Venkatesan Syncfusion Team February 21, 2023 12:42 PM UTC

In general, downloading the PDF document will increase its size due to the instances stored for the actions performed in the document. The same behavior occurs in Adobe Reader too.


However, to restrict this file size change you can make use of the below code snippet. This highlighted code will skip the changes made in the document on every save operation. This code need to be handled on save operation.


Code Snippet:


        byte[] byteArray;
        byteArray = await viewerInstance.GetDocumentAsync();
        string base64String = Convert.ToBase64String(byteArray);
        PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray);
        Stream stream = new MemoryStream();
        
        string base64string = Convert.ToBase64String((stream as MemoryStream).ToArray());
        loadedDocument.EnableMemoryOptimization = true;
        loadedDocument.FileStructure.IncrementalUpdate = false;
        loadedDocument.Save(stream); 
        loadedDocument.Close(true);
        File.WriteAllBytes("D:\\task\\blazor-sample\\blazor-sample\\blazor-sample\\wwwroot\\Data\\output.pdf", (stream as MemoryStream).ToArray());


By setting the incremental update of the file structure to false, you can restrict the incremental file size.


Kindly find the documentation for restricting the incremental file size.


Documentation: https://help.syncfusion.com/file-formats/pdf/working-with-document#performing-incremental-update-for-pdf-document


Kindly try this and let us know if you have any concerns.


Loader.
Live Chat Icon For mobile
Up arrow icon