when converting xlsx file to pdf using aspose-cells library in java, if xlsx file has many columns , the columns which exceed the page width of pdf should move to next page of the pdf. what we observed is, sometimes the column which exceed the page width of pdf moves to next page, but sometimes it shows pages with duplicate contents across multiple pages, please help to resolve this issue
i observed same result with aspose-cells version: 24.4 and 25.5
xlsxinput.7z (43.5 KB)
xlsxoutput.7z (194.8 KB)
private void removeStyleFromCells(Cells cells, Workbook workbook) {
Style defaultStyle = workbook.createStyle();
StyleFlag flag = new StyleFlag();
flag.setAll(true);
cells.applyStyle(defaultStyle, flag);
Range usedRange = cells.getMaxDisplayRange();
usedRange.applyStyle(defaultStyle, flag);
}
PdfSaveOptions getPdfSaveOptions() {
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setOnePagePerSheet(false);
saveOptions.setAllColumnsInOnePagePerSheet(false);
saveOptions.setOutputBlankPageWhenNothingToPrint(false); // Prevent blank pages
saveOptions.setPrintingPageType(PrintingPageType.IGNORE_BLANK); // Ignore blank
saveOptions.setCheckWorkbookDefaultFont(false);
saveOptions.setOptimizationType(PdfOptimizationType.MINIMUM_SIZE);
return saveOptions;
}
public void convert(FileInfo fileInfo) {
try {
System.out.println("Converting " + fileInfo.getFilename_with_ext() + " to PDF");
long startTime = System.currentTimeMillis();
String inputPath = Common.INPUT_DIR_PATH+fileInfo.getFilename_with_ext();
String outputPath = Common.OUTPUT_DIR_PATH+fileInfo.getFilename_without_ext()+".pdf";
String orientation = "LANDSCAPE";
boolean includeComments = true;
com.aspose.cells.LoadOptions options = new com.aspose.cells.LoadOptions();
options.setMemorySetting(MemorySetting.MEMORY_PREFERENCE); // try to optimize mem usage for large files
options.setStandardFont("Arial");
String currentOrientation = (orientation == null || orientation.isEmpty()) ? "DEFAULT" : orientation.toUpperCase().trim();
Workbook workbook = new Workbook(inputPath, options);
PdfSaveOptions saveOptions = new PdfSaveOptions();
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
if (!workbook.getWorksheets().get(i).isVisible()) {
workbook.getWorksheets().get(i).setVisibilityType(VisibilityType.VISIBLE);
}
// allow Aspose to auto-orient if DEFAULT mode is selected for orientation, otherwise we'll orient as specified
if (currentOrientation.equals("LANDSCAPE") || currentOrientation.equals("PORTRAIT")) {
workbook.getWorksheets().get(i).getPageSetup().setOrientation(currentOrientation.equals("LANDSCAPE") ? PageOrientationType.LANDSCAPE : PageOrientationType.PORTRAIT);
}
workbook.getWorksheets().get(i).getPageSetup().setBlackAndWhite(false); // we want to keep the PDF in same colors as input Excel
workbook.getWorksheets().get(i).getPageSetup().setHFScaleWithDoc(false); // keep the scale of the header/footer consistent
String firstPageFooter = workbook.getWorksheets().get(i).getPageSetup().getFirstPageFooter(1);
if (firstPageFooter == null || firstPageFooter.isEmpty()) {
workbook.getWorksheets().get(i).getPageSetup().setFirstPageFooter(1, "Page &P of &N"); // set the default first page footer if none is specified
}
workbook.getWorksheets().get(i).getPageSetup().setPrintComments(includeComments ? PrintCommentsType.PRINT_SHEET_END : PrintCommentsType.PRINT_NO_COMMENTS);
AutoFitterOptions autoFitterOptions = new AutoFitterOptions();
autoFitterOptions.setAutoFitMergedCellsType(AutoFitMergedCellsType.EACH_LINE);
workbook.getWorksheets().get(i).autoFitRows(autoFitterOptions);
workbook.getWorksheets().get(i).autoFitColumns();
}
// finally, save the Excel file as a PDF
workbook.save(outputPath, getPdfSaveOptions());
long endTime = System.currentTimeMillis();
System.out.println("Conversion completed in " + (endTime - startTime) + " ms");
}
catch (Exception e) {
//e.printStackTrace();
System.out.println("138 conversion exception "+e.getMessage());
}
}