Hi,
I have made a small class using Aspose.Cells and Aspose.PDF, which purpose is to extract one of a worbook’s sheet into a PDF. Here is the source code:
import java.io.FileOutputStream;This class works well on simple XLS files, but fails when the targeted sheet is containing an EMF image (no problem with the WMF).
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.pdf.elements.DestinationType;
import com.aspose.pdf.elements.Pdf;
public class ExcelToPdf {
public static void main (String[] args) {
if (args.length < 2) {
System.err.println(“2 arguments needed: fileName, sheetName”);
System.exit(0);
}
new ExcelToPdf().run(args[0], args[1]);
}
public void run(final String fileName, String sheetName) {
Workbook sourceWb = new Workbook();
try {
sourceWb.open(fileName);
}
catch (IOException ioe) {
throw new RuntimeException("Could not open file: " + fileName, ioe);
}
Worksheet sourceWs = sourceWb.getWorksheets().getSheet(sheetName);
final Workbook workWb = new Workbook();
Worksheet workWs = workWb.getWorksheets().addSheet();
workWs.copy(sourceWs);
final PipedInputStream in = new PipedInputStream();
PipedOutputStream outTemp;
try {
outTemp = new PipedOutputStream(in);
}
catch (IOException ioe) {
throw new RuntimeException(“Error”, ioe);
}
final PipedOutputStream out = outTemp;
new Thread() {
public void run() {
try {
workWb.save(out, FileFormatType.ASPOSE_PDF);
}
catch (IOException ioe) {
throw new RuntimeException(“Error”, ioe);
}
}
}.start();
new Thread() {
public void run() {
Pdf pdf;
try {
pdf = Pdf.bindXML(in);
}
catch (Exception e) {
throw new RuntimeException(“Error”, e);
}
pdf.setDestinationType(DestinationType.FitWidth);
try {
pdf.save(new FileOutputStream(fileName + “.pdf”));
}
catch (Exception e) {
throw new RuntimeException("Could not save file: " + fileName + “.pdf”, e);
}
}
}.start();
}
}
Please use the attached bug.zip file as an example. The sheet to be exported is named “B”, so please use the following command line:
java ExcelToPdf bug.xls BThanks
Edit:
I am using an Aspose.Cells hotfix available here: Invalid row height
I am using the last Aspose.PDF available 2.3.1.0 (is there another one?)
I have also included the last available Aspose.Metafile jar in the classpath