【文件】java生成PDF文件
package test;
import java.awt.Color;
import java.io.FileOutputStream; import org.junit.Test; import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter; public class PDFTest { @Test
public void writeSimplePdf() throws Exception{ //1.新建document对象
//第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档 写入到磁盘中。
//创建PdfWriter 对象第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\TMP\\ITextTest.pdf"));
//3.打开文档
document.open();
//4.向文档中添加内容
//通过com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
//5.关闭文档
document.close();
} /**
* 添加含有章节的pdf文件
* @throws Exception
*/
@Test
public void writeCharpter() throws Exception{
//新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
//建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\TMP\\ITextTest.pdf")); //标题
document.addTitle("Hello mingri example");
//作者
document.addAuthor("wolf");
//主题
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
//打开文件
document.open();
//向文档中添加内容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.defaultEncoding, 10,Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new Paragraph("Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0,255)));
//新建章节
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
//关闭文档
document.close();
} }
【文件】java生成PDF文件的更多相关文章
- JAVA生成PDF文件
生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
- java生成pdf文件 --- Table
Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...
- java生成PDF文件(itext)
itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf-5.4.3.jar ToP ...
- pdf文件流生成pdf文件
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Code.Login Starr_mode ...
- python反编译chm文件并生成pdf文件
# -*- coding: utf-8 -*- import os import os.path import logging import pdfkit original_chm = r'C:\Us ...
- java调用wkhtmltopdf生成pdf文件,美观,省事
最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...
- linux下编译bib、tex生成pdf文件
实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...
随机推荐
- asp.net 后台<%@ Page%> page指令属性
aspx文件有如下一行代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" ...
- 浅析python日志重复输出问题
浅析python日志重复输出问题 问题起源: 在学习了python的函数式编程后,又接触到了logging这样一个强大的日志模块.为了减少重复代码,应该不少同学和我一样便迫不及待的写了一个自己的日 ...
- virtualenv 模块
作用 安装虚拟环境 下载组件 pip3 install virtualenv 使用 命令行创建使用 """ 创建虚拟环境 """ virtu ...
- 【luogu4320】道路相遇 (圆方树 + LCA)
Description 给你一张\(~n~\)个点\(~m~\)条边的无向图,保证无重边无自环, 共\(~q~\)组询问求\(~x~\)到\(~y~\)的路径上必经的点数. Solution ...
- Oracle drop table 和 truncate table对grant授权的影响
[oracle@crl ~]$ rlwrap sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Production on Tue May 16 14: ...
- 自学Linux Shell11.5-执行数字运算
点击返回 自学Linux命令行与Shell脚本之路 11.5-执行数字运算 1. Shell基本运算符 Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符 ...
- emwin 之消息 WM_INIT_DIALOG
@2018-08-09 小记 消息 WM_INIT_DIALOG 在创建窗口时首先发送且只在创建窗口时发送即只发送这一次
- LoadRunner断言:正确结果有多种情况
用过lr的都知道,我们可以用web_reg_find这个注册函数判断某个请求的返回中是否有我们要找的内容. 但是,如果正常的情况有多种,该如何断言呢? 设置多个检查点 把每个检查点获取到的结果数量相加 ...
- bzoj4571/luogu3293 美味 (主席树+贪心)
首先想到建出可持久化trie树然后在上面贪心,但是它加了一个数所以不能这么做 但依然可以贪心,仿照上面那个的过程,如果设y是在第i位上^b是1的数(前面的位数已经贪好了),我只要在[l,r]范围内能有 ...
- [模板]Link-Cut-Tree动态树
做法以后再补,先写一些注意事项. 做法以后也不补了,直接看这个吧.https://www.cnblogs.com/candy99/p/6271344.html 1.rotate其实是最容易写错的地方( ...