写在pdf里,我试了不可以阿。谁帮我写写。
解决方案 »
- jsp 设置response.setHeader("WWW-Authenticate", "NTLM")页面submit,获得表单的对象为null
- 请教 如何将js的值无刷新传递给当前的jsp页面
- 如何让 request.getParameter() 正常地接收上一个网页传来的中文字符?
- 在hibernate里,什么情况下多个线程会访问同一个session?
- tomcat 启动关闭时出现问题应该如何解决?
- 就差最后一步了
- 谁有OFBIZ详细部署讲解
- 怎样隐藏提交给action域的字段内容
- 关于JDBC中连接Microsoft SQL Server2000的问题?
- jsp jdbc为什麽不能把数据insert到sql server 中,也不能从sql server中提取数据?!
- 求救,论坛带类似QQ秀那种虚拟形象的!!
- 请问JSP如何读取文件
/*
* 创建日期 2004-12-10
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package xiaopeipei;
/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class xiaopeipei {
public static void main(String[] args) throws MalformedURLException, IOException {
Pn_itext_pdf pdfCreate = new Pn_itext_pdf();
try {
pdfCreate.createPDF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
public void createPDF() throws DocumentException, BadElementException, MalformedURLException, IOException {
Document document = new Document(PageSize.A4);
//document.addTitle("Title");
//document.addHeader("header","Header");
PdfWriter.getInstance(document, new FileOutputStream("C:/Helloworld.PDF"));
document.open();
//add a word
document.add(new Paragraph("Hello World!您好!hehe!"));
//add a table
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
Image png = Image.getInstance("D:\\a.png");
document.add(png);
//BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
//Paragraph pragraph=new Paragraph("你好", FontChinese);
//document.add(pragraph);
document.close();
}
}
Pn_itext_pdf pdfCreate = new Pn_itext_pdf();
这句。改成xiaopeipei pdfCreate = new xiaopeipei();
font.set...就像html一样的。
或者itext组件中提供了这么一个接口:
HtmlParser以下是例子
(1)<html>代码
<?xml version="1.0" encoding="GBK"?>
<html>
<head>
<title>
this is test for Html to pdf
</title>
</head>
<body>
<table border="1" cellpadding="1" cellspacing="1" width="100%">
<tr>
<td width="20%">1</td>
<td width="20%">test2</td>
<td width="20%">3</td>
<td width="20%">test4</td>
<td width="20%">5</td>
<td width="20%">test6</td>
</tr>
<tr width="20%">
<td>zhejiangyidongyingye</td>
<td>49</td>
<td>40</td>
<td>37</td>
<td>26</td>
<td>152</td>
</tr>
<tr width="20%">
<td>zhejiangyidongshoufei</td>
<td>4</td>
<td>7</td>
<td>12</td>
<td>4</td>
<td>27</td>
</tr>
<tr width="20%">
<td>zhejiangyidongzhangwu</td>
<td>4</td>
<td>2</td>
<td>123</td>
<td>123</td>
<td>6</td>
</tr>
<tr width="10%">
<td>tuanduifuzhugongju</td>
<td>5</td>
<td>123</td>
<td>111</td>
<td>123</td>
<td>5</td>
</tr>
<tr width="10%">
<td>heji</td>
<td>435</td>
<td>457</td>
<td>460</td>
<td>425</td>
<td>1777</td>
</tr>
</table>
</body>
</html>
<script language="javascript">
var abc=0;
</script>(2)java代码
package red.web.data.doc;import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.html.HtmlParser;
import java.io.*;public class TstringListToPdf
{
public TstringListToPdf()
{
}
public static void main(String args[])
{
//BaseFont bfComic = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//Font font = new Font(bfComic, 12, Font.NORMAL);
Document document = new Document(PageSize.A4, 1, 1, 1, 1);
try
{
FileInputStream bbb= new FileInputStream("a.htm");
PdfWriter.getInstance(document,new FileOutputStream("C:/Documents and Settings/Administrator/桌面/outputstream/HtmlToPdf.pdf"));
HtmlParser.parse(document,bbb);
}
catch (Exception e)
{
}
}
}
不过这样做对中文的支持不是很友好。我现在也在考虑这个问题。