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文件的更多相关文章

  1. java写入excel文件poi

    java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...

  2. java生成pdf文件 --- Table

    Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...

  3. 【文件】java生成PDF文件

    package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...

  4. Java生成PDF文件(转)

    原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...

  5. [itext]Java生成PDF文件

    一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...

  6. JAVA生成PDF文件

    生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...

  7. Java 合并PDF文件

    处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档.例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电子档的时候,需要合二为一 ...

  8. JAVA中 PDF文件转成TIFF文件的2种方式

    由于在工作中使用到了PDF->TIFF的技术,所以稍微研究了一下实现方式,通过资料查阅,暂时发现了2种方式,2种方式有所区别:第一种方式转化后的tiff文件是黑白的,第二种方式转化后的tiff文 ...

  9. Java 创建PDF文件包的2种方法

    1. 概述 PDF文件包可方便在仅打开一个窗口的情况下阅读多个文档,通过将多个PDF文档或其他非PDF文档封装在一起,打开文件包后可以随意切换查看文件包中的文档,在需要编辑更改的情况,也可以打开文本包 ...

随机推荐

  1. oracle数据库创建表

    实际工作中,在数据库中创建表是经常会用到的.我们今天呢?主要给大家来分享一下在数据库如何通过sql语句去创建表.其实,创建表很简单,只需要把数据库的数据类型和约束搞清楚就可以了,其他的就好说了.接下来 ...

  2. svn ank问题

    subvesion detected a working copy that need upgrade in 可以先在文件夹下cleapup,然后打开解决方案,在解决方案的右键选择upgrade wo ...

  3. wgs84坐标系与gcj02坐标系转换误差分布图 | Mapping the Error in Transformation between WGS84 and GCJ02 Coordinations

    国际上通用的是wgs84坐标系,而我国对于境内的坐标进行了加密,采用了gcj02坐标系,或者称为火星坐标系.亢孟军老师带的一门课<多媒体电子地图设计>要求我们从wgs84坐标系转换为gcj ...

  4. 编写高质量代码改善C#程序的157个建议——建议56:使用继承ISerializable接口更灵活地控制序列化过程

    建议56:使用继承ISerializable接口更灵活地控制序列化过程 接口ISerializable的意义在于,如果特性Serializable,以及与其像配套的OnDeserializedAttr ...

  5. 编写高质量代码改善C#程序的157个建议——建议27:在查询中使用Lambda表达式

    建议27:在查询中使用Lambda表达式 LINQ实际上是基于扩展方法和Lambda表达式的.任何LINQ查询都能通过扩展方法的方式来代替. var personWithCompanyList = f ...

  6. .net连接eDirectory,需要安全连接的解决方案

    用C#连接eDirectory ,提示: “这个请求需要一个安全的连接.” 解决办法,eDirectory禁用TLS(这方法比较猥琐) ssh连接到eDirectory服务器上,执行: ldapcon ...

  7. 【转】Android android listview的HeadView左右切换图片(仿新浪,网易,百度等切换图片)

    首先我们还是看一些示例:(网易,新浪,百度)      下面我简单的介绍下实现方法:其实就是listview addHeaderView.只不过这个view是一个可以切换图片的view,至于这个vie ...

  8. logback-记录日志

      一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配 ...

  9. c# 两个字符串,s="aeiou",s2="welcome to Quantum Asia"

    c#  两个字符串,s="aeiou",s2="welcome to Quantum Asia" 方案一: 使用while循环: static void Mai ...

  10. django-redis 使用规范

    django-redis 基于 BSD 许可, 是一个使 Django 支持 Redis cache/session 后端的全功能组件. 1,安装 django-redis 最简单的方法就是用 pip ...