Closed taihangg closed 2 months ago
By default, when updating a PDF document, we use incremental updates. This means that when pages are removed from an existing PDF file, the original content remains unchanged, and the updated content is added after it. As a result, the file size does not decrease when pages are removed. To resolve this, you can disable the incrementalUpdate property available in the FileStructure API.
Additionally, we recommend setting the cross-reference type to a stream instead of a table to further reduce the file size. Please refer to the following code example for more details.
``` //Disable the incremental update document.fileStructure.incrementalUpdate = false; //Set the cross reference type as cross reference stream document.fileStructure.crossReferenceType = PdfCrossReferenceType.crossReferenceStream; ```
For more information, please refer to the user guide and API documentation:
https://help.syncfusion.com/document-processing/pdf/pdf-library/flutter/working-with-document#performing-incremental-update-for-the-pdf-documentPlease try this solution, and if you continue to experience issues, we kindly request that you share the input PDF document with us. This will allow us to analyze the situation and provide further assistance.
Thank you! I have tried the example code, but it does not work as expected.
my code like this:
int startPage=3, endPage=5;
final _pdfDoc = PdfDocument(inputBytes: File(xxx).readAsBytesSync());
_pdfDoc!.fileStructure.incrementalUpdate = false;
_pdfDoc!.fileStructure.crossReferenceType =
PdfCrossReferenceType.crossReferenceStream;
for (int i = _pageCount! - 1; endPage! <= i; i--) {
_pdfDoc!.pages.removeAt(i);
}
for (int i = startPage - 2; 0 <= i; i--) {
_pdfDoc!.pages.removeAt(i);
}
await saveFile(newPath, _pdfDoc!.save());
_pdfDoc!.dispose();
my file like this: 成功家教启示录 -- 教育部关工委家教中心编写 -- 2001 -- 南京_南京大学出版社.pdf
We have reproduced the reported behavior and are currently analyzing it. Further details will be provided on September 10, 2024.
In the meantime, you can use our createTemplate method on the loaded page to achieve the desired optimized file size. Please refer to the following code snippet for guidance:
For more information on working with pages, kindly use the following link: Pages in Flutter PDF library | Syncfusion
int startPage = 3, endPage = 5;
//Load the existing the document
PdfDocument existingDocument =
PdfDocument(inputBytes: File(xxx).readAsBytesSync());
//Create a new PDF document
PdfDocument newDocument = PdfDocument();
//Initialize compression level
newDocument.compressionLevel = PdfCompressionLevel.normal;
//Set the cross-reference type as cross reference stream
newDocument.fileStructure.crossReferenceType =
PdfCrossReferenceType.crossReferenceStream;
//Set page margins to zero.
newDocument.pageSettings.margins.all = 0;
//Export the required pages to the new document.
for (int pageIndex = startPage - 1; pageIndex < endPage; pageIndex++) {
//Load the existing page as template.
PdfTemplate pageTemplate =
existingDocument.pages[pageIndex].createTemplate();
//Create a page in the new document.
PdfPage page = newDocument.pages.add();
//Draw the existing page template in the new document.
page.graphics
.drawPdfTemplate(pageTemplate, Offset.zero, page.getClientSize());
}
//Save
await saveFile(newPath, newDocument!.save());
//Dispose the documents
newDocument.dispose();
existingDocument.dispose();
Thank you very much! This works!
excuse me, can i close this issue now? or wait for a time.
We have confirmed the issue ”File size does not decrease after removing pages from the PDF document” as a defect in our product and fix will be included in our upcoming weekly release, which will be available on September 24, 2024.
Use the below feedback link to track the status of the reported bug. File size does not decrease after removing pages from the PDF document in Flutter | Feedback Portal (syncfusion.com)
Disclaimer: “Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.”
that's very good!thank you!
The fix for the reported issue "File size does not decrease after removing pages from the PDF document" has been included in our weekly NuGet release v27.1.50.
Kindly use the following link to refer to the latest release version,
when i removed some pages from pdf, then save it. the new file's size is almost the same as the old file's.