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

Free Support Forum - aspose.com

Java Print Document Paper Size VIA Aspose.Words

Hello,

I am trying to print word files via Aspose.Words with set paper sizes.

My code for setting Paper Size and print so far is:

PrinterJob printJob = PrinterJob.getPrinterJob();

PrintServiceAttributeSet attributes = new HashPrintServiceAttributeSet();
attributes.add(new PrinterName(PRINTER_NAME, null));

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, attributes);
if (printServices.length > 0) {
printJob.setPrintService(printServices[0]);

AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(document);

PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();

File tempFile = new File(tempFileName);
printRequestAttributeSet.add(new Destination(tempFile.toURI()));

if(printAttributes != null) {
if(printAttributes.getPrintAttributesPaperSize().equals(“Letter”)) {
printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
}
else if(printAttributes.getPrintAttributesPaperSize().equals(“Ledger”)) {
printRequestAttributeSet.add(MediaSizeName.LEDGER);
}
else if(printAttributes.getPrintAttributesPaperSize().equals(“Legal”)) {
printRequestAttributeSet.add(MediaSizeName.NA_LEGAL);
}
else if(printAttributes.getPrintAttributesPaperSize().equals(“A4”)) {
printRequestAttributeSet.add(MediaSizeName.ISO_A4);
}
else if(printAttributes.getPrintAttributesPaperSize().equals(“A3”)) {
printRequestAttributeSet.add(MediaSizeName.ISO_A3);
}

  if(printAttributes.getPrintAttributesColor() == (short)1) {
  	printRequestAttributeSet.add(Chromaticity.COLOR);
  } else {
  	printRequestAttributeSet.add(Chromaticity.MONOCHROME);				
  }
  
  if(printAttributes.getPrintAttributesSides() == (short)1) {
  	printRequestAttributeSet.add(Sides.DUPLEX);
  } else {
  	printRequestAttributeSet.add(Sides.ONE_SIDED);			
  }			

}

printJob.setPageable(awPrintDoc);
printJob.setJobName(jobName);

printJob.print(printRequestAttributeSet);
}

Submitting these print jobs the paper size doesn’t seem to be setting. It seems all jobs will just use the default.

Any help with setting paper sizes will be helpful, thanks.

@matthewanselmo,

What is the name/brand of the printer you are getting this problem with? Have you tried some other soft/virtual printer e.g. “Microsoft XPS Document Writer”? Will you be able to list step by step procedure to reproduce this issue on our end when using some soft printer?

Also, please ZIP and upload your sample Word document (you are getting this problem with) here for testing. We will investigate the issue on our end and provide you more information.

We are using an empty printer pointing at nul: port. We are printing to file.2page document.zip (23.1 KB)

@matthewanselmo,

We are working on your query and will get back to you soon.

@matthewanselmo,

Please try using the following simple code to set the paper size of printed file:

Document doc = new Document("D:\\2page document\\2page document.docx");

for (Section sec : (Iterable<Section>) doc.getSections()){
    PageSetup ps = sec.getPageSetup();
    ps.setPaperSize(PaperSize.A4);
}

PrinterJob printJob = PrinterJob.getPrinterJob();

PrintServiceAttributeSet attributes = new HashPrintServiceAttributeSet();
attributes.add(new PrinterName("Microsoft Print to PDF", null));

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, attributes);
if (printServices.length > 0) {
    printJob.setPrintService(printServices[0]);

    AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);

    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();

    File tempFile = new File("D:\\2page document\\printed.pdf");
    printRequestAttributeSet.add(new Destination(tempFile.toURI()));

    printJob.setPageable(awPrintDoc);
    printJob.setJobName("jobName");

    printJob.print(printRequestAttributeSet);
}