Java control Pdf automatic printing small example

  • 2020-04-01 01:39:34
  • OfStack


public byte[] autoPrintPdf(byte[] pdf_byte) {
  ByteArrayOutputStream bos=null;
  try {
   PdfReader reader = new PdfReader(pdf_byte);
   bos = new ByteArrayOutputStream();
   PdfStamper ps = new PdfStamper(reader, bos);
   StringBuffer script = new StringBuffer(); 
   script.append("this.print({bUI: false,bSilent: true,bShrinkToFit: false});").append("rnthis.closeDoc();");
   ps.addJavaScript(script.toString());
//   ps.addJavaScript("this.print(true);");
   ps.setFormFlattening(true);
   ps.close();   
  } catch (Exception e) {
   e.printStackTrace();
  } 
  return bos.toByteArray();
 }     


Related articles: