We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Conversion to PDF of an XLS file containing an EMF image object

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;
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();
}
}
This class works well on simple XLS files, but fails when the targeted sheet is containing an EMF image (no problem with the WMF).

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 B
Thanks


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

Hello Vincent,

EMF Image format is currently not supported by Aspose.Pdf for Java. We've already logged this requirement as PDFJAVA-8650 in New Features list over our Issue tracking system. As soon as we've made some progress regarding the support of this feature, we would be pleased to update you with the status of implementation.

Your patience and comprehension is greatly appreciated in this regard.

We apologize for your inconvenience.

Thank you for your answer.

Could there be a way for me to workaround this problem by using the Aspose.Metafile API?

Regards

Hello Vincent,

Sorry for replying you so late.

As a workaround, you can use Aspose.Metafiles for Java to convert the EMF image into JPEG/PNG format and then use that image in Excel worksheet. Please visit the following link for more information on Saving a Metafile as Image.