java用什么工具生成pdf 怎么用java代码生成pdf文档
大家好,今天给各位分享java用什么工具生成pdf的一些知识,其中也会对怎么用java代码生成pdf文档进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!
java怎么输出pdf格式的文件
java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf
的文档,而且可以将XML、Html文件转化为PDF文件。
iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用
iText类库了。
代码如下:
public class createPdf{
//自己做的一个简单例子,中间有图片之类的
//先建立Document对象:相对应的这个版本的jar引入的是com.lowagie.text.Document
Document document= new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);
public void getPDFdemo() throws DocumentException, IOException{
//这个导出用的是 iTextAsian.jar和iText-2.1.3.jar属于比较老的方法。具体下在地址见:
//首先
//字体的定义:这里用的是自带的jar里面的字体
BaseFont bfChinese= BaseFont.createFont("STSong-Light","UniGB-UCS2-H", false);
//当然你也可以用你电脑里面带的字体库
//BaseFont bfChinese= BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//定义字体注意在最新的包里面颜色是封装的
Font fontChinese8= new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));
//生成pdf的第一个步骤:
//保存本地指定路径
saveLocal();
document.open();
ByteArrayOutputStream ba= new ByteArrayOutputStream();
// PdfWriter writer= PdfWriter.getInstance(document, ba);
document.open();
//获取此编译的文件路径
String path= this.getClass().getClassLoader().getResource("").getPath();
//获取根路径
String filePath= path.substring(1, path.length()-15);
//获取图片路径找到你需要往pdf上生成的图片
//这里根据自己的获取的路径写只要找到图片位置就可以
String picPath= filePath+"\\WebContent"+"\\images\\";
//往PDF中添加段落
Paragraph pHeader= new Paragraph();
pHeader.add(new Paragraph("你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));
//pHeader.add(new Paragraph("文字",字体可以自己写也可以用fontChinese8之前定义好的);
document.add(pHeader);//在文档中加入你写的内容
//获取图片
Image img2= Image.getInstance(picPath+"ccf-stamp-new.png");
//定义图片在文档中显示的绝对位置
img2.scaleAbsolute(137.0F, 140.0F);
img2.setAbsolutePosition(330.0F, 37.0F);
//将图片添加到文档中
document.add(img2);
//关闭文档
document.close();
/*//设置文档保存的文件名
response.setHeader("Content-
disposition","attachment;filename=\""+ new String(("CCF会员资格确认
函.pdf").getBytes("GBK"),"ISO-8859-1")+"\"");
//设置类型
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out= response.getOutputStream();
ba.writeTo(out);
out.flush();*/
}
public static void main(String[]args) throws DocumentException, IOException{
createPdf pdf= new createPdf();
pdf.getPDFdemo();
}
//指定一个文件进行保存这里吧文件保存到D盘的text.pdf
public void saveLocal() throws IOException, DocumentException{
//直接生成PDF制定生成到D盘test.pdf
File file= new File("D:\\text2.pdf");
file.createNewFile();
PdfWriter.getInstance(document, new FileOutputStream(file));
}
}
java中怎么利用poi和itext生成pdf文档
生成PDF文档代码如下:
packagepoi.itext;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.awt.Color;
importcom.lowagie.text.*;
importcom.lowagie.text.pdf.*;
importcom.lowagie.text.pdf.BaseFont;
/**
*创建Pdf文档
*@authorAdministrator
*
*/
publicclassHelloPdf
{
publicstaticvoidmain(String[]args)throwsException
{
BaseFontbfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
FontFontChinese=newFont(bfChinese,12,Font.NORMAL);
//第一步,创建document对象
RectanglerectPageSize=newRectangle(PageSize.A4);
//下面代码设置页面横置
//rectPageSize=rectPageSize.rotate();
//创建document对象并指定边距
Documentdoc=newDocument(rectPageSize,50,50,50,50);
Documentdocument=newDocument();
try
{
//第二步,将Document实例和文件输出流用PdfWriter类绑定在一起
//从而完成向Document写,即写入PDF文档
PdfWriter.getInstance(document,newFileOutputStream("src/poi/itext/HelloWorld.pdf"));
//第3步,打开文档
document.open();
//第3步,向文档添加文字.文档由段组成
document.add(newParagraph("HelloWorld"));
Paragraphpar=newParagraph("世界你好",FontChinese);
document.add(par);
PdfPTabletable=newPdfPTable(3);
for(inti=0;i<12;i++)
{
if(i==0)
{
PdfPCellcell=newPdfPCell();
cell.setColspan(3);
cell.setBackgroundColor(newColor(180,180,180));
cell.addElement(newParagraph("表格头",FontChinese));
table.addCell(cell);
}
else
{
PdfPCellcell=newPdfPCell();
cell.addElement(newParagraph("表格内容",FontChinese));
table.addCell(cell);
}
}
document.add(table);
}
catch(DocumentExceptionde)
{
System.err.println(de.getMessage());
}
catch(IOExceptionioe)
{
System.err.println(ioe.getMessage());
}
//关闭document
document.close();
System.out.println("生成HelloPdf成功!");
}
}
希望对你有帮助。
怎么用java代码生成pdf文档
package com.qhdstar.java.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
*TODO【JAVA生成PDF】
*<p>
*
*@title GeneratePDF
*@author SYJ
*@email songyanjun_stars@126.com
*@date 2013-4-6
*@version V1.0
*/
public class GeneratePDF{
public static void main(String[] args){
//调用第一个方法,向C盘生成一个名字为ITextTest.pdf的文件
try{
writeSimplePdf();
}
catch(Exception e){ e.printStackTrace();}
//调用第二个方法,向C盘名字为ITextTest.pdf的文件,添加章节。
try{
writeCharpter();
}
catch(Exception e){ e.printStackTrace();}
}
public static void writeSimplePdf() throws Exception{
// 1.新建document对象
//第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document= new Document(PageSize.A4, 50, 50, 50, 50);
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
//创建 PdfWriter对象第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer= PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
// 3.打开文档
document.open();
// 4.向文档中添加内容
//通过 com.lowagie.text.Paragraph来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.add(new Paragraph("First page of the document."));
document.add(new
Paragraph("Some more text on the first page with different color and
font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new
Color(255, 150, 200))));
// 5.关闭文档
document.close();
}
/**
*添加含有章节的pdf文件
*
*@throws Exception
*/
public static void writeCharpter() throws Exception{
//新建document对象第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document= new Document(PageSize.A4, 20, 20, 20, 20);
//建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer= PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
//打开文件
document.open();
//标题
document.addTitle("Hello mingri example");
//作者
document.addAuthor("wolf");
//主题
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
//向文档中添加内容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new
Paragraph("Some more text on the first page with different color and
font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10,
Font.BOLD, new Color(0, 0, 0))));
Paragraph title1= new
Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18,
Font.BOLDITALIC, new Color(0, 0, 255)));
//新建章节
Chapter chapter1= new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph
title11= new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255,
0, 0)));
Section section1= chapter1.addSection(title11);
Paragraph someSectionText= new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText= new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
//关闭文档
document.close();
}
}
关于本次java用什么工具生成pdf和怎么用java代码生成pdf文档的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。