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

  1. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  2. itext poi 学习之旅 (1)创建pdf

    从零开始学习itext 创建pdf 1.用到流进行创建的pdf import java.io.File; import java.io.FileOutputStream; import com.ite ...

  3. .net快速创建PDF文档 by c#

    原文地址:http://www.cnblogs.com/Creator/archive/2010/03/13/1685020.html C#引用IText创建PDF文档 先引用IText    可以从 ...

  4. 使用Rotativa在ASP.NET Core MVC中创建PDF

    在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP.NET MVC,那么Rot​​ativa工具已经可用,我们可以使用 ...

  5. NET Core MVC中创建PDF

    使用Rotativa在ASP.NET Core MVC中创建PDF 在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP ...

  6. C# /VB.NET 创建PDF项目符号列表和多级编号列表

    使用项目符号和编号,可以让文档的层次结构更清晰.更有条理,也更容易突出重点.在编辑文档的过程中,我个人也比较偏爱项目标号来标注文章重点信息.在之前的文章中,介绍了如何在Word中来创建项目标号和编号列 ...

  7. python从TXT创建PDF文件——reportlab

    使用reportlab创建PDF文件电子书一般都是txt格式的,某些电子阅读器不能读取txt的文档,如DPT-RP1.因此本文从使用python实现txt到pdf的转换,并且支持生成目录,目录能够生成 ...

  8. 01.在Java中如何创建PDF文件

    1.简介 在这篇快速文章中,我们将重点介绍基于流行的iText和PdfBox库从头开始创建 PDF 文档. 2. Maven 依赖 <dependency> <groupId> ...

  9. HTML生成横向的PDF

    HTML生成PDF请参照:https://www.cnblogs.com/yunfeiyang-88/p/10984740.html 如要生成横向的PDF:在html模板的style标签里面加入@pa ...

随机推荐

  1. angstromctf -No libc for You

    0x00 syscall syscall函数原型为: int syscall(int number, ...) 其中number是系统调用号,number后面应顺序接上该系统调用的所有参数.大概意思是 ...

  2. POI转换word doc文件为(html,xml,txt)

    在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容转换为对应的Html文件,也可以把它转换为底层用来描述doc文档的xml文件,还可以把它转换为底层用来描述doc ...

  3. UEditor中多图上传的bug

    多图上传 预览:支持浏览器版本  IE8以上 在线管理:由于存在bug,显示不了 ueditor-1.1.1.jar解压后找到FileManager 1.修改com.baidu.ueditor.hun ...

  4. Bootstrap历练实例:点击激活的按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  5. 稳定性 耗时 gc 过长问题排查 和工具

    自己的另外一篇: http://www.cnblogs.com/fei33423/p/7805186.html 偶有耗时抖动? gc 也有长耗时? fullgc 也是? 有同学反馈 swap 可能导致 ...

  6. 二. python函数与模块

    第四章.内置函数与装饰器详解 1.内置函数补充1 注:红色圆圈:必会:  紫红色方框:熟练:   绿色:了解 callable() 判断函数是否可以被调用执行 def f1(): pass f1() ...

  7. 【线段树 细节题】bzoj1067: [SCOI2007]降雨量

    主要还是细节分析:线段树作为工具 Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小 ...

  8. 快照、克隆,xshell优化,Linux历史

    目录 一.虚拟拍照功能 二.虚拟机克隆功能 三.Xshell的优化 四.介绍Linux历史 一.虚拟拍照功能 1.拍摄快照 关机状态拍照 关机命令:shutdown -h now 或者 init 0 ...

  9. python index 自己实现

    l = [2,3,4,223,42,56,7,389,586,845,8,894,343,46,345,3556,23,233,45,25,78,456,785,576,344,6,34,563,] ...

  10. if else elif 用法和区别

    1.If语句:“如果条件为真,执行子句中的代码."始终包含以下部分: if关键字: 条件(即求值为True或False的表达式): 冒号: 在下一行开始,缩进的代码块(称为if子句) 例如: ...