实现PDF文档打印
2009-04-22 14:56
288 查看
package com.test ;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TestPDF {
public static void createHelloWorld() throws FileNotFoundException,
DocumentException {
// Document document = new Document();
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream("D://HelloWorld.pdf"));
document.open();
// define table
float[] widths = { 0.05f, 0.29f, 0.05f, 0.05f, 0.14f, 0.03f, 0.04f,
0.04f, 0.05f, 0.05f, 0.08f, 0.08f, 0.05f };
// new 一个13列的table
PdfPTable table = new PdfPTable(13);
// 设置table每一列的宽度,widths里写的是百分比,他们加和需要是1
table.setWidths(widths);
// 设置表格在页面上的宽度,设成100表示可以表格填满页面,但是要去掉页面margin
table.setWidthPercentage(100);
// 设置表格上端的空白距离,类似css中的margin-top:xxpx;这样在给表格加上标题后,标题就不会跟表格重叠在一起了。
table.setSpacingBefore(10f);
table.setSpacingAfter(10f);
for (int i = 0; i < 26; i++) {
table.addCell(i + "");
}
// document.add(new Paragraph("Hello World"));
// document.add(table);
document.add(new Paragraph("test table ", FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))));
// 由于设置了table.setSpacingBefore(3f);所以table跟标题不会重合。
document.add(table);
document.newPage();
document.add(new Paragraph("This is the second page!"));
List list = new List(true, 20);
list.add(new ListItem("First line"));
list.add(new ListItem("second line"));
document.add(list);
document.close();
}
public static void main(String[] args) throws FileNotFoundException,
DocumentException {
createHelloWorld();
}
}
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class TestPDF {
public static void createHelloWorld() throws FileNotFoundException,
DocumentException {
// Document document = new Document();
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream("D://HelloWorld.pdf"));
document.open();
// define table
float[] widths = { 0.05f, 0.29f, 0.05f, 0.05f, 0.14f, 0.03f, 0.04f,
0.04f, 0.05f, 0.05f, 0.08f, 0.08f, 0.05f };
// new 一个13列的table
PdfPTable table = new PdfPTable(13);
// 设置table每一列的宽度,widths里写的是百分比,他们加和需要是1
table.setWidths(widths);
// 设置表格在页面上的宽度,设成100表示可以表格填满页面,但是要去掉页面margin
table.setWidthPercentage(100);
// 设置表格上端的空白距离,类似css中的margin-top:xxpx;这样在给表格加上标题后,标题就不会跟表格重叠在一起了。
table.setSpacingBefore(10f);
table.setSpacingAfter(10f);
for (int i = 0; i < 26; i++) {
table.addCell(i + "");
}
// document.add(new Paragraph("Hello World"));
// document.add(table);
document.add(new Paragraph("test table ", FontFactory.getFont(
FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))));
// 由于设置了table.setSpacingBefore(3f);所以table跟标题不会重合。
document.add(table);
document.newPage();
document.add(new Paragraph("This is the second page!"));
List list = new List(true, 20);
list.add(new ListItem("First line"));
list.add(new ListItem("second line"));
document.add(list);
document.close();
}
public static void main(String[] args) throws FileNotFoundException,
DocumentException {
createHelloWorld();
}
}
相关文章推荐
- Android连接蓝牙打印机实现PDF文档的打印
- Python实现将DOC文档转换为PDF的方法
- c#用账号密码访问公共盘,pdf打印文档
- PHP实现word文档转html以及pdf
- iReport+JasperReport实现数据库记录导出到html、excel、pdf文档
- 利用iStylePDF的API实现在PDF文档中动态插入一幅图片
- [置顶] 使用PageOffice实现文档(word,excel,pdf)在线预览编辑
- Android实现PDF预览打印
- Java实现HTML代码生成PDF文档
- 怎样用C#实现完整文档打印功能
- jQuery上传文件到服务器,实现量上传及压缩包导入,支持Office文档、PDF、图像、音视频和图纸等各类型文件。上传完成后系统自动为图片、音、视频类文件增加摘要及缩略图
- jsp实现针对excel及word文档的打印方法
- 利用Office2003实现PDF文件转Word文档
- C#实现office文档转换为PDF或xps
- 水晶报表使用经验谈1--建立水晶报表第一步及编译最易出现错误的解决方法及报表转换成pdf文档进行打印方法
- 用C#实现生成PDF文档
- C#打印程序实现完整文档打印功能
- 实现单文档ListView类型的打印、预览功能
- javaWeb文档预览之PDF.js实现PDF文件跨域预览(附测试实例)
- jdcob实现word转pdf时不能由ps文档转为pdf文档的问题