Java 怎么使用Apache PDFBox创建PDF文档

2019年7月29日19:59:37Java 怎么使用Apache PDFBox创建PDF文档已关闭评论 844 2470字阅读8分14秒

1.简介

Java 怎么使用Apache PDFBox创建PDF文档?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;

public class Simple {

    public static void main(String[] args) throws Exception {
        String outputFileName = "pdfbox/pdf/write/Simple3.pdf";
        if (args.length > 0)
            outputFileName = args[0];

        
        PDDocument document = new PDDocument();
        PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
        
        PDRectangle rect = page1.getMediaBox();
        
        document.addPage(page1);

        
        PDFont fontPlain = PDType1Font.HELVETICA;
        PDFont fontBold = PDType1Font.HELVETICA_BOLD;
        PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE;
        PDFont fontMono = PDType1Font.COURIER;

        
        PDPageContentStream cos = new PDPageContentStream(document, page1);

        int line = 0;

        
        cos.beginText();
        cos.setFont(fontPlain, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50 * (++line));
        cos.drawString("Hello World");
        cos.endText();

        cos.beginText();
        cos.setFont(fontItalic, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50 * (++line));
        cos.drawString("Italic");
        cos.endText();

        cos.beginText();
        cos.setFont(fontBold, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50 * (++line));
        cos.drawString("Bold");
        cos.endText();

        cos.beginText();
        cos.setFont(fontMono, 12);
        cos.setNonStrokingColor(Color.BLUE);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50 * (++line));
        cos.drawString("Monospaced blue");
        cos.endText();

        
        cos.close();

        PDPage page2 = new PDPage(PDPage.PAGE_SIZE_A4);
        document.addPage(page2);
        cos = new PDPageContentStream(document, page2);

        
        cos.setNonStrokingColor(Color.RED);
        cos.fillRect(10, 10, 100, 100);

        
        cos.setLineWidth(1);
        cos.addLine(200, 250, 400, 250);
        cos.closeAndStroke();
        cos.setLineWidth(5);
        cos.addLine(200, 300, 400, 300);
        cos.closeAndStroke();

        
        try {
            BufferedImage awtImage = ImageIO.read(new File(
                    "pdfbox/pdf/write/Duke_Wave.png"));
            PDXObjectImage ximage = new PDPixelMap(document, awtImage);
            float scale = 0.5f; 
            cos.drawXObject(ximage, 100, 400, ximage.getWidth() * scale,
                    ximage.getHeight() * scale);
        } catch (FileNotFoundException fnfex) {
            System.out.println("No image for you");
        }

        
        cos.close();

        
        document.save(outputFileName);
        document.close();
    }
}

 
继续阅读
编程十万个怎么办