[]
        
(Showing Draft Content)

在PDF中渲染Excel区域

GcExcel Java使用户可以在PDF内呈现Excel单元格区域。

此功能在处理电子表格中的批量数据并且仅要在现有PDF文件中呈现特定Excel区域时特别有用。例如,假设您有一个包含大量销售数据的工作表,其中包含“已售产品数量”,“区域销售经理”,“地区”等字段,但是您只想导出一部分有用数据(例如PDF文件中某些位置仅显示“已售产品数量”和“区域”,而不是全部数据(您不希望包含“区域销售经理”信息)。在这种情况下,“在PDF中显示Excel区域”功能可用于选择工作表中的某些特定区域,并将它们呈现到PDF文件中的特定位置以生成完整的PDF报告。


为了在PDF文件中呈现Excel区域,您需要首先创建PrintManager类的实例, 然后使用 draw()方法在某个位置的PDF页面上呈现Excel区域。如果要在PDF文件中添加一些额外的信息(Excel文件中不存在的数据),则可以在配置所有分页设置后使用PrintManager类的appendPage() 方法。最后,调用updatePageNumberAndPageSettings()方法,以更新每个页的页码和页设置的索引。完成所有操作后,只需使用savePageInfosToPDF()方法保存您的PDF文件 。


请参考以下示例代码,以允许用户在PDF文件中呈现Excel区域。

// 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();
}

请参考以下示例代码,以允许用户在运行时将PDF区域内的Excel区域以及一些自定义文本信息呈现到页面上的指定位置。

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);
}