Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Control Pagination / Render Excel Range Inside PDF
In This Topic
    Render Excel Range Inside PDF
    In This Topic

    DsExcel Java enables users to render Excel cell ranges inside PDF.

    This feature is useful especially when you're dealing with bulk data in the spreadsheets and you want to render only specific Excel range inside an existing PDF file. For instance - let's say you have a worksheet containing large amounts of sales data with fields such as "Number of Products Sold", "Area Sales Manager", "Region" etc. but you want to export only a chunk of useful data (like only "Number of Products Sold" and "Region") at some location in a PDF file and not all the data (you don't want to include the "Area Sales Manager" information). In this scenario, the "Render Excel Range Inside PDF" feature can be used to select some specific ranges in the worksheet and render them to specific location in a PDF file to generate full PDF reports.

    In order to render Excel range inside the PDF file, you need to first create an instance of the PrintManager class and then use the draw() method to render the Excel range on a PDF page at a location. In case, you want to add some extra information in your PDF file (data which is not present in your Excel file), you can use the appendPage() method of the PrintManager class after configuring all the pagination settings. Finally, call the updatePageNumberAndPageSettings() method in order to update the indexes of the page number and the page settings for each page. When everything is done, simply save your PDF file using the savePageInfosToPDF() method.   

    Refer to the following example code to allow users to render Excel ranges inside the PDF file .

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
        
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    // Set value
    worksheet.getRange("A4:C4").setValue(new Object[] 
    { "Device", "Quantity", "Unit Price" });
    worksheet.getRange("A5:C8").setValue(new Object[][] 
    {
    { "T540p", 12, 9850 }, { "T570", 5, 7460 },
    { "Y460", 6, 5400 }, { "Y460F", 8, 6240 } 
    });
    
    // Set style
    worksheet.getRange("A4:C4").getFont().setBold(true);
    worksheet.getRange("A4:C4").getFont()
    .setColor(Color.GetWhite());
    worksheet.getRange("A4:C4").getInterior()
    .setColor(Color.GetLightBlue());
    worksheet.getRange("A5:C8")
    .getBorders().get(BordersIndex.InsideHorizontal)
    .setColor(Color.GetOrange());
    worksheet.getRange("A5:C8").getBorders()
    .get(BordersIndex.InsideHorizontal)
    .setLineStyle(BorderLineStyle.DashDot);
    
    // Configure Page size
    float width = 600f;
    float height = 500f;
    PDRectangle pageSize = new PDRectangle(width, height);
        
    // Create a PDF document
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage(pageSize);
    doc.addPage(page);
    
    // Create an instance of the PrintManager class
    PrintManager printManager = new PrintManager();
        
    // Draw the Range"A4:C8" to the specified location on the page
    printManager.draw(doc, page, new Point(30, 100), 
    worksheet.getRange("A4:C8"));
    
    // Save the modified pages into PDF file
    try 
    {
     doc.save("RenderExcelRangesInsidePDFBasic.pdf");
    } 
    catch (IOException e) 
    {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }

    Refer to the following example code to allow users to render Excel ranges inside the PDF file along with some custom textual information at runtime to the specified location on the page.

    Java
    Copy Code
    private static void RenderExcelRangesInPDF() throws Exception 
    {
    // Create to a pdf file stream
    FileOutputStream outputStream = null;
        
    try 
    {
      outputStream = 
      new FileOutputStream("RenderExcelRangesInsideAPDF.pdf");
    } 
    catch (FileNotFoundException e) 
    {
      e.printStackTrace();
    }
        
    // Create a new workbook
    Workbook workbook = new Workbook();
    workbook.open(getResourceStream("xlsx/FinancialReport.xlsx"));
       
    // Create a PDF document.
    PDDocument doc = null;
    try 
    {
     doc = PDDocument.load(getResourceStream
     ("xlsx/Acme-Financial Report 2018.pdf"));
    } 
    catch (IOException e1) 
    {
     e1.printStackTrace();
    }
    
    // Create an instance of the PrintManager class.
    PrintManager printManager = new PrintManager();
    
    // Draw the contents of the sheet3 to the fourth page.
    IRange printArea1 = 
    workbook.getWorksheets().get(2).getRange("A3:C24");
    Size size1 = 
    printManager.getSize(printArea1);
    printManager.draw(doc, doc.getPage(3), new Rectangle(306, 215, 
    size1.getWidth(), size1.getHeight()),
    printArea1);
    
    // Draw the contents of the sheet1 to the fifth page.
    IRange printArea2 = 
    workbook.getWorksheets().get(0).getRange("A3:F29");
    Size size2 = printManager.getSize(printArea2);
    printManager.draw(doc, doc.getPage(4), 
    new Rectangle(71, 250, size2.getWidth(), size2.getHeight()), 
    printArea2);
    
    // Draw the contents of the sheet2 to the sixth page.
    IRange printArea3 = 
    workbook.getWorksheets().get(1).getRange("A3:G27");
    Size size3 = printManager.getSize(printArea3);
    printManager.draw(doc, doc.getPage(5), 
    new Rectangle(71, 230, 783, size3.getHeight()), printArea3);
    
    // Save the modified pages into pdf file.
    try 
    {
      doc.save(outputStream);
      doc.close();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
    }
    
    // Close the file stream
    try 
    {
      outputStream.close();
    } 
    catch (IOException e) 
    {
      e.printStackTrace();
    }
    }
    
    private static InputStream getResourceStream(String resource) throws Exception 
    {
     return UpcomingFeatures.class.getClassLoader()
     .getResourceAsStream(resource);
    }