利用itext生成pdf

1、引入jar包 :

2、demo1

public static void main(String[] args) throws FileNotFoundException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        String filename="D:/111.pdf" ;
        
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }

demo2

public class PDFTest2 {
    private Font FontChinese;
    public void simplePDF() {
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            FontChinese = new Font(bfChinese, 12, Font.NORMAL);
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("F:\\Hello simplePDF.pdf"));
            document.open();

PdfPTable table = new PdfPTable(4);
            table.addCell(getCell("姓名", 1, 1));
            table.addCell(getCell("董亚转", 1, 1));
            table.addCell(getCell("编号", 1, 1));
            table.addCell(getCell("", 1, 1));

table.addCell(getCell("部门", 1, 1));
            table.addCell(getCell("", 1, 1));
            table.addCell(getCell("岗位名称", 1, 1));
            table.addCell(getCell("", 1, 1));

table.addCell(getCell("到职日期", 1, 1));
            table.addCell(getCell("", 1, 1));
            table.addCell(getCell("预定离职日期", 1, 1));
            table.addCell(getCell("", 1, 1));

table.addCell(getCell("事由", 1, 3));
            table.addCell(getCell("", 3, 3));

table.addCell(getCell("部门意见", 1, 3));
            table.addCell(getCell("", 3, 3));
            document.add(table);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

private PdfPCell getCell(String cellValue, int colspan, int rowSpan) {
        PdfPCell cell = new PdfPCell();
        try {
            cell = new PdfPCell(new Phrase(cellValue, FontChinese));
            cell.setRowspan(rowSpan);
            cell.setColspan(colspan);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cell;
    }
    public static void main(String[] args) {
        PDFTest2 pt = new PDFTest2();
        pt.simplePDF();
    }

}

demo3

public static void main(String[] args) throws Exception  
            {  
                Document pdfDoc = new Document();  
                // 将要生成的 pdf 文件的路径输出流  
                FileOutputStream pdfFile =   
                    new FileOutputStream(new File("D:/222.pdf"));  
        
                // pdf 文件中的一个文字段落  
                Paragraph paragraph = new Paragraph("My first PDF file with an image ...");  
                Image image = Image.getInstance("d:/Desert.jpg");  
                  
                // 用 Document 对象、File 对象获得 PdfWriter 输出流对象  
                PdfWriter.getInstance(pdfDoc, pdfFile);  
                pdfDoc.open();  // 打开 Document 文档  
                  
                // 添加一个文字段落、一张图片  
                pdfDoc.add(paragraph);  
                pdfDoc.add(image);  
              
                pdfDoc.close();  
    }

百度云网盘

链接:http://pan.baidu.com/s/1o8IjIlW 密码: ezms

时间: 2024-08-25 00:04:59

利用itext生成pdf的相关文章

利用itext生成pdf的简单例子

一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. iText的安装非常方便,在http://www.lowagie.com/iText/download.html网站上下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了 二 .生成简单的pdf文件 1

新知识:Java 利用itext填写pdf模板并导出(昨天奋战到深夜四点,知道今天两点终于弄懂)

废话少说,不懂itext干啥用的直接去百度吧. ***************制作模板******************* 1.先用word做出界面 2.再转换成pdf格式 3.用Adobe Acrobat 打开你刚刚用word转换成的pdf 会出现如下界面 下一步 点击浏览,选择刚才你转换好的pdf 下一步 4.打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框,有的人说要改成中文字体,可是我没有改一样成功啦 5.一般情况下不需要修改什么东西,至少我没有修改哦 6.直

Java Itext 生成PDF文件

利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.OutputStream; import java.math.BigDecimal; import java.net.URL; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Arr

itext生成PDF文件报错“Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.”

最近需要写一个抽取表结构的工具,类似于powerdesigner中的表图,其中有一步用到了itext这个第三方jar包来生成pdf文件,碰到了一个问题,记录于此. 问题描述: 工程使用maven构建,pom.xml中对于jar包的定义如下: <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</vers

itextsharp利用模板生成pdf文件笔记

iTextSharp是一款开源的PDF操作类库,使用它可以快速的创建PDF文件. 中文参考网站:http://hardrock.cnblogs.com/ http://pdfhome.hope.com.cn/Article.aspx?CID=bf51a5b6-78a5-4fa3-9310-16e04aee8c78&AID=f5fe52dd-8419-4baa-ab1c-ea3f26952132 英文参考网站:http://itext.ugent.be/library/ ·  技术文章(http:

itext 生成pdf文件添加页眉页脚

原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpdf是itext的升级版本,并且itextpdf-5.5.jar以上版本算总页数不需要-1,之前版本必须-1: jfinal.jar只能用2.0版本,用3.0的会报版本过高:希望能帮到有需要的人 itextpdf-5.5.13.jar 下载地址:http://central.maven.org/mav

java使用iText生成pdf表格

转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <dependency>     <groupId>com.lowagie</groupId>     <artifactId>iText</artifactId>  

iText生成PDF的关键源码分析

对于iText的学习,基本上都是来源于官网上的例子.这和我学习easyUI.jQuery的途径都是一致的. 这也再次证明了,想用好一个软件,官方提供的例子和文档都是最佳的学习资料. 因为对方是最了解这款软件的人,设计出来的demo当然是最有应用价值的~ 这里吐槽一下,iText的demo真难找,官网上反反复复找了好多次才找到,不知道大家是否和我一样,在这里给大家一个捷径. 下面就拿官网的例子来讲一讲,首先如何生成PDF? /** * 这也就是所有demo中最核心的代码 * Creates a P

利用PDFLib生成PDF文档

本文代码生成的PDF文档效果图 一.PDF介绍 PDF是Portable Document Format的缩写,PDF文件格式是国际通用的电子文档交换事实标准,被许多国家采用作为电子文档交换.PDF文件可以在各种平台下阅读.编辑.发布.该文件格式支持字体.图像.甚至任何附件的嵌入.您可以通过免费的Adobe Acrobat Reader来阅读.编辑PDF文档. 二.PDFLib介绍 PDFLib是用于创建PDF文档的开发库,提供了简单易用的API,隐藏了创建PDF的复杂细节且不需要第3方软件的支