用Java生成PDF的方法

ousyuryu 2011-10-21 11:00:35
如何用java代码生成pdf文件
求简单的方法,要求是免费的。
...全文
1590 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
轻弹浅唱 2011-11-30
  • 打赏
  • 举报
回复
package test1;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class HelloWorld {

public static void main(String[] args) {
// 创建一个文档对象
Document doc = new Document();
try {
// 定义输出位置并把文档对象装入输出对象中
PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf"));
// 打开文档对象
doc.open();
// 加入文字“Hello World”
doc.add(new Paragraph("HelloWorld"));
// 关闭文档对象,释放资源
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
导入iTEXT的jar包就行了
lianjiaying 2011-10-21
  • 打赏
  • 举报
回复
顶一个!
nanjiwubingqq 2011-10-21
  • 打赏
  • 举报
回复
Itext插件
能生成word,PDF文件
风尘中国 2011-10-21
  • 打赏
  • 举报
回复
你下载一下iText,http://itextpdf.com/ 将iText的jar加入到环境变量,然后下面给你一个生成PDF的具体实例


import java.awt.Color;
import java.io.FileOutputStream;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

/**
* iText5的入门学习文档,这个demo已经提供了自己想要的最为常见的实现
*
* 这个文档主要学习利用itext实现创建PDF文档
* @author http://www.coderanch.com/how-to/java/ItextExample
*
*/

public class Demo {

public static void main (String[] args) {

// creation of the document with a certain size and certain margins
// may want to use PageSize.LETTER instead
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("etc/demo.pdf"));
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));

HeaderFooter event = new HeaderFooter();
writer.setPageEvent(event);

// various fonts
BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false);
BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);
BaseFont bf_symbol = BaseFont.createFont(BaseFont.SYMBOL, "Cp1252", false);

int y_line1 = 650;
int y_line2 = y_line1 - 50;
int y_line3 = y_line2 - 50;

document.open();

PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(0f);
cb.moveTo(250, y_line3 - 100);
cb.lineTo(250, y_line1 + 100);
cb.moveTo(50, y_line1);
cb.lineTo(400, y_line1);
cb.moveTo(50, y_line2);
cb.lineTo(400, y_line2);
cb.moveTo(50, y_line3);
cb.lineTo(400, y_line3);
cb.stroke();
cb.beginText();
cb.setFontAndSize(bf_helv, 12);
String text = "Sample text for alignment";
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, y_line1, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, y_line2, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, y_line3, 0);
cb.endText();

document.newPage();

// add text in two paragraphs from top to bottom
Paragraph par = new Paragraph("bold paragraph");
par.getFont().setStyle(Font.BOLD);
document.add(par);
par = new Paragraph("italic paragraph");
par.getFont().setStyle(Font.ITALIC);
// par.getFont().setStyle(Font.BOLD);

document.add(par);
par = new Paragraph("underlined and strike-through paragraph");
par.getFont().setStyle(Font.UNDERLINE | Font.STRIKETHRU);
document.add(par);

// demonstrate some table features
PdfPTable table = new PdfPTable(3);
table.setSpacingBefore(20);
table.getDefaultCell().setPadding(5);

PdfPCell cell = new PdfPCell(new Phrase("header"));
cell.setPadding(5);
cell.setColspan(3);
table.addCell(cell);
table.setHeaderRows(1);
cell = new PdfPCell(new Phrase("example cell with rowspan 2 and red border"));
cell.setPadding(5);
cell.setRowspan(2);
cell.setBorderColor(new BaseColor(new Color(255, 0, 0)));
table.addCell(cell);
table.addCell("中文");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
cell = new PdfPCell(new Phrase("align center"));
cell.setPadding(5);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("rotated cell"));
cell.setPadding(5);
cell.setRowspan(2);
cell.setColspan(2);
// PdfPCell content can be rotated
cell.setRotation(90);
table.addCell(cell);
cell = new PdfPCell(new Phrase("align right"));
cell.setPadding(5);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
document.add(table);

// add text at an absolute position
cb.beginText();
cb.setFontAndSize(bf_times, 14);
cb.setTextMatrix(100, 300);
cb.showText("Text at position 100, 300.");
cb.endText();

// rotated text at an absolute position
PdfTemplate template = cb.createTemplate(300, 300);
template.beginText();
template.setFontAndSize(bf_times, 14);
template.showText("Rotated text at position 400, 200.");
template.endText();

float rotate = 90;
float x = 400;
float y = 200;
float angle = (float) (-rotate * (Math.PI / 180));
float xScale = (float) Math.cos(angle);
float yScale = (float) Math.cos(angle);
float xRot = (float) -Math.sin(angle);
float yRot = (float) Math.sin(angle);

cb.addTemplate(template, xScale, xRot, yRot, yScale, x, y);

document.close();

} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}

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

public void onEndPage (PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
switch(writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, new Phrase("even header"),
rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase("odd header"),
rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", writer.getPageNumber())),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
}
}
我嘞个去 2011-10-21
  • 打赏
  • 举报
回复
leixing521 2011-10-21
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/jlhjlh521/3704668
挺好用的 支持office,txt 转换pdf
dxqrr 2011-10-21
  • 打赏
  • 举报
回复
itext
fskjb01 2011-10-21
  • 打赏
  • 举报
回复
ireport啊,pdf,excel输出都OK!

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧