java.lang.IllegalArgumentException: Comparison method violates its general contract! when printing workbook

Dear Aspose Support Team

When trying to print the attached file PrintIssue.xlsx from Aspose with below code, we get a java.lang.IllegalArgumentException: Comparison method violates its general contract!

File:
PrintIssue.zip (9.9 KB)

Code:
package support;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.aspose.cells.WorkbookRender;

public class PrintIssue {

	private static final String FILE = "src\\support\\PrintIssue.xlsx";

	public static void main(String[] args) throws Exception {
		if (!License.isLicenseSet())
			new License().setLicense("Aspose.Cells.lic");

		Workbook workbook = new Workbook(FILE);
		PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
		String defaultPrinterName = defaultPrintService.getName();
		ImageOrPrintOptions printOptions = new ImageOrPrintOptions();
		WorkbookRender wr = new WorkbookRender(workbook, printOptions);
		wr.toPrinter(defaultPrinterName);
	}
}

Stack Trace:
Exception in thread “main” java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(Unknown Source)
at java.util.ComparableTimSort.mergeAt(Unknown Source)
at java.util.ComparableTimSort.mergeForceCollapse(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Vector.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at sun.java2d.Spans.sortAndCollapse(Unknown Source)
at sun.java2d.Spans.intersects(Unknown Source)
at sun.print.PeekGraphics.hitsDrawingArea(Unknown Source)
at sun.print.RasterPrinterJob.printPage(Unknown Source)
at sun.print.RasterPrinterJob.print(Unknown Source)
at sun.print.RasterPrinterJob.print(Unknown Source)
at com.aspose.cells.zjx.a(Unknown Source)
at com.aspose.cells.WorkbookRender.toPrinter(Unknown Source)
at support.PrintIssue.main(PrintIssue.java:24)

The attached file PrintIssue.xlsx has been created with Aspose.
We are using Aspose Cells for Java version 20.5.

Thank you in advance for your feedback.

@awepler,

Seeing your stack trace, your issue does not seem to be related to Aspose.Cells APIs. I tested using the simplest lines of code using “Microsoft XPS Document Writer” as my printer settings and it works fine. I saved the print in .oxps file, please find the output file attached.
files1.zip (313.2 KB)

e.g
Sample code:

Workbook workbook = new Workbook("f:\\files\\PrintIssue.xlsx");
		ImageOrPrintOptions printOptions = new ImageOrPrintOptions();
		WorkbookRender wr = new WorkbookRender(workbook, printOptions);

		wr.toPrinter("Microsoft XPS Document Writer");

I think you may search internet or scan relevant source to fix your your issue. For example, see the thread for your reference:

Hope, this helps a bit.

Thank you for your feedback. Using ‘Microsoft XPS Document Writer’ I was also able to print the document. Issue was with other printers (e.g. ‘Microsoft Print to PDF’). Setting the java system property ‘useLegacyMergeSort’ to ‘true’ helped (https://forum.inductiveautomation.com/t/print-report/9714/2). Either as VM argument:
-Djava.util.Arrays.useLegacyMergeSort=true
or directly in the code:

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
Workbook workbook = new Workbook(FILE);
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
String defaultPrinterName = defaultPrintService.getName();
ImageOrPrintOptions printOptions = new ImageOrPrintOptions();
WorkbookRender wr = new WorkbookRender(workbook, printOptions);
wr.toPrinter(defaultPrinterName);

Interestingly, the property needs to be set before creating the workbook, otherwise it does not help…

Now, that I am able to print the document, I realize that it is not printed correctly. The gray background/pattern of the cells E13:K14 is not visible on the print-outs. This can also be seen on the print-out you provided in your previous answer (out.oxps in files1.zip).

@awepler,
We have observed the issue where background pattern is lost in the printout. This issue is logged for further investigation as follows:

CELLSJAVA-43217 - Printing XLSX to XPS loses background formatting

@awepler,

Please try our latest version/fix: Aspose.Cells for Java v20.6.2:
aspose-cells-20.6.2-java.zip (7.1 MB)

Your issue should be fixed in it.

Thank you for providing a fix. The gray background/pattern can now be printed successfully (even without setting the java system property ‘useLegacyMergeSort’ to ‘true’, so that issue seems to be resolved with your fix as well).

When printing to ‘Microsoft XPS Document Writer’ or ‘Microsoft Print to PDF’ the output looks OK (see OK_Microsoft_XPS_Document_Writer.oxps and OK_Microsoft_Print_to_PDF.pdf in attachment).

However, when printing to our physical printers (two: one black/white, one color) the output is not OK (see NOK_Scans_of_physical_printouts.pdf):
(i) the margins are not OK: the left margin is too small, whereas the right margin is too large
(ii) the footer is not printed
printouts.zip (1.1 MB)

While searching in the forum, I came across below support request, which might be related:
https://forum.aspose.com/t/content-is-cut-when-printing/203406

@awepler,
We have tried to reproduce this issue here but need further investigation. It is logged in our database for analysis and fix as follows:

CELLSJAVA-43224 - Issue while printing to a physical printer

@awepler,

Please try our latest fix/version: Aspose.Cells v20.6.3 (attached)
aspose-cells-20.6.3-java.zip (7.1 MB)

Please note, we could not reproduce your issue on our physical printer, please try the fix v20.6.3 and give us your feedback.

Thank you for your fast reply. We have tested your fix and it seems to resolve our issues:
(i) margins are now the same for printing from Excel or from Aspose Cells
(ii) footer is now being printed, too

@awepler,

Good to know that your issue is resolved by the new fix/version. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

The issues you have found earlier (filed as CELLSJAVA-43217,CELLSJAVA-43224) have been fixed in Aspose.Cells for Java 20.7. This message was posted using Bugs notification tool by Amjad_Sahi