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();  
        }  

时间: 2024-08-02 17:10:08

itext A4纸张横向创建PDF的相关文章

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

从零开始学习itext 创建pdf 1.用到流进行创建的pdf import java.io.File; import java.io.FileOutputStream; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; public class PdfTest { public static void main(String[] args) throws Exception { /** * pdf 的准备工作

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

原文地址:http://www.cnblogs.com/Creator/archive/2010/03/13/1685020.html C#引用IText创建PDF文档 先引用IText    可以从这里进行下载组件 下面演示五步创建PDF 第一步:创建文件对象的实例 Document myDocument= new Document(PageSize.A4.Rotate()); 第二步:创建一个Writer监听文件并且向文件写入想要的流 PdfWriter.GetInstance(myDocu

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

使用项目符号和编号,可以让文档的层次结构更清晰.更有条理,也更容易突出重点.在编辑文档的过程中,我个人也比较偏爱项目标号来标注文章重点信息.在之前的文章中,介绍了如何在Word中来创建项目标号和编号列表,在本篇文章中,将介绍创建PDF项目符号列表和多级编号列表的方法. 借助工具:Spire.PDF.dll PS:dll可以直接在安装路径下的Bin文件夹中获取. 1.创建PDF符号列表 C# using System; using System.Drawing; using Spire.Pdf;

创建pdf

//创建pdf图形上下文 NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/pdf.pdf"]; NSLog(@"%@",path); UIGraphicsBeginPDFContextToFile(path, CGRectZero, nil); // CGContextRef context = UIGraphicsGetCurrentContext(); //创建pd

C#中如何创建PDF网格并插入图片

这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中. 网上有一些类似的解决方法,在这里我选择了一个免费版的PDF组件.安装控件后,创建新项目,添加安装目录下的dll文件作为项目的引用以及命名空间,如下: using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Grid; 接下来是详细步骤及代码片段: 步骤1: 首先创建一个PDF文档,并添加一个新页面. PdfDocument doc = new

怎么用OCR图文识别软件在MS Office中创建PDF文件

ABBYY PDF Transformer+是一款可创建.编辑及将PDF文件转换为其他可编辑格式的OCR图文识别软件,不仅可以从纸质文档.图像文件和任何其他流行格式创建PDF文件(相关文章请参考如何从文件创建ABBYY PDF Transformer+ PDF文档),还可以从MS Office中创建PDF文件,本文为大家详解如何使用ABBYY PDF Transformer+从MS Office中轻松创建PDF文件. 本文仅以Microsoft Word为例,MS的其他应用程序操作步骤类似. 步

使用iText生成带底图的PDF

package com.wzh.itext; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf

创建PDF模板,java添加内容、导出下载PDF

本文主要内容是:用java在pdf模板中加入数据,图片. 废话不多说,举个非常简单的例子: 首先创建word文档,导出PDF. 用 软件adobe acrobat打开,操作步骤如图: 在指定位置添加文本域, 保存退出.pdf模板创建完成,我们保存到 E:盘,起名叫 练习. 接下来是java内容. 在pom.xml文件加入, <!-- itext 图片转pdf --> <dependency> <groupId>com.itextpdf</groupId> &

C#创建PDF文档

说明:本实例用到了第三方组件ICSharpCode.SharpZipLib.dll.itextsharp.dll,该组件可到网上下载. 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.F