I need to convert xlsx documents to pdf format. I know that iText can save pdf documents and Docx4j can read and w开发者_C百科rite xslx. In fact our application use both for building reports. But we have very difficult templates so I can't just read xslx(docx4j) and write it to pdf(iText). The formatting will be lost, so I need another conversion lib. I also heard about commercial libraries like (Jxcell ) but want to use open source solution.
Can anyone help me?
i have using iText and apache poi:
FileInputStream filecontent = new FileInputStream(new File(sourcepath));
FileOutputStream out = new FileOutputStream(new File(destinationPath));
HSSFWorkbook my_xls_workbook = null;
HSSFSheet my_worksheet = null;
XSSFWorkbook my_xlsx_workbook = null;
XSSFSheet my_worksheet_xlsx = null;
Document document = new Document(PageSize.ARCH_E, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
PdfDestination magnify = null;
float magnifyOpt = (float) 70.0;
magnify = new PdfDestination(PdfDestination.XYZ, -1, -1, magnifyOpt / 100);
int pageNumberToOpenTo = 1;
PdfAction zoom = PdfAction.gotoLocalPage(pageNumberToOpenTo, magnify, writer);
writer.setOpenAction(zoom);
document.addDocListener(writer);
Iterator<Row> rowIterator = null;
int maxColumn = 0;
if (fileType.equals(".xls")) {
try {
my_xls_workbook = new HSSFWorkbook(filecontent);
my_worksheet = my_xls_workbook.getSheetAt(0);
rowIterator = my_worksheet.iterator();
maxColumn = my_worksheet.getRow(0).getLastCellNum();
} catch (IOException ex) {
Logger.getLogger(PdfConversion.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (fileType.equals(".xlsx")) {
try {
my_xlsx_workbook = new XSSFWorkbook(filecontent);
my_worksheet_xlsx = my_xlsx_workbook.getSheetAt(0);
rowIterator = my_worksheet_xlsx.iterator();
maxColumn = my_worksheet_xlsx.getRow(0).getLastCellNum();
} catch (IOException ex) {
Logger.getLogger(PdfConversion.class.getName()).log(Level.SEVERE, null, ex);
}
}
PdfPTable my_table = new PdfPTable(maxColumn);
my_table.setHorizontalAlignment(Element.ALIGN_CENTER);
my_table.setWidthPercentage(100);
my_table.setSpacingBefore(0f);
my_table.setSpacingAfter(0f);
PdfPCell table_cell;
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next(); //Fetch CELL
switch (cell.getCellType()) { //Identify CELL type
case Cell.CELL_TYPE_STRING:
//Push the data from Excel to PDF Cell
table_cell = new PdfPCell(new Phrase(cell.getStringCellValue()));
if (row.getRowNum() == 0) {
table_cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table_cell.setBorderColor(BaseColor.BLACK);
}
my_table.addCell(table_cell);
break;
}
}
}
document.add(my_table);
document.close();
System.out.println("Excel file converted to PDF successfully");
Does it have to be done in Java? If yes, maybe take a look at Apache POI
Otherwise, why not have a small native app that utilises a PDF Printer, and calls the relevant APIs to print the file directly to PDF? Surely Java is not really made for doing this kind of work...
For example here's a Non-Java framework for that: PDF Class Library from Solidworks
精彩评论