pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Hewson <j...@jahewson.com>
Subject Re: Beginner's question....
Date Tue, 09 Jun 2015 19:07:48 GMT
Hi,

> On 9 Jun 2015, at 01:45, thierry.mathey@havells-sylvania.com wrote:
> 
> Hello,
> 
> I'm a french beginner either in Java and using PDFBox...
> 
> I want to use PDFBox to print PDF files stored on a disk.
> No problem to get the PDF from disk, but my problem is to print the PDF
> file with correct attributes...
> 
> I have "simple" PDF files (format A4, portrait, no duplex print), but I
> have also other attributes (A3 and/or Landscape and/or duplex) that can
> change.
> And I want a complete Silent print…

This is possible.

> I've tried with this class, but this doesn't work :
> 
> public class PDFmanager {
> 	private String filename;
> 	private String size;
> 	//private String orientation;
> 	private Integer copies;
> 	private PrintService printer;
> 	private PrintRequestAttributeSet aset;
> 	private DocAttributeSet asetD;
> 	private DocFlavor flavor;
> 
> 	public PDFmanager(String size, boolean duplex) {
> 		super();
> 		flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
> 		PrintRequestAttributeSet aset = new
> HashPrintRequestAttributeSet();
> 
> 		if (size == "A4"){
> 			aset.add(MediaSizeName.ISO_A4);
> 			;
> 		} else if (size == "A3"){
> 			aset.add(MediaSizeName.ISO_A3);
> 
> 		}
> 		if (duplex){
> 			aset.add(Sides.TWO_SIDED_LONG_EDGE);
> 
> 		} else {
> 			aset.add(Sides.ONE_SIDED);
> 
> 		}
> 
> 		PrintService[] pservices =
> 		PrintServiceLookup.lookupPrintServices(null, null);
> 
> 
> 		if (pservices.length > 0) {
> 		   for (PrintService selprinter : pservices) {
> 		        System.out.println("Printer: " + selprinter.getName()+"
> Attr : "+ selprinter.getAttributes().toString());
> 		        /* TODO : modifier MYPRINTER par nom imprimante STE */
> 		        if (selprinter.getName().equals("\\\\svgendc01
> \\PNGEN0029")) {
> 	                printer = selprinter;
> 		        }
> 		   }
> 		}
> 	}
> 
> 
> 	public void printPDF() throws IOException, PrinterException {
> 
> 	    if (!(printer == null)) {
> 		PrinterJob job = PrinterJob.getPrinterJob();
> 	    job.setCopies(copies);
> 
> 	    job.setPrintService(printer);
> 	    PDDocument doc = PDDocument.load(filename);
> 	    doc.silentPrint(job);
> 	    //doc.print();
> 	    doc.close();
> 	    } else {
> 	    	JOptionPane.showMessageDialog(null,
> 	                "Pas d'imprimante valide pour l'impression des
> Notices !",
> 	                "Erreur",
> 	                JOptionPane.WARNING_MESSAGE);
> 	    }
> 	}
> 
> 
> 
> 	public String getFilename() {
> 		return filename;
> 	}
> 
> 	public void setFilename(String filename) {
> 		this.filename = filename;
> 	}
> 
> 	public String getSize() {
> 		return size;
> 	}
> 
> 	public void setSize(String size) {
> 		this.size = size;
> 	}
> 
> 	public Integer getCopies() {
> 		return copies;
> 	}
> 
> 	public void setCopies(Integer copies) {
> 		this.copies = copies;
> 	}
> 
> 	public PrintService getPrinter() {
> 		return printer;
> 	}
> 
> 	public void setPrinter(PrintService printer) {
> 		this.printer = printer;
> 	}
> 
> }
> 
> I use  INPUT_STREAM.AUTOSENSE   because  INPUT_STREAM.PDF  doesn't work...
> And the printer I select can print with any of selected attributes (Kyocera
> FS-C8100DN).

You’re not using Java's printing API correctly. An INPUT_STREAM DocFlavor is for
client formatted print data and cannot be used with Printable / Pageable which are
for use with service formatted print data.

See: http://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/printing2d.fm2.html <http://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/printing2d.fm2.html>
And: https://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/printing2d.fm1.html <https://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/printing2d.fm1.html>

However, you never do anything with the “flavor” variable anyway.

> The attributes were used to select the Printer Service, but no printer was
> selected when I did this. Now, they are not used…

Actually the attributes are never used in your code.

> But, the printing is not correct (no A3, content not adjusted to page
> format...).
> Is it possible to set attributes at the moment I start the print ?

You've created a PrintRequestAttributeSet however you never do anything
with it. You can pass it to the PrinterJob as follows:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDPageable(doc));
job.print(aset);

— John

> 
> Maybe I'm not using PDFBox the right way....
> 
> What can I do ?
> 
> Cordialement / Best regards
> 
> 
> Thierry MATHEY
> IT EDI Expert
> Havells-Sylvania
> 
> thierry.mathey@havells-sylvania.com
> 
> T +33 (0)1 55 51 11 64
> M +33 (0)6 71 01 87 06
> This e-mail and any attachments may contain confidential and privileged information.
If you are not the intended recipient, please notify the sender immediately by return e-mail,
delete this e-mail and destroy any copies. Any dissemination or use of this information by
a person other than the intended recipient is prohibited and may be unlawful.


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message