`
ssydxa219
  • 浏览: 608737 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

itext itextAsian

 
阅读更多

.准备工作

       首先需要加载 itext-1.1.4.jariTextAsian.jar两个jar,其中iTextAsian.jar用于中日韩文字的显示问题.

.主要类   

·  com.lowagie.text.Paragraph —— 这个类表示一个缩进的段落。

·  com.lowagie.text.Chapter —— 这个类表示 PDF 文档中的章节。使用Paragraph作为题目并使用int作为章节号码来创建它。

·  com.lowagie.text.Font —— 这个类包含了全部的字体规范,例如字体、大小、样式和颜色。各种字体都在这个类中声明为静态常数。

·  com.lowagie.text.List —— 这个类表示一个列表,按顺序包含许多ListItems

·  com.lowagie.text.Table —— 这个类表示包含单元格的表,单元格有序地排列在矩阵中。

·  SIMSUN.TTC:宋体和新宋体

SIMKAI.TTF:楷体

SIMHEI.TTF:黑体

SIMFANG.TTF:仿宋体

.步骤

     一个PDF文件的输出,总共只需要5个步骤
      
a.创建一个Document实例
       Document document = new Document(PageSize.A4, 50, 50, 50, 50);

      第一个参数是页面大小A4大小,默认为竖置,改为横置PageSize.A4.rotate();。接下来的参数分别是左、右、上和下页边距

     b.Document实例和文件输出流用PdfWriter类绑定在一起
     PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("HelloWorld.pdf"));

      其他写入器为 HtmlWriterRtfWriterXmlWriter 等等,不同的类对应不同的文件格式.
     
c.打开文档
    document.open();


    
d.在文档中添加文字, 图片、表格、标题等.

   1.添加章

   chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 18,
     com.lowagie.text.Font.NORMAL, Color.black); 
// 设置字体大小、颜色

    Paragraph cTitle = new Paragraph("第一章", chapterFont);
    
chapter1 = new Chapter(cTitle, 1);      

    document.add(chapter1); //添加新章

  2.添加节

   Paragraph sTitle = new Paragraph("第一章第一节", , sectionFont);
   Section section =
chapter1.addSection(sTitle, 2); 

   3.添加表格

     table = new Table(n);  //n表示有多少列

    Table table = new Table(8);
     float[] widths = { 0.10f, 0.15f, 0.21f, 0.22f, 0.08f, 0.08f, 0.10f,
                    0.06f };
    table.setWidths(widths); 
//假设有8,并设置每列的列宽

  table中添加cell内容,

    Cell cell = new Cell(new Phrase("内容", tableHeaderFont));// 或者   Cell cell = new Cell("内容");
    cell.setBackgroundColor(Color.gray);  //设置背景色
    cell.setHorizontalAlignment(Element.ALIGN_CENTER); //设置为居中,默认为左对齐
    table.addCell(cell);

section.add(table); //把表加入节中

4.添加图片

Section subsection = section21.addSection(subTitle, 3); //节下再加新的节

subsection.add("图表"); //给图表加个小标题

   通过URL得到图片实例:

Image wmf = Image.getInstance(new URL("../examples/harbour.wmf"));

Image gif = Image.getInstance(new URL("../examples/vonnegut.gif"));

Image jpeg = Image.getInstance(new URL("../examples/myKids.jpg"));

Image png = Image.getInstance(new URL("../examples/hitchcock.png"));

通过文件名得到图片实例:

Image gif = Image.getInstance("vonnegut.gif");

Image jpeg = Image.getInstance("myKids.jpg");

Image png = Image.getInstance("hitchcock.png"); // 在本地的完整路径也可以

 subsection.add(image);
  Paragraph endPgh = new Paragraph("\n");  
//换行的作用
  endPgh.setAlignment(com.lowagie.text.Image.MIDDLE); //设置图表位置
  subsection.add(endPgh);

5.一般的文字
 document.add(new Paragraph("Hello World"));

6.添加页码

 HeaderFooter footer = new HeaderFooter(new Phrase("页码:",getChineseFont()), true);
             footer.setBorder(Rectangle.NO_BORDER);
             document.setFooter(footer);
             document.open();

 
   e.关闭文档
  document.close();

 

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
 
public class ItextDemo {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Document doc=new Document();
try {
PdfWriter writer=PdfWriter.getInstance(doc,new FileOutputStream("ItextDemo.pdf"));
doc.open();
BaseFont bf=BaseFont.createFont( "STSong-Light",   "UniGB-UCS2-H",   BaseFont.NOT_EMBEDDED);
Font font=new Font(bf,12,Font.NORMAL);
doc.add(new Paragraph("中文测试。。 Itext! ",font));
doc.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
}
 
 // step 1: creation of a document-object

        Document document = new Document();      

        try {

            // step 2:

            // we create a writer that listens to the document

            // and directs a PDF-stream to a file

            PdfWriter.getInstance(document, new FileOutputStream("D:\\ChinesePDF005_"+new

java.util.Date().getTime()+".pdf"));

          

            // step 3: we open the document

            document.open();

          

           //  step 4: we add content to the document

           // 楷体字

            BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\simkai.ttf",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

           // 方正舒体

            BaseFont bfComic2 = BaseFont.createFont("c:\\windows\\fonts\\FZSTK.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

           // 方正姚体

            BaseFont bfComic3 = BaseFont.createFont("c:\\windows\\fonts\\FZYTK.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//
         //   仿宋体

            BaseFont bfComic4 = BaseFont.createFont("c:\\windows\\fonts\\SIMFANG.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

          //  黑体

            BaseFont bfComic5 = BaseFont.createFont("c:\\windows\\fonts\\SIMHEI.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

           // 华文彩云

            BaseFont bfComic6 = BaseFont.createFont("c:\\windows\\fonts\\STCAIYUN.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //华文仿宋

            BaseFont bfComic7 = BaseFont.createFont("c:\\windows\\fonts\\STFANGSO.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

           // 华文细黑

            BaseFont bfComic8 = BaseFont.createFont("c:\\windows\\fonts\\STXIHEI.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

          //  华文新魏

            BaseFont bfComic9= BaseFont.createFont("c:\\windows\\fonts\\STXINWEI.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //华文行楷

            BaseFont bfComic0 = BaseFont.createFont("c:\\windows\\fonts\\STXINGKA.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

           // 华文中宋

            BaseFont bfComic99 = BaseFont.createFont("c:\\windows\\fonts\\STZHONGS.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //隶书

            BaseFont bfComic11= BaseFont.createFont("c:\\windows\\fonts\\SIMLI.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //宋体&新宋体    (这种字体的输出不了.有问题)

         //   BaseFont bfComic12 = BaseFont.createFont("c:\\windows\\fonts\\SIMSUN.TTC", null,

BaseFont.NOT_EMBEDDED, BaseFont.NOT_EMBEDDED, null, null);

            //宋体-方正超大字符集

            BaseFont bfComic13 = BaseFont.createFont("c:\\windows\\fonts\\SURSONG.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            //幼圆

            BaseFont bfComic14 = BaseFont.createFont("c:\\windows\\fonts\\SIMYOU.TTF",

BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            Font font = new Font(bfComic0, 14);

            String text1 = "啊发生的发球特工是大哥是法国时的风格是This is the quite popular True Type

font (繁體字測試VS简体字测试) ==>"+new java.util.Date();

            document.add(new Paragraph(text1, font));

        }

        catch(DocumentException de) {

            System.err.println(de.getMessage());

        }

        catch(IOException ioe) {

            System.err.println(ioe.getMessage());

        }      

        // step 5: we close the document

        document.close();

        System.out.println(">>> Export : "+"D:\\ChinesePDF005__.pdf");

  • src.rar (130.8 KB)
  • 下载次数: 51
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics