java生成pdf文档

一.什么是PDF文档

        Adobe 公司的PDF 是 Portable Document Format( 便携文件格式 ) 的缩写, 是全世界电子版文档分发的公开实用标准。PDF 是一种通用文件格式,能够保存任何源文档的所有字体、格式、颜色和图形,而不管创建该文档所使用的应用程序和平台。

二.PDF文档的好处

   1.阅读方便:源文件小,便于浏览。

    2.特别适合打印:可以再现原稿的每一个字符,颜色以及头像。

    3.高效浏览查阅:可以加入书签,有目录,还有标注等辅助功能。

    4.加密特性:让你能够控制机密文件的访问权限,如访问密码。

     5.跨平台:不局限于文档的创建系统,只要是PDF文件IOS,Windows,linux等都可以浏览。

     ......待续......

   三.生成的PDF的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
    
public class PDFExport {

    
  public static void printPDF(String content) throws DocumentException,
            IOException {
        Document document = new Document();
 
        PdfWriter.getInstance(document, new FileOutputStream("D:/demo.pdf"));
        // BaseFont chinese = BaseFont.createFont("STSong-Light",  "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 使用windows自带的字体
        BaseFont chinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(chinese, 14, Font.NORMAL);
 
        document.addAuthor("WESTDREAM");
        document.addTitle("Test iText");
        document.addSubject("This is an iText demo");
        document.addKeywords("iText keywords");
        document.addCreator("Using iText");
        document.open();
        document.add(new Paragraph(content, font));
        document.close();
    }
 
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\pdf.txt");
        byte[] bytes = new byte[(int) file.length()];
        InputStream input = new FileInputStream(file);
        input.read(bytes, 0, (int)file.length());
        printPDF(new String(bytes,"GBK"));
    }
}

注:需要添加生成PDF的jar,此例使用       com.lowagie.text-2.1.7.jar  下载地址: http://download.csdn.net/download/yyd19921214/8598289

 其他小功能

//设置密码

    pdfWriter.setEncryption(userPassword.getBytes(), userPassword.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

//设置表格
            PdfPTable table = new PdfPTable(5);
            table.setTotalWidth(PageSize.A4.getWidth());
            table.setLockedWidth(true);
            PdfPCell cell =  null;
            table.addCell(get(cell, "期数", fontNormal));


更多精品博客:http://www.cnblogs.com/crazyjava/p/3199936.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值