java itext 中文_iText中输出中文

/*

* $Id: RepeatingTable.java,v 1.5 2005/05/09 11:52:47 blowagie Exp $

* $Name: $

*

* This code is part of the 'iText Tutorial'.

* You can find the complete tutorial at the following address:

* http://itextdocs.lowagie.com/tutorial/

*

* This code is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

*

* itext-questions@lists.sourceforge.net

*/

package com.lowagie.examples.objects.tables.alternatives;

import java.awt.Color;

import java.io.File;

import java.io.FileOutputStream;

import java.util.Date;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.FontFactory;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Phrase;

import com.lowagie.text.Rectangle;

import com.lowagie.text.Table;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

/**

* Shows how a table is split if it doesn't fit the page.

*/

public class RepeatingTable {

/**

* Shows how a table is split if it doesn't fit the page.

*

* @param args

* no arguments needed

*/

public static void main(String[] args) {

System.out.println("table splitting");

// creation of the document with a certain size and certain margins

Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);

try {

// creation of the different writers

String filePath = "d:" + File.separator + "temp" + File.separator

+ "iText_Generated_pdf" + File.separator + "table"

+ File.separator;

File file = new File(filePath);

if (!file.exists()) {

file.mkdirs();

}

PdfWriter.getInstance(document, new FileOutputStream(filePath

+ "repeatingtable.pdf"));

// we add some meta information to the document

document.addAuthor("chenzwei@cn.ibm.com,CTE WAC,GBSC,CDL,IBM");

document.addSubject("This is a sample of iText in CTE.");

document.open();

Table datatable = new Table(10);

int headerwidths[] = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };

datatable.setWidths(headerwidths);

datatable.setWidth(100);

datatable.setPadding(3);

// the first cell spans 10 columns

Cell cell = new Cell(new Phrase(

"Administration -System Users Report", FontFactory.getFont(

FontFactory.HELVETICA, 24, Font.BOLD)));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setLeading(30);

cell.setColspan(10);

cell.setBorder(Rectangle.NO_BORDER);

cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));

datatable.addCell(cell);

// These cells span 2 rows

datatable.setBorderWidth(2);

datatable.setAlignment(1);

datatable.addCell("User Id");

datatable.addCell("Name\nAddress");

datatable.addCell("Company");

datatable.addCell("Department");

datatable.addCell("Admin");

datatable.addCell("Data");

datatable.addCell("Expl");

datatable.addCell("Prod");

datatable.addCell("Proj");

datatable.addCell("Online");

// this is the end of the table header

datatable.endHeaders();

datatable.setBorderWidth(1);

for (int i = 1; i < 30; i++) {

datatable.setAlignment(Element.ALIGN_LEFT);

datatable.addCell("myUserId");

datatable

.addCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");

datatable.addCell("No Name Company");

datatable.addCell("D" + i);

datatable.setAlignment(Element.ALIGN_CENTER);

datatable.addCell("No");

datatable.addCell("Yes");

datatable.addCell("No");

datatable.addCell("Yes");

datatable.addCell("No");

datatable.addCell("Yes");

}

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);

document.add(new Paragraph(" 产生的报告",FontChinese));

document.add(datatable);

document.newPage();

document.add(new Paragraph(

"com.lowagie.text.pdf.PdfPTable - Cells split\n\n"));

datatable.setConvert2pdfptable(true);

document.add(datatable);

document.newPage();

document.add(new Paragraph(

"com.lowagie.text.Table - Cells kept together"));

datatable.setConvert2pdfptable(false);

datatable.setCellsFitPage(true);

document.add(datatable);

document.newPage();

document

.add(new Paragraph(

"com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n"));

datatable.setConvert2pdfptable(true);

document.add(datatable);

} catch (Exception e) {

e.printStackTrace();

}

// we close the document

document.close();

}

}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论

打赏作者

我会飞不是吹

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值