java-pdf导入导出使用

2018-12-24 12:49

salyenli0我的:收件箱资源博客空间设置|帮助|退出

? 首页 ? 业界 ? 移动 ? 云计算 ? 研发 ? 论坛 ? 博客 ? 下载 ?

更多

首页论坛帮助论坛牛人论坛地图

CSDN>CSDN论坛>Java>Java EE

? 管理菜单 ? 结帖 ? 发帖 ?

回复

不显示删除回收藏

用Java生成PDF的方法 [问题点数:分]

楼主 发表于: 2011-10-21 11:00:35 如何用java代码生成pdf文件 求简单的方法,要求是免费的。

对我有用[0]丢个板砖[0]引用 | 举报 | 管理 | TOP ousyuryu 回复次数:8

大熊 等级: 结帖率:90%

60gavin_ts 痛丶并快乐着 等级: 结帖率:97.53%

ioe_gaoyong 风尘中国 等级: 结帖率:75%

#1得分:0 回复于: 2011-10-21 11:04:11 http://www.2cto.com/kf/201108/101989.html

希望对LZ有帮助!

微软大会2012立即注册购票享8折优惠! 对我有用[0]丢个板砖[0]引用 | 举报 | 管理 | TOP

#2得分:0 回复于: 2011-10-21 11:12:43 你下载一下iText,http://itextpdf.com/ 将iText的jar加入到环境变量,然后下面给你一个生成PDF的具体实例 Java code? 1 import java.awt.Color; 2 import java.io.FileOutputStream; 3 import com.itextpdf.text.*; 4 import com.itextpdf.text.pdf.*; 5 /**

6 * iText5的入门学习文档,这个demo已经提供了自7 己想要的最为常见的实现 8 *

9 * 这个文档主要学习利用itext实现创建PDF文档 10 * @author 11 http://www.coderanch.com/how-to/java/ItextExa12 mple 13 * 14 */ 15 public class Demo { 16 public static void main (String[] args) { 17 // creation of the document with a certain size 18 and certain margins 19 // may want to use PageSize.LETTER instead 20 Document document = new Document(PageSize.A4, 21 50, 50, 50, 50); 22 try { 23 // creation of the different writers 24 PdfWriter writer = 25 PdfWriter.getInstance(document, new 26 FileOutputStream(\27 writer.setBoxSize(\28 559, 788)); 29 HeaderFooter event = new HeaderFooter(); 30 writer.setPageEvent(event); 31 // various fonts 32 BaseFont bf_helv =

33 BaseFont.createFont(BaseFont.HELVETICA, 34 \35 BaseFont bf_times = 36 BaseFont.createFont(BaseFont.TIMES_ROMAN, 37 \38 BaseFont bf_courier = 39 BaseFont.createFont(BaseFont.COURIER, 40 \41 BaseFont bf_symbol = 42 BaseFont.createFont(BaseFont.SYMBOL, 43 \44 int y_line1 = 650; 45 int y_line2 = y_line1 - 50; 46 int y_line3 = y_line2 - 50; 47 document.open(); 48 PdfContentByte cb = writer.getDirectContent(); 49 cb.setLineWidth(0f); 50 cb.moveTo(250, y_line3 - 100); 51 cb.lineTo(250, y_line1 + 100); 52 cb.moveTo(50, y_line1); 53 cb.lineTo(400, y_line1); 54 cb.moveTo(50, y_line2); 55 cb.lineTo(400, y_line2); 56 cb.moveTo(50, y_line3); 57 cb.lineTo(400, y_line3); 58 cb.stroke(); 59 cb.beginText(); 60 cb.setFontAndSize(bf_helv, 12); 61 String text = \62 cb.showTextAligned(PdfContentByte.ALIGN_CENTE63 R, text + \64 cb.showTextAligned(PdfContentByte.ALIGN_RIGHT65 , text + \66 cb.showTextAligned(PdfContentByte.ALIGN_LEFT, 67 text + \68 cb.endText(); 69 document.newPage(); 70 // add text in two paragraphs from top to bottom 71 Paragraph par = new Paragraph(\72 paragraph\73 par.getFont().setStyle(Font.BOLD); 74 document.add(par); 75 par = new Paragraph(\76 par.getFont().setStyle(Font.ITALIC);

77 // par.getFont().setStyle(Font.BOLD); 78 document.add(par); 79 par = new Paragraph(\80 strike-through paragraph\81 par.getFont().setStyle(Font.UNDERLINE | 82 Font.STRIKETHRU); 83 document.add(par); 84 // demonstrate some table features 85 PdfPTable table = new PdfPTable(3); 86 table.setSpacingBefore(20); 87 table.getDefaultCell().setPadding(5); 88 PdfPCell cell = new PdfPCell(new 89 Phrase(\90 cell.setPadding(5); 91 cell.setColspan(3); 92 table.addCell(cell); 93 table.setHeaderRows(1); 94 cell = new PdfPCell(new Phrase(\95 with rowspan 2 and red border\96 cell.setPadding(5); 97 cell.setRowspan(2); 98 cell.setBorderColor(new BaseColor(new 99 Color(255, 0, 0))); 10table.addCell(cell); 0 table.addCell(\中文\10table.addCell(\1 table.addCell(\10table.addCell(\

2 cell = new PdfPCell(new Phrase(\10center\

3 cell.setPadding(5);

10cell.setHorizontalAlignment(Element.ALIGN_CEN4 TER);

10table.addCell(cell);

5 cell = new PdfPCell(new Phrase(\10cell\

6 cell.setPadding(5); 10cell.setRowspan(2); 7 cell.setColspan(2);

10// PdfPCell content can be rotated 8 cell.setRotation(90); 10table.addCell(cell);

9 cell = new PdfPCell(new Phrase(\right\11cell.setPadding(5);

0 cell.setHorizontalAlignment(Element.ALIGN_RIG11HT);

1 table.addCell(cell); 11document.add(table);

2 // add text at an absolute position 11cb.beginText();

3 cb.setFontAndSize(bf_times, 14); 11cb.setTextMatrix(100, 300);

4 cb.showText(\11cb.endText();

5 // rotated text at an absolute position

11PdfTemplate template = cb.createTemplate(300, 6 300);

11template.beginText();

7 template.setFontAndSize(bf_times, 14);

11template.showText(\8 400, 200.\

11template.endText(); 9 float rotate = 90; 12float x = 400; 0 float y = 200;

12float angle = (float) (-rotate * (Math.PI / 1 180));

12float xScale = (float) Math.cos(angle); 2 float yScale = (float) Math.cos(angle); 12float xRot = (float) -Math.sin(angle); 3 float yRot = (float) Math.sin(angle);

12cb.addTemplate(template, xScale, xRot, yRot, 4 yScale, x, y); 12document.close();

5 } catch (Exception ex) {

12System.err.println(ex.getMessage()); 6 } 12}

7 /** Inner class to add a header and a footer. */ 12static class HeaderFooter extends 8 PdfPageEventHelper {

12public void onEndPage (PdfWriter writer, 9 Document document) {

13Rectangle rect = writer.getBoxSize(\0 switch(writer.getPageNumber() % 2) { 13case 0:

1 ColumnText.showTextAligned(writer.getDirectCo13ntent(),


java-pdf导入导出使用.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:历史与社会中考复习教案 (复习导引)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: