新增方法

This commit is contained in:
wanggeng888 2021-03-05 18:33:41 +08:00
parent 839555c33e
commit f6d48921d6

View File

@ -167,6 +167,47 @@ public class PDFUtil {
return true;
}
public static boolean addImageReplaceKeyword(String sourceFile, String keyword, String outFile, String imageFile) {
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try {
pdfReader = new PdfReader(sourceFile);
pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(outFile));
CharInfo lastCharInfo = readPdfAndGetFirstKeywordCharInfo(pdfReader, keyword);
if (lastCharInfo == null) {
LOG.debug("未在PDF正文中匹配到关键字");
return false;
}
// 合成PDF
Image image = Image.getInstance(imageFile);
image.scalePercent(50F);
// image.scaleAbsolute(200);
image.setAbsolutePosition((float) (lastCharInfo.getX() + 10), (float) (lastCharInfo.getY() - 25));
PdfContentByte underContent = pdfStamper.getUnderContent(1);
underContent.addImage(image);
pdfStamper.close();
pdfReader.close();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return false;
} finally {
if (pdfStamper != null) {
try {
pdfStamper.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (pdfReader != null) {
pdfReader.close();
}
}
return true;
}
/**
* 读取pdf并获取关键字最后一个字符信息
*
@ -174,6 +215,49 @@ public class PDFUtil {
* @throws Exception
*/
public static CharInfo readPdfAndGetLastKeywordCharInfo(PdfReader pdfReader, String keyword) throws Exception {
List<PageInfo> pageInfos = listPageInfo(pdfReader);
// 关键字最后一个字符
CharInfo lastCharInfo = null;
for (PageInfo pageInfo : pageInfos) {
int index = pageInfo.getContent().indexOf(keyword);
if (index < 0) {
continue;
}
lastCharInfo = pageInfo.getCharInfos().get(index + keyword.length() - 1);
}
return lastCharInfo;
}
/**
* 读取pdf并获取关键字第一个
*
* @param pdfReader
* @param keyword
* @return
* @throws Exception
*/
public static CharInfo readPdfAndGetFirstKeywordCharInfo(PdfReader pdfReader, String keyword) throws Exception {
List<PageInfo> pageInfos = listPageInfo(pdfReader);
// 关键字第一个字符
CharInfo firstCharInfo = null;
for (PageInfo pageInfo : pageInfos) {
int index = pageInfo.getContent().indexOf(keyword);
if (index < 0) {
continue;
}
firstCharInfo = pageInfo.getCharInfos().get(index);
}
return firstCharInfo;
}
/**
* pdf信息列表
*
* @param pdfReader
* @return
* @throws IOException
*/
private static List<PageInfo> listPageInfo(PdfReader pdfReader) throws IOException {
List<PageInfo> pageInfos = new ArrayList<>();
// 获取PDF有多少页
int pageCount = pdfReader.getNumberOfPages();
@ -193,17 +277,7 @@ public class PDFUtil {
pdfContentStreamProcessor.processContent(ContentByteUtils.getContentBytesForPage(pdfReader, pageNum), resourcesPdfDictionary);
pageInfos.add(new PageInfo(pdfRenderListener.getContent(), pdfRenderListener.getCharInfos()));
}
// 关键字最后一个字符
CharInfo lastCharInfo = null;
for (PageInfo pageInfo : pageInfos) {
int index = pageInfo.getContent().indexOf(keyword);
if (index < 0) {
continue;
}
lastCharInfo = pageInfo.getCharInfos().get(index + keyword.length() - 1);
}
return lastCharInfo;
return pageInfos;
}
public static class PdfRenderListener implements RenderListener {