pdfbox 創建pdf文檔

pdfbox創建pdf文檔,hello world!

記下代碼:

public class CreatPdfFile {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        PDDocument document = null;
        try{
            document = new PDDocument();
            PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);
            System.out.println();
            
            PDFont font = PDType1Font.HELVETICA_BOLD;
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            
            contentStream.beginText();
            contentStream.setFont(font, 20);
            contentStream.newLineAtOffset(100, 100);
            //contentStream.showText("Hello World !");
            //contentStream.newLine();
            contentStream.showText("Hello World !");
            //contentStream.showTextWithPositioning(args);
            contentStream.newLineAtOffset(0, 200);
            contentStream.setFont(font, 200);
            contentStream.showText("The individual calls to add resources such as PDResources.addFont(PDFont font) and PDResources.addXObject(PDXObject xobject, String prefix) have been replaced with PDResources.add(resource type) where resource type represents the different resource classes such as PDFont, PDAbstractPattern and so on. The add method now supports all the different type of resources available.");
            contentStream.endText();
            
            
            contentStream.moveTo(0, 0);
            contentStream.lineTo(300, 300);
            contentStream.stroke();
            //contentStream.drawLine(0, 0, 100, 100);
            contentStream.close();
            document.save("./data/practice/HelloWorld.pdf");
            
            System.out.println(PDRectangle.A4.getWidth());
            System.out.println(PDRectangle.A4.getHeight());
            System.out.println(PDRectangle.A4.getLowerLeftX());
            System.out.println(PDRectangle.A4.getLowerLeftY());
            System.out.println(PDRectangle.A4.getUpperRightX());
            System.out.println(PDRectangle.A4.getUpperRightY());
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            document.close();
        }
        
        return;
    }
}

 

 

發現pdfbox的座標系統,遠點在左下角!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章