[]
        
(Showing Draft Content)

PrintManager

Class PrintManager

java.lang.Object
com.grapecity.documents.excel.PrintManager
All Implemented Interfaces:
Serializable

public class PrintManager extends Object implements Serializable
Represents a print manager for pagination and PDF print output.

Use this class to generate PageInfo objects for a workbook or worksheet, adjust pagination results, and save the resulting pages to PDF. It is the entry point for custom pagination workflows such as controlling page breaks, keeping content together, and combining content from one or more workbooks into a single PDF output.


 worksheet.getRange("A1:B4").setValue(new Object[][] {
     {"Name", "Value"},
     {"A", 100},
     {"B", 200},
     {"C", 300}
 });
 PrintManager printManager = new PrintManager();
 List<PageInfo> pages = printManager.paginate(worksheet);
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 printManager.savePageInfosToPDF(stream, pages);
 
  • Constructor Details

    • PrintManager

      public PrintManager()
  • Method Details

    • finalize

      protected void finalize() throws Throwable
      Releases internal font collection resources before this print manager is garbage collected.

      This implementation clears each cached font collection in the internal collection and then calls super.finalize().

      This method is invoked by the JVM finalization mechanism and should not be called directly from user code.

      Overrides:
      finalize in class Object
      Throws:
      Throwable - If an error occurs while finalizing this object or while invoking super.finalize().
    • saveWorkbooksToPDF

      public final void saveWorkbooksToPDF(OutputStream stream, List<IWorkbook> workbooks)
      Saves the pages generated from the specified workbooks to a PDF output stream.

      This method paginates the workbooks in list order by calling paginate(List) and then writes the generated pages to the specified output stream by calling savePageInfosToPDF(OutputStream,List).

      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       IWorkbook secondWorkbook = workbook.clone();
       secondWorkbook.getWorksheets().get(0).getRange("B2").setValue("Summary");
       List<IWorkbook> workbooks = new ArrayList<IWorkbook>();
       workbooks.add(workbook);
       workbooks.add(secondWorkbook);
       PrintManager printManager = new PrintManager();
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       printManager.saveWorkbooksToPDF(stream, workbooks);
       
      Parameters:
      stream - The output stream that receives the generated PDF data.
      workbooks - The workbooks to paginate and export. The workbooks are processed in the order they appear in the list.
    • saveWorkbooksToPDF

      public final void saveWorkbooksToPDF(OutputStream stream, List<IWorkbook> workbooks, PdfSaveOptions options)
      Saves the pages generated from the specified workbooks to a PDF output stream.

      The workbooks are paginated in list order, and the generated pages are written to the supplied stream by calling savePageInfosToPDF(OutputStream,List,PdfSaveOptions).

      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       PrintManager printManager = new PrintManager();
       List<IWorkbook> workbooks = new ArrayList<IWorkbook>();
       workbooks.add(workbook);
       IWorkbook secondWorkbook = workbook.clone();
       secondWorkbook.getWorksheets().get(0).getRange("A1").setValue("Second workbook");
       workbooks.add(secondWorkbook);
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       printManager.saveWorkbooksToPDF(stream, workbooks, new PdfSaveOptions());
       
      Parameters:
      stream - The output stream to write the PDF data to. Must not be null.
      workbooks - The workbooks to paginate and export. The workbooks are processed in the order they appear in the list.
      options - The PDF save options. May be null.
    • saveWorkbooksToPDF

      public final void saveWorkbooksToPDF(String fileName, List<IWorkbook> workbooks)
      Saves data from different workbooks to the specified PDF file.

      Each workbook in the collection is paginated, and all generated pages are written to the PDF. This overload passes null for PdfSaveOptions to the three-argument overload.

      
       worksheet.getRange("A1").setValue("First workbook");
       IWorkbook secondWorkbook = workbook.clone();
       secondWorkbook.getWorksheets().get(0).getRange("A1").setValue("Second workbook");
       PrintManager printManager = new PrintManager();
       List<IWorkbook> workbooks = Arrays.asList(workbook, secondWorkbook);
       printManager.saveWorkbooksToPDF("path/to/workbooks.pdf", workbooks);
       
      Parameters:
      fileName - The path and name of the PDF file to create. Must be a valid file path; null or an invalid path causes an IllegalArgumentException.
      workbooks - The collection of Workbook objects to save. This value is passed to the underlying save operation without additional validation by this overload.
      Throws:
      IllegalArgumentException - if fileName is not a valid path.
      RuntimeException - if an I/O error occurs while creating or writing the PDF file.
    • saveWorkbooksToPDF

      public final void saveWorkbooksToPDF(String fileName, List<IWorkbook> workbooks, PdfSaveOptions options)
      Saves the datas from different workbooks to the specified pdf file.
      
       worksheet.getRange("A1").setValue("First workbook");
       IWorkbook secondWorkbook = workbook.clone();
       secondWorkbook.getWorksheets().get(0).getRange("A1").setValue("Second workbook");
       PrintManager printManager = new PrintManager();
       List<IWorkbook> workbooks = Arrays.asList(workbook, secondWorkbook);
       PdfSaveOptions options = new PdfSaveOptions();
       printManager.saveWorkbooksToPDF("path/to/workbooks.pdf", workbooks, options);
       
      Parameters:
      fileName - The specified pdf file.
      workbooks - The workbook collection.
      options - Options for saving pdf file.
    • savePageInfosToPDF

      public final void savePageInfosToPDF(OutputStream stream, List<PageInfo> pages)
      Saves the page datas to the specified pdf file stream.
      
       worksheet.getPageSetup().setPrintArea("A1:B4");
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       printManager.savePageInfosToPDF(stream, pages);
       
      Parameters:
      stream - The specified pdf file.
      pages - The page information collection.
    • savePageInfosToPDF

      public final void savePageInfosToPDF(OutputStream stream, List<PageInfo> pages, PdfSaveOptions options)
      Saves the specified page information collection to a PDF output stream.

      The pages collection should be generated by paginate(IWorksheet). Passing a null or empty collection causes an IllegalStateException.

      
       worksheet.getPageSetup().setPrintArea("A1:B4");
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
           printManager.savePageInfosToPDF(stream, pages, new PdfSaveOptions());
       } catch (Exception e) {
       }
       
      Parameters:
      stream - The output stream to write the PDF data to. Must not be null.
      pages - The page information collection to export. Must contain at least one page; null or an empty list causes an IllegalStateException.
      options - The PDF save options. Can be null.
      Throws:
      IllegalStateException - if pages is null or empty.
    • savePageInfosToPDF

      public final void savePageInfosToPDF(String fileName, List<PageInfo> pages)
      Saves the specified page information collection to a PDF file.

      This overload forwards null for the PdfSaveOptions argument to savePageInfosToPDF(String,List,PdfSaveOptions).

      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(workbook);
       printManager.savePageInfosToPDF("path/to/pages.pdf", pages);
       
      Parameters:
      fileName - The path of the PDF file to create. Must be a valid file path.
      pages - The page information collection to save.
      Throws:
      IllegalArgumentException - if fileName is not a valid path.
    • savePageInfosToPDF

      public final void savePageInfosToPDF(String fileName, List<PageInfo> pages, PdfSaveOptions options)
      Saves the specified page information collection to a PDF file.

      This method writes the supplied PageInfo objects to the PDF file identified by fileName. The options argument is passed to savePageInfosToPDF(OutputStream,List,PdfSaveOptions).

      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(workbook);
       PdfSaveOptions options = new PdfSaveOptions();
       printManager.savePageInfosToPDF("path/to/pages.pdf", pages, options);
       
      Parameters:
      fileName - The path of the PDF file to create. Must be a valid file path.
      pages - The page information collection to save.
      options - The options to use when saving the PDF file. May be null.
      Throws:
      IllegalArgumentException - if fileName is not a valid path.
      RuntimeException - if an I/O error occurs while creating or writing the PDF file.
    • paginate

      public final List<PageInfo> paginate(List<IWorkbook> workbooks)
      Generates pagination information for all worksheets in all workbooks.

      This method paginates each workbook in the specified list by calling paginate(IWorkbook), combines the resulting PageInfo objects into a single list, and then updates the page number and page settings for the combined pages.

      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       List<IWorkbook> workbooks = new ArrayList<IWorkbook>();
       workbooks.add(workbook);
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(workbooks);
       
      Parameters:
      workbooks - The workbooks to paginate. The workbooks are processed in the order they appear in the list.
      Returns:
      A list of PageInfo objects generated from all workbooks. Returns an empty list if the specified list is empty.
    • paginate

      public final List<PageInfo> paginate(IWorkbook workbook)
      Generates pagination information for all visible worksheets in the workbook.

      This method paginates each worksheet whose visibility is Visibility.Visible, combines the resulting PageInfo objects into a single list, and updates the page number, page count, and page settings for the generated pages.

      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(workbook);
       
      Parameters:
      workbook - The workbook whose visible worksheets are paginated. Must not be null.
      Returns:
      A list of PageInfo objects for all visible worksheets in the workbook. Returns an empty list if no pages are generated.
    • paginate

      public final List<PageInfo> paginate(IWorksheet worksheet)
      Generates pagination information for the worksheet.

      The pagination result is calculated from the worksheet's current print areas and page setup. Each returned PageInfo describes one generated page in page order.

      
       worksheet.getPageSetup().setPrintArea("B2:N40");
       worksheet.getRange("B2:N40").setValue(10);
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       
      Parameters:
      worksheet - The worksheet whose pages are generated. Must not be null.
      Returns:
      A list of PageInfo objects that describe the generated pages in page order. Returns an empty list if no printable pages are generated.
    • paginate

      public final List<PageInfo> paginate(IWorksheet worksheet, List<IRange> keepTogetherRanges, List<RepeatSetting> repeatSettings)
      Gets pagination information for the specified worksheet.

      The pagination result is calculated from the worksheet's current page setup and print areas. Use keepTogetherRanges to keep specific ranges on the same page, and use repeatSettings to repeat title or tail rows or columns for a range during pagination.

      
       worksheet.getPageSetup().setPrintArea("A1:D80");
       worksheet.getRange("A1:D80").setValue(100);
       List<IRange> keepTogetherRanges = new ArrayList<>();
       keepTogetherRanges.add(worksheet.getRange("$20:$25"));
       RepeatSetting repeatSetting = new RepeatSetting();
       repeatSetting.setRange(worksheet.getRange("A1:D80"));
       repeatSetting.setTitleRowStart(0);
       repeatSetting.setTitleRowEnd(0);
       List<RepeatSetting> repeatSettings = new ArrayList<>();
       repeatSettings.add(repeatSetting);
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet, keepTogetherRanges, repeatSettings);
       
      Parameters:
      worksheet - The worksheet whose pages are generated. Must not be null.
      keepTogetherRanges - The ranges that should remain on the same page. Can be null or empty if no keep-together ranges are needed.
      repeatSettings - The repeat settings applied during pagination. Can be null or empty if no repeating rows or columns are needed.
      Returns:
      A list of PageInfo objects that describe the generated pages in page order. Returns an empty list if no printable pages are generated.
      Throws:
      IllegalStateException - if a keep-together range or repeat setting cannot fit within the printable page area.
    • generatePageContentInfo

      public final PageContentInfo generatePageContentInfo(IRange printArea, Size availableSize, int rowStart, int columnStart, boolean rowHeaderVisible, boolean columnHeaderVisible)
      Returns a PageContentInfo object for the specified print area and page size.

      The result is calculated from the supplied start row, start column, available size, and row and column header visibility settings.

      
       worksheet.getRange("A1:C5").setValue(new Object[][] {
           {"Name", "Value", "Status"},
           {"Test", 100, "Ready"}
       });
      
       PrintManager printManager = new PrintManager();
       PageContentInfo info = printManager.generatePageContentInfo(
               worksheet.getRange("A1:C5"), new Size(400, 300), 0, 0, true, false);
       
      Parameters:
      printArea - The print area to evaluate. Must not be null.
      availableSize - The page content size excluding margins. Must not be null.
      rowStart - The zero-based start row index used to calculate the ending row.
      columnStart - The zero-based start column index used to calculate the ending column.
      rowHeaderVisible - Whether the row header is printed with this page.
      columnHeaderVisible - Whether the column header is printed with this page.
      Returns:
      A PageContentInfo object that describes the calculated page content.
    • updatePageNumberAndPageSettings

      public final void updatePageNumberAndPageSettings(List<PageInfo> pages)
      Updates the page number, page count, and page settings of each page.

      Call this method after the page collection changes, such as when pages are added, deleted, or moved. It refreshes the supplied PageInfo list so pagination metadata stays consistent before further processing or export.

      
       worksheet.getPageSetup().setPrintArea("B2:N40");
       worksheet.getRange("B2:N40").setValue(10);
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       PageInfo page = pages.remove(0);
       pages.add(page);
       printManager.updatePageNumberAndPageSettings(pages);
       
      Parameters:
      pages - The page collection to update. Typically this is a list returned by paginate(IWorksheet) after the collection has been changed.
    • updatePageNumberAndPageSettings

      public final void updatePageNumberAndPageSettings(List<PageInfo> pages, int firstPageNumber, int pageCount)
      Updates the page number, page count, and page settings of each page.

      Call this method after the page collection changes, such as when pages are added, deleted, or moved. This overload updates the specified pages by using the provided first page number and total page count.

      
       worksheet.getPageSetup().setPrintArea("B2:N40");
       worksheet.getRange("B2:N40").setValue(10);
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       printManager.updatePageNumberAndPageSettings(pages, 5, 12);
       
      Parameters:
      pages - The page collection to update.
      firstPageNumber - The page number to assign to the first page in pages.
      pageCount - The total number of pages to apply to the page information.
    • appendPage

      public final void appendPage(org.apache.pdfbox.pdmodel.PDDocument doc, PageInfo pageInfo)
      Appends the specified printed page to a PDF document.

      This method adds a new page to doc using the paper size from pageInfo, and then draws the page content onto the appended PDF page.

      This overload forwards null to appendPage(PDDocument,PageInfo,PdfSaveOptions).

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       PageInfo page = printManager.paginate(worksheet).get(0);
       try (PDDocument doc = new PDDocument()) {
           printManager.appendPage(doc, page);
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that receives the appended page. Must not be null.
      pageInfo - The printed page information to append. Must not be null.
    • appendPage

      public final void appendPage(org.apache.pdfbox.pdmodel.PDDocument doc, PageInfo pageInfo, PdfSaveOptions options)
      Appends the specified printed page to a PDF document.

      This method creates a new PDF page in doc by using the paper size defined by pageInfo, and then draws the page content onto the appended page. Use paginate(IWorksheet) to generate the PageInfo instance before calling this method.

      The options argument controls PDF rendering behavior. Pass null if no PdfSaveOptions are needed.

      
       worksheet.getRange("A1:B3").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200}
       });
       PrintManager printManager = new PrintManager();
       PageInfo pageInfo = printManager.paginate(worksheet).get(0);
       PdfSaveOptions options = new PdfSaveOptions();
       try (PDDocument doc = new PDDocument()) {
           printManager.appendPage(doc, pageInfo, options);
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that receives the appended page. Must not be null.
      pageInfo - The printed page information to append. Must not be null.
      options - The PDF save options applied when drawing the page content; may be null.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, List<PageInfo> pages, int rows, int columns)
      Draws the pages in a page collection onto a PDF page.

      This method divides the target PDF page into a grid defined by rows and columns, then draws the specified PageInfo objects into that layout. This overload forwards to draw(PDDocument,PDPage,List,int,int,Order) with Order.OverThenDown.

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, pages, 1, 1);
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that receives the drawn content. Must not be null.
      page - The target PDF page. Must not be null.
      pages - The page collection to draw. Must not be null.
      rows - The number of rows used to divide the target page layout. Must be greater than 0.
      columns - The number of columns used to divide the target page layout. Must be greater than 0.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, List<PageInfo> pages, int rows, int columns, Order order)
      Draws the pages in a page collection onto a PDF page in the specified order.

      This overload forwards to draw(PDDocument,PDPage,List,int,int,Order,PdfSaveOptions) with null options.

      
       worksheet.getRange("A1:B3").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200}
       });
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, pages, 1, 1, Order.OverThenDown);
       } catch (Exception e) {
       }
       
      Parameters:
      doc - The PDF document that receives the drawn content. Must not be null.
      page - The target PDF page. Must not be null.
      pages - The page collection to draw. Must not be null.
      rows - The number of rows used to divide the page layout. Must be greater than 0.
      columns - The number of columns used to divide the page layout. Must be greater than 0.
      order - The direction used to arrange the pages. Must not be null.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, List<PageInfo> pages, int rows, int columns, Order order, PdfSaveOptions options)
      Draws the pages in the specified page collection onto a PDF page.

      This method divides the target PDF page into a grid defined by rows and columns, then draws each PageInfo into one grid cell. The order parameter controls whether the pages are arranged across columns first or down rows first.

      
       worksheet.getRange("A1:C6").setValue(new Object[][] {
           {"Name", "Q1", "Q2"},
           {"North", 100, 120},
           {"South", 90, 110}
       });
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       PdfSaveOptions options = new PdfSaveOptions();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, pages, 1, 1, Order.OverThenDown, options);
       } catch (Exception e) {
       }
       
      Parameters:
      doc - The PDF document that receives the drawn content. Must not be null.
      page - The target PDF page. Must not be null.
      pages - The page collection to draw. Must not be null.
      rows - The number of rows used to divide the target page layout.
      columns - The number of columns used to divide the target page layout.
      order - The direction used to arrange the pages in the grid. Must not be null.
      options - The PDF save options applied when drawing the page content; can be null.
    • hasPrintContent

      public final boolean hasPrintContent(IRange range)
      Returns whether the specified range contains content to print.
      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       PrintManager printManager = new PrintManager();
       boolean hasContent = printManager.hasPrintContent(worksheet.getRange("B2:D6"));
       
      Parameters:
      range - The printed range of a page. Must not be null.
      Returns:
      true if the specified range contains printable content; otherwise, false.
      Throws:
      NullPointerException - if range is null.
    • getSize

      public final Size getSize(PageContentInfo contentInfo)
      Returns the size of the PageContentInfo.
      
       worksheet.getPageSetup().setPrintArea("A1:B4");
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(worksheet);
       PageContentInfo contentInfo = pages.get(0).getPageContent();
       Size contentSize = printManager.getSize(contentInfo);
       
      Parameters:
      contentInfo - The PageContentInfo object.
      Returns:
      The size of the PageContentInfo.
    • getSize

      public final Size getSize(IRange range)
      Returns the size of the range.
      
       worksheet.getRange("B2:D6").setValue("Test");
       IRange printArea = worksheet.getRange("B2:D6");
       PrintManager printManager = new PrintManager();
       Size rangeSize = printManager.getSize(printArea);
       
      Parameters:
      range - The range object.
      Returns:
      The size of the range.
    • paginate

      public final List<PageInfo> paginate(IRange printArea)
      Generates pagination information for the specified range.

      The pagination result is calculated for the provided printArea by using the worksheet's current page setup. Each returned PageInfo represents one generated page in page order, and its page number, page count, and page settings are updated before the list is returned.

      
       IRange printArea = worksheet.getRange("B2:D20");
       printArea.setValue("Test");
       PrintManager printManager = new PrintManager();
       List<PageInfo> pages = printManager.paginate(printArea);
       
      Parameters:
      printArea - The range to paginate. Must not be null.
      Returns:
      A list of PageInfo objects that describe the generated pages for the specified range in page order. Returns an empty list if no printable pages are generated.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Point location, IRange range)
      Draws the specified worksheet range at a specific location on a PDF page.

      This method measures the rendered size of range by calling getSize(IRange) and then forwards to draw(PDDocument,PDPage,Rectangle,IRange) with a Rectangle whose origin is location and whose width and height match the measured range size.

      
       worksheet.getRange("A1:B2").setValue(new Object[][] {
           {"Name", "Value"},
           {"Test", 100}
       });
       IRange range = worksheet.getRange("A1:B2");
       PrintManager printManager = new PrintManager();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, new Point(36, 72), range);
       } catch (Exception e) {
       }
       
      Parameters:
      doc - The PDF document that receives the rendered range.
      page - The PDF page on which to draw the range.
      location - The upper-left location, in page coordinates, where the range is drawn.
      range - The worksheet range to render.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Point location, IRange range, PdfSaveOptions options)
      Draws the specified worksheet range at the given location on a PDF page.

      This overload measures range, creates a rectangle whose origin is location and whose size matches the range, and then forwards to draw(PDDocument,PDPage,Rectangle,IRange,PdfSaveOptions).

      
       worksheet.getRange("A1:B2").setValue(new Object[][] {
           {"Name", "Value"},
           {"Test", 100}
       });
       IRange range = worksheet.getRange("A1:B2");
       PrintManager printManager = new PrintManager();
       PdfSaveOptions options = new PdfSaveOptions();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, new Point(36, 72), range, options);
       } catch (Exception e) {
       }
       
      Parameters:
      doc - The PDF document that receives the rendered range.
      page - The PDF page on which to draw the range.
      location - The top-left location, in page coordinates, where the range is drawn.
      range - The worksheet range to render.
      options - The PDF rendering options. May be null.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Rectangle rect, IRange range)
      Draws the specified worksheet range into a rectangular area on a PDF page.

      This method renders the cells in range on page within rect. It forwards to draw(PDDocument,PDPage,Rectangle,IRange,PdfSaveOptions) with null for options.

      
       worksheet.getRange("A1:B2").setValue(new Object[][] {
           {"Name", "Value"},
           {"Test", 100}
       });
       IRange range = worksheet.getRange("A1:B2");
       PrintManager printManager = new PrintManager();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, new Rectangle(36, 72, 220, 90), range);
       } catch (Exception e) {
       }
       
      Parameters:
      doc - The PDF document that receives the rendered range.
      page - The PDF page on which to draw the range.
      rect - The target rectangle, in page coordinates, that defines where the range is drawn.
      range - The worksheet range to render.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Rectangle rect, IRange range, PdfSaveOptions options)
      Draws the specified worksheet range into a rectangular area on a PDF page.

      This method renders the cells in range by using the worksheet's page settings and writes the output to page within rect. If options is not null, its PDF document properties and security settings are also applied to doc before the range is drawn.

      
       worksheet.getRange("A1:B2").setValue(new Object[][] {
           {"Name", "Value"},
           {"Test", 100}
       });
       IRange range = worksheet.getRange("A1:B2");
       PrintManager printManager = new PrintManager();
       PdfSaveOptions options = new PdfSaveOptions();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           Rectangle rect = new Rectangle(36, 72, 220, 90);
           printManager.draw(doc, page, rect, range, options);
       } catch (Exception e) {
       }
       
      Parameters:
      doc - The PDF document that receives the rendered range. If options contains document properties or security settings, they are applied to this document.
      page - The PDF page on which to draw the range.
      rect - The target rectangle, in page coordinates, that defines where the range is drawn.
      range - The worksheet range to render.
      options - The PDF rendering options. May be null.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Rectangle rect, PageContentInfo pageContent)
      Draws the printed page content to the specified rectangle on a PDF page.

      Use this method to render a PageContentInfo object, such as one obtained from paginate(IWorksheet), into the target area defined by rect on page. This overload forwards null for options to draw(PDDocument,PDPage,Rectangle,PageContentInfo,PdfSaveOptions).

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       PageContentInfo pageContent = printManager.paginate(worksheet).get(0).getPageContent();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage(new PDRectangle(400, 600));
           doc.addPage(page);
           printManager.draw(doc, page, new Rectangle(36, 36, 328, 528), pageContent);
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that contains page. Must not be null.
      page - The PDF page on which the printed content is drawn. Must not be null.
      rect - The target location and size, in PDF page coordinates, used to draw the printed content. Must not be null.
      pageContent - The printed page content information to render. Must not be null and must contain a range.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Rectangle rect, PageContentInfo pageContent, PdfSaveOptions options)
      Draws printed page content into a specified area of a PDF page.

      This method creates a PageInfo from pageContent, uses the worksheet's odd-page settings, sets the paper size to the size of pageContent, clears all page margins, and then draws the result into rect on page.

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       PageContentInfo pageContent = printManager.paginate(worksheet).get(0).getPageContent();
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage(new PDRectangle(400, 600));
           doc.addPage(page);
           printManager.draw(doc, page, new Rectangle(36, 36, 328, 528), pageContent, new PdfSaveOptions());
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that contains page. Must not be null.
      page - The PDF page on which the printed content is drawn. Must not be null.
      rect - The target location and size, in PDF page coordinates, used to draw the printed content. Must not be null.
      pageContent - The printed page content information to render. Must not be null and must contain a range.
      options - The PDF save options applied during drawing. Can be null.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Rectangle rect, PageInfo pageInfo)
      Draws the specified printed page information into the given rectangle on a PDF page.

      This overload forwards to draw(PDDocument,PDPage,Rectangle,PageInfo,PdfSaveOptions) with null for options.

      
       worksheet.getPageSetup().setPrintArea("A1:B4");
       worksheet.getRange("A1:B4").setValue(new Object[][] {{"Name", "Value"}, {"Test", 100}, {"Hello", 200}, {"Total", 300}});
       PrintManager printManager = new PrintManager();
       PageInfo pageInfo = printManager.paginate(worksheet).get(0);
       Rectangle rect = new Rectangle(36, 36, 400, 300);
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage();
           doc.addPage(page);
           printManager.draw(doc, page, rect, pageInfo);
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that contains the target page.
      page - The page in the PDF document.
      rect - The location and size of the area where the page information is drawn.
      pageInfo - The printed page information to draw.
    • draw

      public final void draw(org.apache.pdfbox.pdmodel.PDDocument doc, org.apache.pdfbox.pdmodel.PDPage page, Rectangle rect, PageInfo pageInfo, PdfSaveOptions options)
      Draws printed page information into a specified area of a PDF page.

      This method renders the content represented by pageInfo into rect on page. The page content stream is opened in append mode, so the drawn output is added to the existing PDF page content. The pageInfo object is typically obtained from paginate(IWorksheet).

      
       worksheet.getRange("A1:B4").setValue(new Object[][] {
           {"Name", "Value"},
           {"A", 100},
           {"B", 200},
           {"C", 300}
       });
       PrintManager printManager = new PrintManager();
       PageInfo pageInfo = printManager.paginate(worksheet).get(0);
       try (PDDocument doc = new PDDocument()) {
           PDPage page = new PDPage(new PDRectangle(400, 600));
           doc.addPage(page);
           printManager.draw(doc, page, new Rectangle(36, 36, 328, 528), pageInfo, new PdfSaveOptions());
       } catch (IOException e) {
       }
       
      Parameters:
      doc - The PDF document that contains page. Must not be null.
      page - The PDF page on which the printed content is drawn. Must not be null.
      rect - The target location and size, in PDF page coordinates, used to draw the printed content. Must not be null.
      pageInfo - The printed page information to render. Must not be null.
      options - The PDF save options applied during drawing. Can be null.
    • getPrintAreas

      public final List<IRange> getPrintAreas(IWorksheet worksheet)
      Returns the print areas of the specified worksheet.
      
       worksheet.getPageSetup().setPrintArea("B2:D6");
       worksheet.getRange("B2:D6").setValue("Test");
       PrintManager printManager = new PrintManager();
       List<IRange> printAreas = printManager.getPrintAreas(worksheet);
       String address = printAreas.get(0).getAddress();
       
      Parameters:
      worksheet - The worksheet object.
      Returns:
      The print areas of the specified worksheet.
    • GetPaginationInfo

      public List<Integer> GetPaginationInfo(IWorksheet worksheet, PaginationOrientation orientation)
      Returns the row or column indexes that define page boundaries for the specified worksheet.

      The pagination is based on the current IPageSetup settings of the worksheet. Call this method separately for horizontal and vertical pagination. If no print area is defined, pagination is determined from cell A1 to the last cell that contains data.

      
       worksheet.getPageSetup().setPrintArea("B2:N40");
       worksheet.getRange("B2:N40").setValue(10);
       PrintManager printManager = new PrintManager();
       List<Integer> columnPageBoundaries =
               printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal);
       
      Parameters:
      worksheet - The worksheet whose pagination information is retrieved. Must not be null.
      orientation - The pagination direction that determines whether row or column boundary indexes are returned.
      Returns:
      A list of row or column indexes that represent the page boundaries for the specified orientation.
    • GetPaginationInfo

      public List<Integer> GetPaginationInfo(IWorksheet worksheet, PaginationOrientation orientation, List<IRange> keepTogetherRanges, List<RepeatSetting> repeatSettings)
      Gets the row or column indexes that define page boundaries for the specified worksheet.

      The pagination result is calculated from the current page setup of the worksheet and the specified orientation. Use keepTogetherRanges to prevent specific ranges from being split across pages, and use repeatSettings to repeat title or tail rows or columns for a range during pagination.

      If the worksheet has no print area, this method returns null.

      
       worksheet.getPageSetup().setPrintArea("A1:D80");
       worksheet.getRange("A1:D80").setValue(100);
      
       List<IRange> keepTogetherRanges = new ArrayList<>();
       keepTogetherRanges.add(worksheet.getRange("A20:D25"));
      
       RepeatSetting repeatSetting = new RepeatSetting();
       repeatSetting.setRange(worksheet.getRange("A1:D80"));
       repeatSetting.setTitleRowStart(0);
       repeatSetting.setTitleRowEnd(0);
       List<RepeatSetting> repeatSettings = new ArrayList<>();
       repeatSettings.add(repeatSetting);
      
       PrintManager printManager = new PrintManager();
       List<Integer> pageBoundaries = printManager.GetPaginationInfo(
               worksheet, PaginationOrientation.Vertical, keepTogetherRanges, repeatSettings);
       
      Parameters:
      worksheet - The worksheet whose pagination information is retrieved. Must not be null.
      orientation - The pagination direction that determines whether row or column boundary indexes are returned.
      keepTogetherRanges - The ranges that should remain on the same page. Can be null or empty if no keep-together ranges are needed.
      repeatSettings - The repeat settings applied during pagination. Can be null or empty if no repeating rows or columns are needed.
      Returns:
      A list of row or column indexes that represent the page boundaries for the specified orientation, or null if the worksheet has no print area.
      Throws:
      IllegalStateException - if the keep-together or repeat settings cannot fit within the printable page area.