生成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成功!");
}
}
希望对你有帮助。
② Apache POI 实现word转pdf会有线程方面的问题吗有要,如何解决。
Iterator<HSSFRow> iterator = sheet.iterator();
POI中没有Row类,一般都是以HSSF开始的类。
③ 如何使用POI转换.DOC / DOCX为PDF在Java
如果只是文字的话,直接读出来,然后用07的存回去,poi能实现。但是有目录什么的就麻烦了,03的读出来目录是一行代码,就容易出问题
我用的poi正好在纠结,最后没办法,用按键精灵一个个转的。有个word文档批处理貌似能做到,但是要收费。
下面的是我读取word文字的代码
03读取:
InputStream is = new FileInputStream(docfile);
WordExtractor ex = new WordExtractor(is);
text = ex.getText();
System.out.println(text);
07读取:
OPCPackage opcPackage = POIXMLDocument.openPackage(docxfile.getAbsolutePath());
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
text = extractor.getText();
System.out.println(text);
最后:doc不一定是03的,也可能是07的
④ 怎样用poi jar包生成一个PDF文件
下载转换工具
网上大把的
⑤ poi可以写pdf吗
不可以,poi主要是操作Excel的!
⑥ java中poi如何将word文档转换成pdf
itext等等,可以方便转换的了
~
~
~
~