Java 写入pdf文件
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Chapter;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFDemo {
private Color black = new Color(, , ); // 黑色
private Color red = new Color(, , ); // 红色
private Color blue = new Color(, , ); // 蓝色
private int bold = Font.BOLD; // 粗体
private int normal = Font.NORMAL; // 正常字体
private int italic = Font.ITALIC; // 斜体
private int boldItalic = Font.BOLDITALIC; // 粗斜体
private float setting = ; // 首行缩进参数
public Document createDoc(String filename) throws Exception {
// 新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, , , , );
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter.getInstance(document, new FileOutputStream(filename));
return document;
}
public void writePdf(String filename, String imgPath) throws Exception {
Document document = createDoc(filename); // 打开文档
document.open(); // 文档里写入
document.add(convertParToChinese("红色字体", , bold, red));
document.add(new Paragraph("\n"));
document.add(convertParToChinese("黑色", , boldItalic, black));
document.add(new Paragraph("\n"));
document.add(convertParToChinese("蓝色", , normal, blue));
document.add(new Paragraph("\n"));
// 文档写入图片
if (checkFile(imgPath)) {
Image image = writeImg(imgPath);
document.add(image);
document.add(new Paragraph("\n"));
}
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n")); // 生成三列表格
PdfPTable table = new PdfPTable(); // 设置表格具体宽度
table.setTotalWidth(); // 设置每一列所占的长度
table.setWidths(new float[] { 50f, 15f, 25f });
PdfPCell cell1 = new PdfPCell();
Paragraph para = new Paragraph("aaaaa");
cell1.setPhrase(para);
table.addCell(cell1);
table.addCell(new PdfPCell(new Phrase("IText")));
table.addCell(new PdfPCell(new Phrase("IText")));
document.add(table);
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n")); // PDF同行显示
Paragraph par = new Paragraph();
Chunk chunk1 = new Chunk(convertChunkByChinese("考试分数:", , bold, black));
Chunk chunk2 = new Chunk(convertChunkByChinese("", , bold, red));
par.add(chunk1);
par.add(chunk2); // 设置整体缩进
par.setFirstLineIndent(setting); // 居中
Paragraph centerPar = convertParToChinese("剧中测试", , italic, black);
centerPar.setAlignment(Element.ALIGN_CENTER);
document.add(par); // 新建章节
// //节标题
Paragraph chapterTitle = new Paragraph(convertParToChinese("章节标题", , boldItalic, blue));
Chapter chapter1 = new Chapter(chapterTitle, );
chapter1.setNumberDepth();
for (int i = ; i < ; i++)
{
Paragraph p = new Paragraph((i + ) + "test!!!!!");
chapter1.add(p);
}
document.add(chapter1); // 5.关闭文档
document.close();
}
public Image writeImg(String imgPath) throws Exception {
Image img = Image.getInstance(imgPath); // 控制图片大小
img.scaleAbsolute(, );
return img;
}
public boolean checkFile(String path) {
File file = new File(path);
if (file.exists()) {
return true;
}
return false;
}
public static Paragraph convertParToChinese(String text, int fontsize, int fontStyle, Color color)
throws Exception {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle, color);
Paragraph graph = new Paragraph(text, fontChinese);
return graph;
}
public Chunk convertChunkByChinese(String text, int fontsize, int fontStyle, Color color) throws Exception {
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle, color);
Chunk chunk = new Chunk(text, fontChinese);
return chunk;
}
public static void main(String[] args) throws Exception {
PDFDemo pdfDemo = new PDFDemo();
pdfDemo.writePdf("F:\\大数据\\test.pdf",
"C:\\Users\\lenovo\\Pictures\\心2.png");
} }
转载自:http://blog.sina.com.cn/s/blog_c4ffa28f0102wfeq.html
Java 写入pdf文件的更多相关文章
- java写入excel文件poi
java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...
- java生成pdf文件 --- Table
Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...
- 【文件】java生成PDF文件
package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
- JAVA生成PDF文件
生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...
- Java 合并PDF文件
处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档.例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电子档的时候,需要合二为一 ...
- JAVA中 PDF文件转成TIFF文件的2种方式
由于在工作中使用到了PDF->TIFF的技术,所以稍微研究了一下实现方式,通过资料查阅,暂时发现了2种方式,2种方式有所区别:第一种方式转化后的tiff文件是黑白的,第二种方式转化后的tiff文 ...
- Java 创建PDF文件包的2种方法
1. 概述 PDF文件包可方便在仅打开一个窗口的情况下阅读多个文档,通过将多个PDF文档或其他非PDF文档封装在一起,打开文件包后可以随意切换查看文件包中的文档,在需要编辑更改的情况,也可以打开文本包 ...
随机推荐
- Visual Studio 2010调试本地 IIS 站点
点击vs的Debug-Attach to Process选中 w3wp.exe,然后点击Attach, vs便进入debug模式.
- 软件工程:Java实现WC.exe基本功能
项目相关要求 GitHub地址:https://github.com/3216004716/WC 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处 ...
- tornado+nginx上传视频文件
[http://arloz.me/tornado/2014/06/27/uploadvideotornado.html] [NGINX REFRER:Nginx upload module] 由于to ...
- CentOS 系统管理与yum软件仓库搭建
重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 ...
- Java以邮件附件的方式发送excel文件
String to = "xxx@qq.com"; // 收件人的QQ邮箱 String from = "xxx@qq.com"; // 发件人的QQ邮箱 St ...
- 3月份GitHub上最热门的Java开源项目
今天,我们来盘点3月份GitHub上最热门的Java项目的时候了,如果你每月都有关注猿妹发布的排行榜,那么本月的Java项目对你来说一定不陌生,这些都是曾经多次出现在榜单中的项目: 1 advance ...
- &(引用) 和 *(指针)
引用: 引用相当于给引用的对象起一个别名,对引用的操作即是对原对象的操作. 使用方式: int cnt = 1024; int &x = cnt; 一般情况下引用的类型要和被引用对象一致(例外 ...
- 【spring源码】bean的实例化(转载)
首先来看一段代码,看过上一节的朋友肯定对这段代码并不陌生.这一段代码诠释了Spring加载bean的完整过程,包括读取配置文件,扫描包,加载类,实例化bean,注入bean属性依赖. 上一节介绍了Sp ...
- 条目十四《使用reserve来避免不必要的重新分配》
条目十四<使用reserve来避免不必要的重新分配> 使用vector和string的插入元素的时候,我们是不用担心内存问题的(只要不超过容器的max_size).因为底层有分配子管理内存 ...
- nandflash之基本特性
nandflash作为嵌入式中的”磁盘”, 被广泛的应用, 以(K9F2G08U0B)为例,其他型号都差不多 nandflash的结构 nandflash的结构有页(page), block(块)的概 ...