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. .NET Framework各版本特性一览

    https://msdn.microsoft.com/en-us/library/bb822049.aspx .NET Framework version CL version Features In ...

  2. Git教程--廖雪峰

    Git简介 1.Git是目前世界上最先进的分布式版本控制系统(没有之一) 2.集中式和分布式版本控制系统有什么区别呢?      区别在于历史版本维护的位置:Git本地仓库包含代码库还有历史库,在本地 ...

  3. 献上一款漂亮的手写PHP验证码

    献上一款漂亮的PHP验证码,可以根据个人需求作调整,代码如下(审美观不同,欢迎吐槽): <?php /** * Author: xiongwei * Email: 695704253@qq.co ...

  4. Abp使用不同仓储连接多个数据库

    有群友说官方例子中有,无奈英文和网速太差...自己琢磨吧. 最近开发的项目中,需要从外部系统中读取一些信息,计算之后存入本地的数据库中,外部系统直接提供数据库给我..所以本地需要用到多数据库连接. 项 ...

  5. [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 6.

    1. Introduction. 1.1 Starting from part 4 I have started to discuss how to interop marshal a managed ...

  6. 七、CommonJS规范和Note.js模块概念的介绍

    在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多 ...

  7. 手动给kvm虚机挂载lvm卷

    1.查看计算节点上虚机挂载的卷 [root@xgto01n010243186070 ~]# virsh domblklist instance- Target Source ------------- ...

  8. [Algorithm]排序

    一.排序算法 1.插入排序 1) 直接插入排序:(插入类) 1 void InsertSort( ElemType R[], int n ) 2 { 3 for ( int i = 2; i < ...

  9. 解决Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker fro问题

    项目中碰到一个问题,就是将一个map转换成json格式的时候出现错误,最后排查将延迟加载关闭后成功转换,因为数据量较大,于是重新创建了一个对象进行接收. 解决办法是在配置文件中进行配置 虽然解决了这个 ...

  10. 4.SVM