Skip to content

[syncfusion_flutter_pdf] Can not insert a page at specific index #2353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marcsanny opened this issue May 14, 2025 · 2 comments
Open

[syncfusion_flutter_pdf] Can not insert a page at specific index #2353

marcsanny opened this issue May 14, 2025 · 2 comments
Labels
pdf PDF component waiting for customer response Cannot make further progress until the customer responds.

Comments

@marcsanny
Copy link

marcsanny commented May 14, 2025

Bug description

We are not able to insert a page at specific index (add() method works as expected). We get: Null check operator used on a null value
It seems like the line is causing the error:

Image

Steps to reproduce

  1. Create document
  2. Insert page

Code sample

Minimal reproducible code:

Code sample
    final doc = PdfDocument();
    doc.pages.insert(0);
@LavanyaGowtham2021 LavanyaGowtham2021 added pdf PDF component open Open labels May 15, 2025
@irfanajaffer
Copy link

irfanajaffer commented May 16, 2025

Hi @marcsanny ,

Yes, it's possible to insert a PDF page at a specific index in a newly created PDF document using Syncfusion Flutter PDF package. There are a couple of ways to achieve this based on your requirements.

Method 1: Inserting an Empty Page at a Specific Index

You can insert an empty page at any specific index in a PDF document using the insert method:


//Create a new PDF document

PdfDocument document = PdfDocument();

 

//Add some initial content

document.pages.add().graphics.drawString(

    'First Page',

    PdfStandardFont(PdfFontFamily.helvetica, 20)

);

 

//Insert a new empty page at index 1 (second position)

document.pages.insert(1);

 

//Add content to the third page

document.pages.add().graphics.drawString(

    'Third Page',

    PdfStandardFont(PdfFontFamily.helvetica, 20)

);

 

//Save and dispose the PDF document

File('output.pdf').writeAsBytes(await document.save());

document.dispose();


Method 2: Inserting a Page from Another PDF Document

To insert a page from an existing PDF at a specific index in your document:


Future _insertPdf() async {

   //Load the existing PDF document

   final PdfDocument sourceDocument =

       PdfDocument(inputBytes: await _readPdfDocumentData('barcode.pdf'));

  

   //Get the page if you want to insert another PDF

   PdfPage page = sourceDocument.pages[0];

  

   //Create the page as template

   PdfTemplate template = page.createTemplate();

  

   //Load the target document

   PdfDocument destDocument =

       PdfDocument(inputBytes: await _readPdfDocumentData('invoice.pdf'));

  

   //Add new page to the document at a specific index (e.g., index 1)

   PdfPage destPage = destDocument.pages.insert(1);

  

   //Draw the template on the new page

   destPage.graphics

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

 

   //Save the document

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

  

   // Dispose both the documents

   destDocument.dispose();

   sourceDocument.dispose();

 

   //Save the file and launch/download

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

}

The key part is using document.pages.insert(index) to add an empty page at the specified index, or you can use it to insert and then immediately add content from another PDF document using templates.

To read PDF files from your Flutter project, you'll need to set up proper file handling as
shown in the included helper method:


Future<List<int>> _readPdfDocumentData(String name) async {

  final ByteData data = await rootBundle.load('assets/pdf/$name');

  return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);

}

This allows you to insert pages from existing PDFs at specific indexes in your newly created PDF document

@irfanajaffer
Copy link

Please share with us your complete requirements so that we can assist with you further in this

@chinnumuniyappan chinnumuniyappan added waiting for customer response Cannot make further progress until the customer responds. and removed open Open labels May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pdf PDF component waiting for customer response Cannot make further progress until the customer responds.
Projects
None yet
Development

No branches or pull requests

4 participants