Merge Pdf

Hi,


Is it possible to merge pdf documents or add existing pages from other pdf documents? Didn't find anything in the documentation.


1 Reply

JT Jeyalakshmi Thangamarippandian Syncfusion Team May 28, 2024 09:55 AM UTC

Hi Ziad Ghanem,

At present we do not have support to merge PDF documents in Flutter. We have added it to our feature request list, and it will be implemented in any of our upcoming releases.

Feedback link: https://www.syncfusion.com/feedback/25644/add-support-to-merge-pdf-documents

However, as a workaround solution, we can create the existing PDF page as a template and draw it to another PDF document. We have shared the code for your reference.

List<String> files = ['input.pdf', 'invoice.pdf'];

PdfDocument newDocument = PdfDocument();

PdfSection? section;

 for (String file in files) {

   // Load the PDF document.

    PdfDocument loadedDocument = PdfDocument(inputBytes: await _readData(file));

   // Export the pages to the new document.

   for (int index = 0; index < loadedDocument.pages.count; index++) {

      // Get the page template.

      PdfTemplate template = loadedDocument.pages[index].createTemplate();

      // Create a new section if the page settings are different.

      if (section == null || section.pageSettings.size != template.size) {

        section = newDocument.sections!.add();

        section.pageSettings.size = template.size;

        section.pageSettings.margins.all = 0;

     }

      // Draw the page template to the new document.

      section.pages

          .add()

          .graphics

          .drawPdfTemplate(template, const Offset(0, 0));

   }

    // Dispose the loaded document.

    loadedDocument.dispose();

 }

    //Save the document.

    List<int> bytes = await newDocument.save();

 

    //Disposes the document

    newDocument.dispose();

  

    //Save the file and launch/download.

    SaveFile.saveAndLaunchFile(bytes, 'output.pdf');


Please refer to the UG documentation for further details:

Templates in Flutter PDF library | Syncfusion


Note: By moving this template approach, interactive elements such as annotations, form fields will be removed to the newly created PDF document.


Regards,

Jeyalakshmi T



Loader.
Up arrow icon