本文是总结javaweb操作PDF的各个语句。 源码及说明如下: package web; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.awt.Color; import java.io.*; import org.pdfbox.pdfparser.PDFParser; import org.pdfbox.pdmodel.PDDocument; import org.pdfbox.util.PDFTextStripper; public class goods { public static void main(String agre[])throws Exception { /*******************************************在pdf中插入表格*************************************/ // BaseFont ch = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); //Font font = new Font(ch,12,Font.BOLD); /*String Tab[] = {"","1","2","3","4","5"}; String cont[][] = {{"A1","A2","A3","A4","A5","A6"},{"B1","B2","B3","B4","B5","B6"}, {"C1","C2","C3","C4","C5","C6"},{"D1","D2","D3","D4","D5","D6"}}; Document document = new Document(PageSize.A4); //创建document对象 PdfWriter.getInstance(document, new FileOutputStream("D://first.pdf")); //创建书写器 document.open(); //打开文档 String content = "This is pdf file"; //文档内容 Paragraph p = new Paragraph(content); //创建段落 p.setAlignment(Paragraph.ALIGN_CENTER); //居中对齐 document.add(p); PdfPTable table = new PdfPTable(6); //建立一个6列的空白表格对象 table.setSpacingBefore(30f); //设置表格上面的空白宽度 for(int i=0;i<Tab.length;i++) { p = new Paragraph(Tab[i]); PdfPCell cell = new PdfPCell(p); //建立一个单元格 cell.setHorizontalAlignment(Element.ALIGN_CENTER); //设置水平居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //设置垂直居中 table.addCell(cell); } for(int i=0;i<cont.length;i++) { for(int j=0;j<cont[i].length;j++) { PdfPCell cell = new PdfPCell(new Paragraph(cont[i][j])); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); } } document.add(table); document.close();*/ /*******************************************在pdf中插入图像*************************************/ /*Document document = new Document(); Image image = Image.getInstance("D://11.jpg"); image.scalePercent(40); image.setRotation(180); image.setAlignment(Image.ALIGN_CENTER); PdfWriter.getInstance(document, new FileOutputStream("D://first.pdf")); document.open(); document.add(image); document.close();*/ /*******************************************应用PDFBox组件解析PDF文档*************************************/ try { File f = new File("D://first.pdf"); FileInputStream in = new FileInputStream(f); PDFParser parser = new PDFParser(in); parser.parse(); PDDocument document = parser.getPDDocument(); PDFTextStripper str = new PDFTextStripper(); String ss = str.getText(document); System.out.println(ss); in.close(); System.out.println("success"); }catch(Exception e){} } }