Pages in Flutter PDF

5 May 20217 minutes to read

Adding a new page to the PDF document

The following code sample explains how to add a PdfPage to a PDF document. When multiple pages are added, the new page will always be added to the end of the document.

  • dart
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Create a new PDF page and draw the text
    document.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
        brush: PdfBrushes.darkBlue, bounds: const Rect.fromLTWH(170, 100, 0, 0));
    
    //Save and dispose the PDF document
    File('Output.pdf').writeAsBytes(document.save());
    document.dispose();

    Inserting pages in an existing document

    You can insert an empty page at any location in the existing PDF document using the insert method. The following code sample explains the same.

  • dart
  • //Loads an existing PDF document
    PdfDocument document =
        PdfDocument(inputBytes: File('input.pdf').readAsBytesSync());
      
    //Inserts a new page at index 0
    document.pages.insert(0);
    
    //Saves the document
    File('output.pdf').writeAsBytes(document.save());
    
    //Disposes the document
    document.dispose();

    Adding margin to the PDF pages

    You can add margin to all the PDF pages of a PDF document using the pageSettings property. The following code snippet explains the same.

  • dart
  • //Create a new PDF documentation
    PdfDocument document = PdfDocument();
    
    //Set margin for all the pages
    document.pageSettings.margins.all = 200;
    
    //Draw the text by adding page to the document
    document.pages.add().graphics.drawString(
        'Hello World!!!', PdfStandardFont(PdfFontFamily.helvetica, 27),
        brush: PdfBrushes.darkBlue);
    
    //Save and dispose the PDF document
    List<int> bytes = document.save();
    document.dispose();

    Adding sections with different page settings

    Flutter PDF supports adding sections with different page settings such as margins, orientation, rotate, size. You can add sections to a PDF document by using the PdfSection available in PdfDocument instance and create page settings to the PdfSection using the pageSettings property.

    The following code snippet explains how to add more sections to a PDF document with different page settings.

  • dart
  • //Create a PDF document
    PdfDocument document = PdfDocument();
    
    //Set the font
    PdfFont font = PdfStandardFont(PdfFontFamily.helvetica, 14);
    
    //Section - 1
    //Add section to the document
    PdfSection section = document.sections!.add();
    
    //Create page settings to the section
    section.pageSettings.rotate = PdfPageRotateAngle.rotateAngle0;
    section.pageSettings.size = const Size(300, 400);
    
    //Draw simple text on the page
    section.pages.add().graphics.drawString('Rotated by 0 degrees', font,
        brush: PdfBrushes.black, bounds: const Rect.fromLTWH(20, 20, 0, 0));
    
    //Section - 2
    //Add section to the document
    section = document.sections!.add();
    
    //Create page settings to the section
    section.pageSettings.rotate = PdfPageRotateAngle.rotateAngle90;
    section.pageSettings.size = const Size(300, 400);
    
    //Draw simple text on the page
    section.pages.add().graphics.drawString('Rotated by 90 degrees', font,
        brush: PdfBrushes.black, bounds: const Rect.fromLTWH(20, 20, 0, 0));
    
    //Section - 3
    //Add section to the document
    section = document.sections!.add();
    
    //Create page settings to the section
    section.pageSettings.rotate = PdfPageRotateAngle.rotateAngle180;
    section.pageSettings.size = const Size(500, 200);
    
    //Draw simple text on the page
    section.pages.add().graphics.drawString('Rotated by 180 degrees', font,
        brush: PdfBrushes.black, bounds: const Rect.fromLTWH(20, 20, 0, 0));
    
    //Section - 4
    //Add section to the document
    section = document.sections!.add();
    
    //Create page settings to the section
    section.pageSettings.rotate = PdfPageRotateAngle.rotateAngle270;
    section.pageSettings.size = const Size(300, 200);
    
    //Draw simple text on the page
    section.pages.add().graphics.drawString('Rotated by 270 degrees', font,
        brush: PdfBrushes.black, bounds: const Rect.fromLTWH(20, 20, 0, 0));
    
    //Save and dispose the PDF document
    File('Output.pdf').writeAsBytes(document.save());
    document.dispose();

    Get the number of pages from a PDF document

    You can get the page count from the existing PDF document as shown in the following code sample.

  • dart
  • //Loads an existing PDF document
    PdfDocument document =
        PdfDocument(inputBytes: File('input.pdf').readAsBytesSync());
    
    //Gets the pages count
    int pageCount = document.pages.count;
    
    //Disposes the document
    document.dispose();

    Rotating a PDF page

    You can rotate a PDF page in the PDF document using the PdfPageRotateAngle enum as shown in the following code snippet.

  • dart
  • //Create a PDF document
    PdfDocument document = PdfDocument();
    
    //Add section to the document
    PdfSection section = document.sections!.add();
    
    //Create page settings to the section
    section.pageSettings.rotate = PdfPageRotateAngle.rotateAngle180;
    section.pageSettings.size = PdfPageSize.a4;
    
    //Draw simple text on the page
    section.pages.add().graphics.drawString(
        'Rotated by 180 degrees', PdfStandardFont(PdfFontFamily.helvetica, 14),
        brush: PdfBrushes.black, bounds: const Rect.fromLTWH(20, 20, 0, 0));
    
    //Add section to the document
    section = document.sections!.add();
    
    //Create page settings to the section
    section.pageSettings.rotate = PdfPageRotateAngle.rotateAngle270;
    section.pageSettings.size = PdfPageSize.a4;
    
    //Draw simple text on the page
    section.pages.add().graphics.drawString(
        'Rotated by 270 degrees', PdfStandardFont(PdfFontFamily.helvetica, 14),
        brush: PdfBrushes.black, bounds: const Rect.fromLTWH(20, 20, 0, 0));
    
    //Save and dispose the PDF document
    File('Output.pdf').writeAsBytes(document.save());
    document.dispose();

    Removing pages from a document

    You can remove the pages from the existing PDF document using the remove or removeAt method as shown in the following code sample.

  • dart
  • //Loads an existing PDF document
    PdfDocument document =
        PdfDocument(inputBytes: File('input.pdf').readAsBytesSync());
     
    //Gets the second page of the document
    PdfPage page = document.pages[1];
    
    //Removes the second page from the document
    document.pages.remove(page);
    
    //Removes the first page from the document
    document.pages.removeAt(0);
    
    //Saves the document
    File('output.pdf').writeAsBytes(document.save());
    
    //Disposes the document
    document.dispose();