javaweb操作PDF的总结

本文是总结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){}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值