2014-10-02 10 views
0
내가 PDFRenderer.jar을 사용하고

이 내가 인쇄 형식을 설정하는 데 사용하고있어서,동향 PDF 인쇄의 첫 페이지 만 설정 - 자바

private void initialize(byte[] pdfContent, String jobName) throws IOException, PrinterException { 
     ByteBuffer bb = ByteBuffer.wrap(pdfContent); 
     // Create PDF Print Page 
     PDFFile pdfFile = new PDFFile(bb); 
     PDFPrintPage pages = new PDFPrintPage(pdfFile); 

     // Create Print Job 
     pjob = PrinterJob.getPrinterJob(); 
     PageFormat pf = PrinterJob.getPrinterJob().defaultPage(); 

     pf.setOrientation(PageFormat.PORTRAIT); 
     pjob.setJobName(jobName); 

     Book book = new Book(); 
     book.append(pages, pf, pdfFile.getNumPages()); 
     pjob.setPageable(book); 

//   to remove margins 
     Paper paper = new Paper(); 
     paper.setSize(paper.getWidth(), paper.getHeight()); 
     paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); 

     pf.setPaper(paper); 
    } 

문제는, 방향 인물이다 pdf의 첫 페이지 만 설정하면 두 번째 페이지는 코드에서 설정하지 않은 REVERSE_LANDSCAPE이됩니다.

아이디어가 있으십니까?

답변

0

마침내, 나는 그것이

우리는 java.awt.print.PageFormat에 속성을 설정에서 인쇄 형식을 설정할 수 없습니다 발견, 우리는 .This는 PDF 형식의 파일 인쇄를 (텍스트 정렬)을 만들 java.awt.print.Printableprint(Graphics g, PageFormat format, int index) 방법을 오버라이드 (override)하는해야합니다.

따라서이, 그래서, 여기에 PDF 인쇄를위한 전체 코드가 (프린터에 직접 인쇄를 지원하지 않습니다) PDF 렌더러를 사용하고,

public int print(Graphics g, PageFormat format, int index) throws PrinterException { 
     int pagenum = index + 1; 
     if ((pagenum >= 1) && (pagenum <= file.getNumPages())) { 
      Graphics2D g2 = (Graphics2D) g; 
      PDFPage page = file.getPage(pagenum); 

      // fit the PDFPage into the printing area 
      Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(), 
        (int) format.getImageableWidth(), (int) format.getImageableHeight()); 
      g2.translate(0, 0); 
      PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null); 
      try { 
       page.waitForFinish(); 
       pgs.run(); 
      } catch (InterruptedException ie) { 
       // nothing to do 
      } 
      return PAGE_EXISTS; 
     } else { 
      return NO_SUCH_PAGE; 
     } 
    } 

재정의 인쇄 방법이다

public static void main(String[] args) throws IOException, PrinterException { 
    if (args.length != 1) { 
     System.err.println("The first parameter must have the location of the PDF file to be printed"); 
    } 

    FileInputStream fis = new FileInputStream(new File("x.pdf"));//file path 

    PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF"); 
    printPDFFile.print(); 
} 

public static PrintService setPrintService(String argPrintServiceName) throws PrinterException { 
    PrintService psr = null; 
    PrintService[] printServices = PrinterJob.lookupPrintServices(); 
    int i; 
    for (i = 0; i < printServices.length; i++) { 
     if (printServices[i].getName().equalsIgnoreCase(argPrintServiceName)) { 
      psr = printServices[i]; 
      break; 
     } 
    } 
    if (i == printServices.length) { 
     throw new PrinterException("Invalid print service name: " + argPrintServiceName); 
    } 
    return psr; 
} 

/** 
* Constructs the print job based on the input stream 
* 
* @param inputStream 
* @param jobName 
* @throws IOException 
* @throws PrinterException 
*/ 
public PrintPdf(InputStream inputStream, String jobName) throws IOException, PrinterException { 
    byte[] pdfContent = new byte[inputStream.available()]; 
    inputStream.read(pdfContent, 0, inputStream.available()); 
    initialize(pdfContent, jobName); 
} 

/** 
* Initializes the job 
* 
* @param pdfContent 
* @param jobName 
* @throws IOException 
* @throws PrinterException 
*/ 
private void initialize(byte[] pdfContent, String jobName) throws IOException, PrinterException { 
    ByteBuffer bb = ByteBuffer.wrap(pdfContent); 
    /* Create PDF Print Page*/ 
    PDFFile pdfFile = new PDFFile(bb); 
    PDFPrintPage pages = new PDFPrintPage(pdfFile); 

    /* Create Print Job */ 
    pjob = PrinterJob.getPrinterJob(); 
    pf = PrinterJob.getPrinterJob().defaultPage(); 

    pf.setOrientation(PageFormat.PORTRAIT); 
    pjob.setJobName(jobName); 

    Book book = new Book(); 
    book.append(pages, pf, pdfFile.getNumPages()); 
    pjob.setPageable(book); 

    /* to remove margins */ 
    Paper paper = new Paper(); 
    paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); 

    pf.setPaper(paper); 
} 

public void print() throws PrinterException { 
    // Send print job to default printer 
    pjob.print(); 
} 

/** 
* Class that actually converts the PDF file into Printable format 
*/ 
class PDFPrintPage implements Printable { 

    private PDFFile file; 

    PDFPrintPage(PDFFile file) { 
     this.file = file; 
    } 

    public int print(Graphics g, PageFormat format, int index) throws PrinterException { 
     int pagenum = index + 1; 
     if ((pagenum >= 1) && (pagenum <= file.getNumPages())) { 
      Graphics2D g2 = (Graphics2D) g; 
      PDFPage page = file.getPage(pagenum); 

      // fit the PDFPage into the printing area 
      Rectangle imageArea = new Rectangle((int) format.getImageableX(),(int)format.getImageableY(),(int) format.getImageableWidth(), (int) format.getImageableHeight()); 
      g2.translate(0, 0); 
      PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null); 
      try { 
       page.waitForFinish(); 
       pgs.run(); 
      } catch (InterruptedException ie) { 
       // nothing to do 
      } 
      return PAGE_EXISTS; 
     } else { 
      return NO_SUCH_PAGE; 
     } 
    } 

} 

감사합니다 버그를 찾으려고 시도한 사람이 누구인지 그리고 내 수석 엔지니어가 이걸 찾도록 도와 주었다면.