读取pdf

package com.huangliang.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.AcroFields.Item;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfSignatureAppearance;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfStream;
import com.lowagie.text.pdf.PdfWriter;

public class PdfUtil {

/***
* 给pdf数字签名
*
* @param source
* @param export
* @param keyStoryPath
*/
public static void signatureToPdf(String source, OutputStream output, String keyStorePath) {

PdfReader reader = null;
FileInputStream ketStoreStream = null;
try {
reader = new PdfReader(source);
ketStoreStream = new FileInputStream(keyStorePath);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(ketStoreStream, "1234567890".toCharArray());
String alias = keyStore.aliases().nextElement();
PrivateKey key = (PrivateKey) keyStore.getKey(alias, "1234567890".toCharArray());
Certificate[] chain = keyStore.getCertificateChain(alias);
PdfStamper signature = PdfStamper.createSignature(reader, output, '\0');
PdfSignatureAppearance signatureAppearance = signature.getSignatureAppearance();
signatureAppearance.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
signatureAppearance.setReason("setReason");
signatureAppearance.setLocation("setLocation");
signatureAppearance.setContact("setContact");
signatureAppearance.setAcro6Layers(true);
signatureAppearance.setVisibleSignature(new Rectangle(5, 0, 0, 5), 1, "signature");
signature.getWriter();
signature.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (ketStoreStream != null)
ketStoreStream.close();
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/***
* 获取PDF表单的内容
*/
public static Map<String, String> readPDFForm(String path) {
Map<String, String> map = null;
PdfReader reader = null;
try {
reader = new PdfReader(path);
map = new HashMap<String, String>();
HashMap<String, Object> fields = reader.getAcroFields().getFields();
Set<Entry<String, Object>> entrySet = fields.entrySet();
for (Entry<String, Object> entry : entrySet) {
String fieldRealName = entry.getKey();
if (fieldRealName.equals("signature"))// 证书字段除外
continue;
String key = fieldRealName.substring(fieldRealName.lastIndexOf(".") + 1,
fieldRealName.lastIndexOf("["));
String value = reader.getAcroFields().getField(fieldRealName);
map.put(key, value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.toString());
e.printStackTrace();
} finally {
if (reader != null)
reader.close();
}
return map;
};

/**
* 得到pdf的数字签名信息
*
* @return
*/
public static String getDigtalSignature(File file) {
PdfReader reader = null;
String signature = null;
InputStream input = null;
try {
input = new FileInputStream(file);
reader = new PdfReader(input);
AcroFields fields = reader.getAcroFields();
Item item = fields.getFieldItem("signature");
if (item != null) {
PdfDictionary widget = item.getWidget(0);
PdfDictionary ap = widget.getAsDict(PdfName.AP);
PdfStream normal = ap.getAsStream(PdfName.N);
PdfDictionary resources = normal.getAsDict(PdfName.RESOURCES);
PdfDictionary xobject = resources.getAsDict(PdfName.XOBJECT);
PdfStream frm = xobject.getAsStream(PdfName.FRM);
PdfDictionary res = frm.getAsDict(PdfName.RESOURCES);
PdfDictionary xobj = res.getAsDict(PdfName.XOBJECT);
PRStream n2 = (PRStream) xobj.getAsStream(PdfName.N2);
byte[] stream = PdfReader.getStreamBytes(n2);
signature = new String(stream);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
if (reader != null)
reader.close();
}
return signature;
};

/***
* 用map的内容生成一个pdf到一个输出流
*
* @param map
* @param output
*/
public static void createReturnPdf(Map<String, String> map, OutputStream output) {
Document document = new Document();
try {
PdfWriter.getInstance(document, output);
document.open();
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese, 12, Font.NORMAL);
Font boldChinese = new Font(baseFontChinese, 14, Font.BOLD);
Table table = new Table(6);
table.setPadding(3);
table.setBorder(3);

Cell cell_a1 = new Cell();
cell_a1.setColspan(6);
cell_a1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell_a1.setVerticalAlignment(Element.ALIGN_CENTER);
cell_a1.add(new Paragraph("项目办理表(存根)", boldChinese));
table.addCell(cell_a1);

Cell cell_b1 = new Cell();
cell_b1.setColspan(1);
cell_b1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell_b1.setVerticalAlignment(Element.ALIGN_CENTER);
cell_b1.add(new Paragraph("行政区域", fontChinese));
table.addCell(cell_b1);

Cell cell_b2 = new Cell();
cell_b2.setColspan(5);
cell_b2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell_b2.setVerticalAlignment(Element.ALIGN_CENTER);
cell_b2.add(setFont(map.get("行政区域"), fontChinese));
table.addCell(cell_b2);

document.add(table);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (document != null)
document.close();
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

private static Paragraph setFont(String str, Font fontChinese) {
Paragraph paragraph = null;
if (str != null) {
if (str.matches(".*[\\u4e00-\\u9faf].*"))
paragraph = new Paragraph(str, fontChinese);
else
paragraph = new Paragraph(str);
} else
paragraph = new Paragraph("");
return paragraph;
}

public static void main(String[] args) {
try {
FileOutputStream fileOutputStream = new FileOutputStream("e:/123.pdf");
// Map<String, String> map = new HashMap<>();
// map.put("行政区域", "行政区域value");
// createReturnPdf(map, fileOutputStream);
String keystorepath = "config/keystore.keystore";
signatureToPdf("e:/123.pdf", fileOutputStream, keystorepath);
fileOutputStream.close();
System.out.println("操作成功");
} catch (Exception e) {
e.printStackTrace();
}
// Map<String, String> map = readPDFForm(input);
// for (Map.Entry<String, String> string : map.entrySet()) {
// System.out.println(string.getKey()+"="+string.getValue());
// }
}

}

转载于:https://www.cnblogs.com/RecordMyFootstep/p/8116777.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值