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 · 4 comments
Open

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

marcsanny opened this issue May 14, 2025 · 4 comments
Labels
open Open pdf PDF component

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
@marcsanny
Copy link
Author

Hi @irfanajaffer, thanks for your response :).

In my use case I had to draw an image outside the margins. I finally achieved what I needed with templates

  1. Create template for every page
  2. Create new document without margins
  3. For every template add a new page and draw this template
  4. If this is the page where I want to add the image, I additionally draw this image

My pseudo code for that looks like this:

    // Get templates from pages.
    final templates = [
      for (int index = 0; index < document.pages.count; index++)
        document[index].createTemplate(),
    ];

    // Draw pages and image on a new document without margins.
    final documentWithoutMargins = PdfDocument()..pageSettings.setMargins(0);
    for (int index = 0; index < templates.length; index++) {
      final template = templates[index];

      final page = documentWithoutMargins.pages.add();
      page.graphics.drawPdfTemplate(template, Offset.zero);
      if (index == pageIndex) {
        // Draw image on page.
        page.layers.add(name: layerName).graphics.drawImage(
              PdfBitmap(properties.imageData),
              properties.rectForImage(page.size),
            );
      }
    }

We can close this issue, but the actual reason I created it was that insert() was not working for me... Maybe it was because I wasn't using the latest version, will try soon with the update.

@chinnumuniyappan chinnumuniyappan added open Open and removed waiting for customer response Cannot make further progress until the customer responds. labels May 23, 2025
@irfanajaffer
Copy link

Hi @marcsanny ,

Thank you for providing the feedback. Please let us know if you need any further assistance.

Regards,
Irfana J.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open Open pdf PDF component
Projects
None yet
Development

No branches or pull requests

4 participants