itext A4纸张横向创建PDF
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class Test{
public static void main(String[] as) throws Exception{
Test.testRotate();
}
/**
*itext A4纸张横向创建PDF,需要加itext jar报
*http://www.cnblogs.com/qgc88/
*/
public static void testRotate(){
Document document = new Document();
//横向
Rectangle pageSize = new Rectangle(PageSize.A4.height(), PageSize.A4.width());
pageSize.rotate();
document.setPageSize(pageSize);
// 页码
Page page = new Page();
page.setPageFormat("— %d —");
page.setRelativeY(-10);
page.setRelativeX(-35);
PdfPageEventHelper eventPage[] = new PdfPageEventHelper[1];
eventPage[0] = page;
try {
String destPath="E:\\test.pdf";
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(destPath));
if (eventPage != null) {
for (PdfPageEventHelper event : eventPage) {
writer.setPageEvent(event);
}
}
document.open();
BaseFont fontChinese = null;
try {
fontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);// 设置中文字体(支持中文显示)
} catch (Exception e) {
e.printStackTrace();
}
String titlec="意见汇总";
Font chinese12 = new Font(fontChinese, 12, Font.NORMAL);
Font chineseB12 = new Font(fontChinese, 12, Font.BOLD);
Font chinese = new Font(fontChinese, 18, Font.BOLD);
Paragraph title = new Paragraph(titlec, chinese);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
Table table = new Table(8); //8是总的列数,也可以同时指定行数和列数new Table(3,4)
table.setTableFitsPage(true);//防止跨页有段开的边框(填充整页)
table.setWidths(new int[]{5,20,10,10,10,10,5,30}); //每列比例
table.setWidth(100);//表格站table 100%
table.setBorder(0);
Cell cell = new Cell();
cell.setHeader(true);
// cell.setColspan(8);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("序号\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("项目名称\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("申报单位\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("组别\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("计划类型\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("资金(万元)\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("评分\n ", chineseB12)));
table.addCell(cell);
cell = new Cell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement((new Paragraph("专家组意见\n ", chineseB12)));
table.addCell(cell);
table.endHeaders();
for (int i = 0; i < 20; i++) {
Cell cell1 = new Cell(String.valueOf(i));
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
//max lines ,设置成1防止换行,配合cell宽度,可以实现自动截取
// cell1.setMaxLines(1);
table.addCell(cell1);
Cell cell2 = new Cell();
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.addElement(new Paragraph("项目测目测目测目测目测试1", chinese12));
table.addCell(cell2);
Cell cell3 = new Cell();
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.addElement(new Paragraph("项目目测目测测试1", chinese12));
table.addCell(cell3);
Cell cell4 = new Cell();
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.addElement(new Paragraph("http://www.cnblogs.com/qgc88/", chinese12));
table.addCell(cell4);
Cell cell5 = new Cell();
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setHorizontalAlignment(Element.ALIGN_LEFT);
cell5.addElement(new Paragraph("科技人才需求", chinese12));
table.addCell(cell5);
Cell cell6 = new Cell();
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.addElement(new Paragraph("22", chinese12));
table.addCell(cell6);
Cell cell7 = new Cell();
cell7.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
cell7.addElement(new Paragraph("100.00", chinese12));
table.addCell(cell7);
Cell cell8 = new Cell();
cell8.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell8.setHorizontalAlignment(Element.ALIGN_LEFT);
cell8.addElement(new Paragraph("主审家综合意", chinese12));
table.addCell(cell8);
}
document.add(table);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
itext A4纸张横向创建PDF的更多相关文章
- 使用iText库创建PDF文件
前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...
- itext poi 学习之旅 (1)创建pdf
从零开始学习itext 创建pdf 1.用到流进行创建的pdf import java.io.File; import java.io.FileOutputStream; import com.ite ...
- .net快速创建PDF文档 by c#
原文地址:http://www.cnblogs.com/Creator/archive/2010/03/13/1685020.html C#引用IText创建PDF文档 先引用IText 可以从 ...
- 使用Rotativa在ASP.NET Core MVC中创建PDF
在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP.NET MVC,那么Rotativa工具已经可用,我们可以使用 ...
- NET Core MVC中创建PDF
使用Rotativa在ASP.NET Core MVC中创建PDF 在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP ...
- C# /VB.NET 创建PDF项目符号列表和多级编号列表
使用项目符号和编号,可以让文档的层次结构更清晰.更有条理,也更容易突出重点.在编辑文档的过程中,我个人也比较偏爱项目标号来标注文章重点信息.在之前的文章中,介绍了如何在Word中来创建项目标号和编号列 ...
- python从TXT创建PDF文件——reportlab
使用reportlab创建PDF文件电子书一般都是txt格式的,某些电子阅读器不能读取txt的文档,如DPT-RP1.因此本文从使用python实现txt到pdf的转换,并且支持生成目录,目录能够生成 ...
- 01.在Java中如何创建PDF文件
1.简介 在这篇快速文章中,我们将重点介绍基于流行的iText和PdfBox库从头开始创建 PDF 文档. 2. Maven 依赖 <dependency> <groupId> ...
- HTML生成横向的PDF
HTML生成PDF请参照:https://www.cnblogs.com/yunfeiyang-88/p/10984740.html 如要生成横向的PDF:在html模板的style标签里面加入@pa ...
随机推荐
- cdlinux
xset q xset s 6000 xset -dpms ntpdate time.nist.gov date
- iOS之WKWebView
Xcode8发布以后,编译器开始不支持IOS7,所以很多应用在适配IOS10之后都不在适配IOS7了,其中包括了很多大公司,网易新闻,滴滴出行等.因此,我们公司的应用也打算淘汰IOS7. 支持到IOS ...
- ES6新数据类型map与set
一.map,简单的键值对映射,具有很快的查找速度 1. 初始化map,map的键名可以使用其他数据类型,对象的属性名只能使用字符串或symbol 使用二维数组 var m = new Map([['n ...
- 【支付宝支付】扫码付和app支付,回调验证签名失败问题
在检查了参数排序,编码解码,文件编码等问题后,发现还是签名失败,最后找出原因: 扫码付和app支付采用的支付宝公钥不一样 Pid和公钥管理里面: 开放平台密钥界面和开放平台应用界面的密钥应该一 ...
- The US in understimating Huawei, says founder Ren zhengfei
Huawei Founder Ren Zhengfei has downplayed the impact of the US executive order that cripple Huawei' ...
- MySQL学习点滴
MySQL学习点滴 --分区表 概述: 分区功能并不是在存储引擎层完成的,因此很多存储引擎包括InnoDB, MyISAM, NDB等都支持分区功能.但也并不是所有的存储引擎都支持分区.在使用分区前, ...
- (转)UITextField
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- file结构体
struct file结构体定义在include/linux/fs.h中定义.文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file.它由内核在打开文件时 ...
- if-else优化
过多if-else分支的优化 超过3个就应该去优化,说if-else过多的分支可以使用switch或者责任链模式等等方式来优化.确实,这是一个小问题,不过我们还是可以整理一下这个小问题的重构方式. ...
- Head First HTML5 Programming笔记--chapter2 介绍Javascript和DOM
你已经了解了HTML标记(也称为结构),而且知道了CSS样式(也称为表示),剩下的就是Javascript(也称为行为). JavaScript的工作方式 1. 编写 你创建HTML标记和JavaSc ...