I used the sample code on the web for PrintPreviewDialog. The preview window opens but it shows nothing in the window. No errors in the log file either. Any idea what might be the issue? I tried both pj.setPageable and pj.setPrintable(awPrintDoc) but neither works. Here are the codes I used:
PrinterJob pj = PrinterJob.getPrinterJob();
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, mergeDoc.getPageCount()));
if (!pj.printDialog(attributes))
{
log("not able to open print dialog");
return;
}
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(mergeDoc);
// pj.setPageable(awPrintDoc);
pj.setPrintable(awPrintDoc);
log("Starting preview dialog");
// aspose has a nice functionality of preview and print combine in one. Can test later
PrintPreviewDialog previewDig = new PrintPreviewDialog(awPrintDoc);
previewDig.setPrinterAttributes(attributes);
if (previewDig.display())
{
pj.print(attributes);
}
@mingzhu Does the problem occur with some particular document? If so, please attach the problematic document here for testing. Also, please try using a simple document like in the code example below:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello, World!!!");
PrinterJob pj = PrinterJob.getPrinterJob();
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
if (!pj.printDialog(attributes))
{
System.out.println("not able to open print dialog");
return;
}
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
// pj.setPageable(awPrintDoc);
pj.setPrintable(awPrintDoc);
System.out.println("Starting preview dialog");
// aspose has a nice functionality of preview and print combine in one. Can test later
PrintPreviewDialog previewDig = new PrintPreviewDialog(awPrintDoc);
previewDig.setPrinterAttributes(attributes);
if (previewDig.display())
{
pj.print(attributes);
}
This code properly shows print preview on my side:
I tested in a separate java code and it seems to work fine. But when I put in my java program, it seems to hang on this statement:
if (previewDig.display()) {
I’m attaching the method. You can see that I even hardcoded the Document object as you have suggested but the preview screen opens with nothing there and the debug tells me that it’s waiting for the above command.
public void merge(String odpRoot, String templateName, String mergedFileName, String dataFileURL, String verifyPrint, String autoSave) throws Exception
{
log("dataFileUrl :" + dataFileURL + "; verifyPrint : " + verifyPrint);
Document mergeDoc = new Document(ODP_HOME + templateName);
BufferedReader reader;
StringBuffer sb = new StringBuffer("");
ArrayList<String> arrayList = new ArrayList<>();
try {
URL url = new URL(dataFileURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = reader.readLine();
int j = 0;
int numOfColumns = 0;
StringBuffer records = new StringBuffer();
while (line != null) {
//log("line=" + line);
sb.append(line + "\n");
if (j == 0) {
arrayList.add(line);
numOfColumns = arrayList.get(0).split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)").length;
} else {
records.append(line + "\n");
}
j++;
line = reader.readLine();
}
reader.close();
log("numOfColumns = " + numOfColumns + "\nrecords=" + records);
String[] indRecords = records.toString().trim().split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
// log("length of indRecords = " + indRecords.length);
// for (String element : indRecords) {
// log("indRecords element=" + element);
// }
j = 0;
StringBuffer sb1 = new StringBuffer();
for (String ir : indRecords)
{
if (j < numOfColumns)
{
sb1.append(ir + ",");
j++;
}
else
{
//log("adding string " + " || " + sb1.toString().trim() + " || " + " to arrayList");
arrayList.add(sb1.toString().trim());
j = 1;
sb1 = new StringBuffer(ir + ",");
}
}
log("adding last string " + " || " + sb1.toString().trim() + " || " + " to arrayList");
arrayList.add(sb1.toString().trim());
reader.close();
} catch (IOException e)
{
log("Unsuccessful data retrieval of: " + dataFileURL);
e.printStackTrace();
}
writingDATA(sb); //writing data in ODPDAT.txt file
//log("Merge_Data :"+arrayList.toString());
String[] merge_fields = arrayList.get(0).trim().split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
//log("mergeFields size = " + merge_fields.length);
DataTable table = new DataTable();
for (String obj: merge_fields)
{
table.getColumns().add(obj);
}
for (int i = 1; i < arrayList.size(); i++)
{
String colValue = arrayList.get(i);
String[] colValues = colValue.trim().split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); ;
//log("colValues size before = " + colValues.length);
/*
writingDATAToFile(colValue, "c:\\temp\\tempFile.txt");
String filePath = "c:\\temp\\tempFile.txt";
String fileToString = readLineByLineJava8( filePath );
log("fileToString= " + fileToString);
//String[] colValues = fileToString.split(",");
//log("colValues size here = " + colValues.length);
String[] colValues = fileToString.split(",");
log("colValues size after = " + colValues.length);
*/
int j = 0;
for (String v : colValues)
{
colValues[j] = v.replaceAll("\"", "");
j++;
}
table.getRows().add((Object[])colValues);
}
// Field values from the table are inserted into the mail merge fields found in the document
File mergeFile = new File(ODP_HOME + mergedFileName);
if (!isFileClosed(mergeFile))
{
log("File is locked");
throw new FileLockedException("File is not writable");
}
else
{
mergeDoc.getMailMerge().execute(table);
mergeDoc.save(ODP_HOME + mergedFileName);
log("mergedFileName = " + ODP_HOME + mergedFileName);
// Ming testing for print preview dialog
verifyPrint = "N";
//Snippet for Opening Document
if (verifyPrint != null && verifyPrint.equalsIgnoreCase("V"))
{
log("verifyPrint");
if (Desktop.isDesktopSupported())
{
log("desktop supported");
try
{
Desktop.getDesktop().open(new File(ODP_HOME + mergedFileName));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
log("not verify print");
// ming testing printPreview
//mergeDoc = new Document("C:\\ODPHOME\\mingtest3_test.docx");
mergeDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(mergeDoc);
builder.writeln("Hello, World!!!");
PrinterJob pj = PrinterJob.getPrinterJob();
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, mergeDoc.getPageCount()));
log("pageCount is " + mergeDoc.getPageCount());
if (!pj.printDialog(attributes))
{
log("not able to open print dialog");
return;
}
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(mergeDoc);
//pj.setPageable(awPrintDoc);
pj.setPrintable(awPrintDoc);
log("Starting preview dialog");
// aspose has a nice functionality of preview and print combine in one. Can test later
PrintPreviewDialog previewDig = new PrintPreviewDialog(awPrintDoc);
log("setting printer attricutes");
previewDig.setPrinterAttributes(attributes);
log("start to display preview");
if (previewDig.display())
{
log("previewDigd.display: " + previewDig.display());
try
{
pj.print(attributes);
}
catch (PrinterException e)
{
e.printStackTrace();
}
}
log("end printPreview");
// if not auto save, remove all the files in the ODPHOME directory.
if (autoSave == null || autoSave.equalsIgnoreCase("N"))
{
log("deleting " + ODP_HOME + mergedFileName);
File mf = new File(ODP_HOME + mergedFileName);
if (!mf.delete())
{
log(mf.getName() + " failed to be deleted");
}
}
}
}
}
@mingzhu Could you please create a simple application that will allow us to reproduce the problem? I cannot reproduce the problem on my side in a simple application.
Also, please provide more information about the environment where the problem occurs.