javafx 创建windows程序记录8 生成/写入PDF文件

导包

pdfbox-app-2.0.6.jar

		 PDDocument doc = null;
        PDPage page1 = null;
       try {
            doc = new PDDocument();
            PDRectangle  pdRectangle = new PDRectangle(page_width, page_height);  //设置PDF页面宽高
            page1 = new PDPage(pdRectangle);
            doc.addPage(page1);  //文档添加页面1

			//doc = PDDocument.load(new File(inputFile));  //加载某pdf文档
			// PDPageTree pages = doc.getPages();  //获取文档所有页对象
			PDPage page = doc.getPage(pageIndex);  //获取文档某一页,
			
			PDPageContentStream contentStream = new PDPageContentStream(doc, page,PDPageContentStream.AppendMode.APPEND,true,false); //写入对象 向pdf写入内容全靠它

		//向pdf 写入图片
          PDImageXObject pdImage=PDImageXObject.createFromFile(images[i].getPath(),doc);
          //参数1 图片  
          //参数2 3 图片放于pdf位置  默认位置为 pdf页面左下角为0 0  
          //参数4 图片宽度
          //参数5 图片高度
          contentStream.drawXObject(pdImage, x, y, image_width, image_height);
        
        // 画实心矩形
        contentStream.setStrokingColor(00, 00, 00); //设置颜色
            //参数1 2 图片放于pdf位置  默认位置为 pdf页面左下角为0 0  
          //参数3 图片宽度
          //参数4 图片高度
         contentStream.addRect(x,  y, width, height); //参数说明与
         contentStream.fill();
 		
 		// 画线
		contentStream.drawLine(xStart, yStart, xEnd, yEnd); 
		
		//写字
		PDFont font= PDType1Font.TIMES_BOLD_ITALIC;
         contentStream.beginText();
          contentStream.newLineAtOffset(x,y); //位置便宜
          // 设置字体type,size
          contentStream.setFont(font, 12);
          contentStream.setNonStrokingColor(0, 0, 0);
          // 插入文本
          contentStream.showText("text");
          contentStream.endText();

		contentStream.close();
        doc.save(outputFile);  //保存生成的PDF
      }catch (Exception e){

       }finally {
       	System.out.println("生成完成");
	}

打印PDF

// file 要打印的文件   printNumber打印份数
    public void PDFprint0(File file ,int printNumber) throws Exception {
        printNumber = printNumber == 0 ? 1:printNumber;
        PDDocument document = null;
        PrinterJob printJob = null;
        try {
                PrintService printService = printServiceList.get(serviceIndex);  //选择设置的打印机
                System.out.println(printService.getName());
           
                document = PDDocument.load(file);
                printJob = PrinterJob.getPrinterJob();
                printJob.setJobName(file.getName());
                
                if(printService!=null){
                    printJob.setPrintService(printService);
                }else{
                    return;
                }

                printJob.setPageable(new PDFPageable(document));
                //设置纸张及缩放
                PDFPrintable pdfPrintable = new PDFPrintable(document); //Scaling.SCALE_TO_FIT 适应适应
                //设置多页打印
                Book book = new Book();
                PageFormat pageFormat = new PageFormat();
                //设置打印方向
                pageFormat.setOrientation(PageFormat.PORTRAIT);//纵向
                pageFormat.setPaper(getPaper());//设置纸张
                book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
                //          book.append(pdfPrintable, pageFormat, 1);
                printJob.setPageable(book);
                printJob.setCopies(printNumber);//设置打印份数
                //添加打印属性
                HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
                pars.add(Sides.ONE_SIDED); //设置单双页
                printJob.print(pars);
            
        }catch (Exception e){
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                     DialogUtil.showDialog(primaryStage,"警告","打印出错,请检查打印机链接","确定");
                }
            });
        }finally {
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

public Paper getPaper() {
        Paper paper = new Paper();
        // 默认为A4纸张,对应像素宽和高分别为 595, 842
//        int width = (int) page_width;
        int width = 595;
//        int height = (int) page_height;
        int height =842;
        // 设置边距,单位是像素,10mm边距,对应 28px
        int marginLeft = 0;
        int marginRight = 0;
        int marginTop = 0;
        int marginBottom = 0;
        paper.setSize(width, height);
        // 下面一行代码,解决了打印内容为空的问题
        paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
        return paper;
    }

版权声明:本文为weixin_41648633原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。