zoukankan      html  css  js  c++  java
  • PDFBox之文档创建

    1.创建一个空的PDF

    下面的小例子表示如何使用PDFBox来创建一个新的PDF文档。

     // 创建一个空的文档
    PDDocument document = new PDDocument();
    
    // 创建一个空的Page然后添加到文档中
    PDPage blankPage = new PDPage();
    document.addPage( blankPage );
    
    // 保存文档
    document.save("BlankPage.pdf");
    
    // 一定要确保最后文档是别关闭的
    document.close();
    
    

    1.1举例说明

    	public static void createPDFFile() {
    		
    		PDDocument document = null;
    		PDPage blankPage = null;
    
    		try {
    			document = new PDDocument();
    			blankPage = new PDPage();
    			
    			document.addPage(blankPage);
    			document.save("D:" + File.separator + "pdfBox.pdf");
    			
    		} catch (COSVisitorException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				document.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    

    2.使用PDF字体的Hello World

    // 创建一个文档并且添加一个Page
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage( page );
    
    // 创建一个FONTT
    PDFont font = PDType1Font.HELVETICA_BOLD;
    
    // 创建一个待加入的文档流
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    
    // 使用选择的字体定义一个文本内容
    contentStream.beginText();
    contentStream.setFont( font, 12 );
    contentStream.moveTextPositionByAmount( 100, 700 );
    contentStream.drawString( "Hello World" );
    contentStream.endText();
    
    // 关闭内容流
    contentStream.close();
    
    // 保存结果并且关闭文档对象
    document.save( "Hello World.pdf");
    document.close();
    

    2.1举例说明

    	public static void usePdfFont() {
    
    		PDDocument document = new PDDocument();
    		PDPage page = new PDPage();
    		PDPageContentStream contentStream = null;
    	
    
    		PDFont font = PDType1Font.HELVETICA_BOLD;
    
    		try {
    			document.addPage(page);
    			contentStream = new PDPageContentStream(document, page);
    			contentStream.beginText();
    			contentStream.setFont(font, 12);
    			contentStream.moveTextPositionByAmount(100, 700);
    			contentStream.drawString("Hello World");
    			contentStream.endText();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				contentStream.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    
    		try {
    			document.save("D:" + File.separator + "Hello World.pdf");
    		} catch (COSVisitorException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				document.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
  • 相关阅读:
    JVM笔记3-java内存区域之运行时常量池
    JVM笔记2-Java虚拟机内存管理简介
    JVM笔记1-内存溢出分析问题与解决
    ActiveMq笔记1-消息可靠性理论
    python基础学习16----模块
    python基础学习15----异常处理
    在windows下搭建汇编编程环境
    python基础学习14----正则表达式
    python基础学习13----生成器&迭代器
    python基础学习12----装饰器
  • 原文地址:https://www.cnblogs.com/zhangminghui/p/4700626.html
Copyright © 2011-2022 走看看