Corrupted Tiff when converting from Greek Excel files

Dear Aspose Team,


I use Aspose Cells aspose-cells-17.4.0.jar to write a converter in order to obtain a tiff file.
Please find attached the input files I am using.

My Standalone converter code is as follows:

import java.io.File;
import java.io.IOException;

import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.SheetRender;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.words.TiffCompression;

public class StandaloneXLSConverter {

public File runConverter( File file ) throws Exception {
File convertedFile = null;
try {
Workbook workbook = new Workbook( file.getAbsolutePath());
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

imgOptions.setOnePagePerSheet(true);
imgOptions.setVerticalResolution(400);
imgOptions.setHorizontalResolution(400);
imgOptions.setTiffCompression(TiffCompression.CCITT_4);
imgOptions.setImageFormat(ImageFormat.getTiff());

for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
Worksheet sheet = workbook.getWorksheets().get(i);

SheetRender sr = new SheetRender(sheet, imgOptions);
convertedFile = getConvertedTiffFile( file );
sr.toImage(i, convertedFile.getAbsolutePath());
}
}
catch( Exception e ) {
throw e;
}
return convertedFile;
}

private File getConvertedTiffFile(File file) throws IOException {
return File.createTempFile( file.getName().substring(0, file.getName().indexOf(".") - 1), “.tiff” );
}

public static void main( String[] args ){
File inputFile1 = new File(“isologismos-ΝΑΚ-2014.xls”);
File inputFile2 = new File(“ΙΣΟΛΟΓΙΣΜΟΣ-ΧΡΗΣΗΣ-20131.xls”);
File inputFile3 = new File(“LoginNovember2013.xlsx”);
StandaloneXLSConverter converter = new StandaloneXLSConverter();
try {
File outputFile1 = converter.runConverter( inputFile1 );
File outputFile2 = converter.runConverter( inputFile2 );
File outputFile3 = converter.runConverter( inputFile3 );
System.out.println(outputFile1.getAbsolutePath());
System.out.println(outputFile2.getAbsolutePath());
System.out.println(outputFile3.getAbsolutePath());
} catch (Exception e) {
System.err.println( “An error occurred while trying to convert excel to tiff.” + e );
}
}
}




I get 2 corrupted tiff files in return. I do not get any exceptions at runtime so I don’t know what’s wrong. Note that my xls files are written in Greek.
I also tried with a simple excel file containing English characters only and it worked fine. The test files I used are real application files we wish to convert (except the test file LoginNovember2013.xlsx).
Can you please suggest a solution for the Greek files?

Hope to hear from you soon!
Best regards,
George

Hi,


Thanks for the sample JAVA program with template files.

I tried to test your sample code with your template file but I got the error:
"Exception in thread “main” java.lang.NoClassDefFoundError: com.sun.media.jai.codec.SeekableStream
at javax.media.jai.operator.BMPDescriptor.class$(BMPDescriptor.java:72)
at javax.media.jai.operator.BMPDescriptor.(BMPDescriptor.java:94)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javax.media.jai.RegistryFileParser.getInstance(RegistryFileParser.java:224)
at javax.media.jai.RegistryFileParser.registerDescriptor(RegistryFileParser.java:360)
at javax.media.jai.RegistryFileParser.parseFile(RegistryFileParser.java:295)
at javax.media.jai.RegistryFileParser.loadOperationRegistry(RegistryFileParser.java:55)
at javax.media.jai.OperationRegistry.initializeRegistry(OperationRegistry.java:371)
at javax.media.jai.JAI.(JAI.java:566)
…"

I do have all the Java Advanced Imaging (JAI) Jars (e.g jai_codec-1.1.3.jar, jai_core.jar and jai_imageio.jar) into classpath but still I get the exception.

Could you provide us yours JAI jars (you may zip the archive) here, so I could evaluate your issue properly.

Thank you.

Hi,


Please ignore my previous post as it might be due to different JAI libraries or something else.
We did evaluate your issue a bit. For your code segment:
i.e.,
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
Worksheet sheet = workbook.getWorksheets().get(i);

SheetRender sr = new SheetRender(sheet, imgOptions);
convertedFile = getConvertedTiffFile( file );
sr.toImage(i, convertedFile.getAbsolutePath());
}

the line “sr.toImage” is not right. We think the code should be something like:
int pageNo = 1;
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
Worksheet sheet = workbook.getWorksheets().get(i);

SheetRender sr = new SheetRender(sheet, imgOptions);
for(int j = 0; j < sr.getPageCount(); j++)
{
sr.toImage(j, TMP_DIR + “test_Java_” + pageNo + “.tiff”);
}
pageNo++;
}

the param “pageIndex” should not be sheet index.

Also, your code generates tiff file to same path for one workbook. If there are more than one page, the generated tiff will be overwritten for sure, only last one will be kept/retained.
We think you can use WorkbookRender to save one workbook to one tiff file, like this:
e.g
Sample code:

WorkbookRender wr = new WorkbookRender(workbook, imgOptions);
wr.toImage(TMP_DIR + “test_Java.tiff”);

Hope, this helps a bit.

Let us know if you still have any issue.

Thank you.

Hi again,


I run my jar on linux with the following command
/opt/IBM/WebSphere8/AppServer/java/bin/java -Xms512m -Xmx1024m -classpath “/opt/IBM/WebSphere8/AppServer/java/lib/tools.jar:.:testConverter.jar:.:aspose-cells-17.4.0.jar:.:bcprov-jdk16-46.jar:.:jai_codec.jar:.:jai_core.jar:.:jai_imageio.jar” icap.gr.aspose.StandaloneXLSConverter


I only use jai jars (please see attached) and aspose jars: aspose-cells-17.4.0.jar and bcprov-jdk16-46.jar.

Please see attached my jar file and the input files I used. I changed the input file names in my code.

I don’t get any exception at runtime but input1.xls and input2.xls generate corrupted tiff files. Only input3.xlsx generates a valid tiff file.

I hope you can reproduce my scenario now.

Thank you Amjad!

Hi,


Please see my previous reply:
https://forum.aspose.com/t/17368

Thank you.

Hello Amjad,

I made some tests and I concluded to the following:

1) when I use save method on the workbook
workbook.save(workbookFilePath, SaveFormat.TIFF);
I get a multipaged tiff file but quality is low.

2) when I use the code you provided I get separate tiff files for each workbook, which I then zip together into one zip file.

So I think the solution you provided is very nice. I tested it on some sample excel files and I came now into another issue related to the tiff file being truncated.

Please find attached the source file and the resulting tiffs. The first two tiff files that correspond to the first two workbook sheets are OK. The third tiff file corresponding to the third sheet is truncated. You think you can help me or should I open another issue?

Please find attached my input excel file and output zip file.

My code:

Workbook workbook = new Workbook( file.getAbsolutePath() );
// Low quality multipage tiff
// String workbookFilePath = getConvertedTiffFile( file ).getAbsolutePath();
// workbook.save(workbookFilePath, SaveFormat.TIFF);


//Produce ZIP of TIFFS when there are multiple WorkSheets and/or Pages
if ( isMultiPageTiff(workbook) ) {
System.out.println(“ZIP of TIFFS”);
short part = 1;
List fileList = new ArrayList();
File baseDirectory = getZipDirectoryName();

for(int idx = 0; idx<workbook.getWorksheets().getCount(); idx ++) {

Worksheet sheet = workbook.getWorksheets().get( idx );
System.out.println(“sheet” + (idx+1) + “/” + workbook.getWorksheets().getCount());

ImageOrPrintOptions imgOptions = getImageOptions();

SheetRender sr = new SheetRender(sheet, imgOptions);
for(int i = 0;i<sr.getPageCount();i++) {

System.out.println(“page” + (i+1) + “/” + sr.getPageCount() );

//Create a file inside ‘baseDirectory’ folder
convertedFile = getSplitFileName( getSplitFilePrefix(), baseDirectory, part );

fileList.add(convertedFile);

//write into this file one part of the PDF
sr.toImage(i, convertedFile.getAbsolutePath() );

part++;
}
}
//zip files
convertedFile = getConvertedZIPFile( file );

//zip folder named ‘baseDirectory’ into a file named ‘convertedFile’
ZipUnzipHelper.zipFile( baseDirectory, convertedFile );


private ImageOrPrintOptions getImageOptions() {
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setHorizontalResolution( 400 );
imgOptions.setVerticalResolution( 400 );
imgOptions.setOnePagePerSheet( true );
imgOptions.setImageFitToPage( false );
imgOptions.setTiffCompression(TiffCompression.COMPRESSION_CCITT_4);
imgOptions.setImageFormat(ImageFormat.getTiff());
return imgOptions;
}

Hi,


Thanks for the new template file and output TIFF image files.

Good to know that your issue is sorted out by the suggested code.

Regarding your recent concern/issue (tiff is being truncated), Well, this is due to the printable area (which does not cover your whole sheet) set for the third sheet, see the screenshot for your reference:
http://prntscr.com/f41wk1

so, either you remove this printable area in MS Excel manually or remove it via Aspose.Cells APIs in code. For your information, Aspose.Cells does render image files (of sheet(s)) as per the print preview shown in MS Excel manually. See the line of code (in bold) which would work for your needs:
e.g
Sample code:

Worksheet sheet = workbook.getWorksheets().get( idx );
sheet.PageSetup.PrintArea = “”;

Hope, this helps a bit.

Thank you.
Hi,

GSComponent:

1) when I use save method on the workbook
workbook.save(workbookFilePath, SaveFormat.TIFF);
I get a multipaged tiff file but quality is low.

Well, you can try to use:
e.g
Sample code:

..........
WorkbookRender wr = new WorkbookRender(workbook, imageOrPrintOptions);
wr.toImage(TMP_DIR + "test_Java.tiff");

so, you can get a higher quality image by setting HorizontalResolution and VerticalResolution of ImageOrPrintOptions class.

Thank you.

Dear Amjad,

Your advice helped a lot!
Thank you very much for all your support!

Best Regards,
–George Souliotis