pdf 中的java运行,java - 从pdf文件读取特定位置的itext在intellij中运行,并提供所需的输出,但是可执行jar抛出错误 - 堆栈内存溢出...

我正在从n个页面的输入pdf文件中读取特定位置,并在这些位置上列出文本。 然后,我编写一个新的pdf文档,并将列表中的这些字符串写入包含单元格的表中。 我提出了两个主要问题。

我想在表中有三列,但是如果列表中的字符串不是3的倍数(即列数),那么它将留下多余的字符串,并且不会打印它们。 例如,如果我要打印4个字符串,则程序将在第一行的三个单元格中打印前三个字符串,但会保留一个字符串。 我编写了一些代码来检查字符串的数量,并将其设为3的mod( % ),并在其中添加了带点(。)的空白单元格,以提供多余的单元格来完成该行,从而不留任何字符串。 有更好的方法吗?

当我运行主类时,该程序在intellij中运行,并为我生成输出pdf文件。 但是,当我制作可执行jar并通过双击运行它时,它什么也没有做。 为了再次检查,我在intellij终端中运行了jar,发现它引发了以下错误:

72ac2a2006a713876673973ec953c210.png

现在,为什么在intellij中运行它时也不会出现相同的问题? 我该如何克服这个问题? 我在Eclipse中重新编写了整个项目,而eclipse根本不编译它,并给出了intellij命令行中可执行文件运行的问题。

这是我在项目中拥有的三个班级:

package addressLabels;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.pdf.PdfReader;

import com.itextpdf.text.pdf.parser.FilteredTextRenderListener;

import com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy;

import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import com.itextpdf.text.pdf.parser.RegionTextRenderFilter;

import com.itextpdf.text.pdf.parser.RenderFilter;

import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Driver {

public static final String SRC = "C:/temp/ebay.pdf";

public static void main(String[] args) throws IOException, DocumentException {

ReadCertainLocationOnPageInPdf contentsObj = new ReadCertainLocationOnPageInPdf(SRC);

WritePdf writer = new WritePdf(contentsObj.getListOfAddresses());

//contentsObj.printListOfAddresses();

}

}//class Driver ends here.

package addressLabels;

import com.itextpdf.text.Rectangle;

import com.itextpdf.text.pdf.PdfReader;

import com.itextpdf.text.pdf.parser.*;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class ReadCertainLocationOnPageInPdf {

//private String cleanTextMarkedForTokenization;

private List listOfAddresses;

public ReadCertainLocationOnPageInPdf(String pdfFileAddress){

this.listOfAddresses = new ArrayList();

parsePdf(pdfFileAddress);

}//constructor ends here.

private void parsePdf(String pdfFileAddress) {

File f = new File(pdfFileAddress);

if (f.isFile() && f.canRead()){

try {

PdfReader reader = new PdfReader(pdfFileAddress);

int numPages = reader.getNumberOfPages();

//Get information about the page size

//Rectangle mediabox = reader.getPageSize(1);

//printDataAboutThisPage(mediabox);

//StringBuilder sb = new StringBuilder("");

for (int pageNum = 1; pageNum <= numPages; pageNum++){

String oneAddress = getTextFromThisPage(pageNum, reader);

this.addOneAddressToListOfAddresses(oneAddress);

//sb.append(getTextFromThisPage(pageNum, reader)).append("\n\n");

}

//this.addOneAddressToListOfAddresses(sb.toString());

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}//if ends here

//System.out.println(sb.toString());

}

private void printDataAboutThisPage(Rectangle mediabox) {

//Lower left corner is x

float x = mediabox.getRight();

float y = mediabox.getTop();

System.out.println("Lower left corner: " + x);

System.out.println("Upper right conrner: " + y);

System.out.println("The values of x increase from left to right; the values of y increase from bottom to top. \n The unit of the measurement system in PDF is called \"user unit\". \n By default one user unit coincides with one point (this can change, but you won't find many PDFs with a different UserUnit value).\n In normal circumstances, 72 user units = 1 inch.");

}

private String getTextFromThisPage(int pageNo, PdfReader reader) throws IOException {

//java.awt.geom.Rectangle2D rect = new java.awt.geom.Rectangle2D.Float(226, 547, 240, 158);

java.awt.geom.Rectangle2D rect = new java.awt.geom.Rectangle2D.Float(226, 547, 240, 158);

RenderFilter regionFilter = new RegionTextRenderFilter(rect);

TextExtractionStrategy strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), regionFilter);

String t = PdfTextExtractor.getTextFromPage(reader, pageNo, strategy);

t = this.cleanOneLabel(t);

return t;

}

private String cleanOneLabel(String t) {

StringBuilder sb2 = new StringBuilder("");

String[] lines = t.split(System.getProperty("line.separator"));

for(String s:lines) {

if(!s.equals(""))

sb2.append(s).append("\n");

}

String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";

String replacement = "";

return sb2.toString().replaceAll(pattern, replacement);// ??? s = s.replaceAll("\n+", "\n");

}

private String cleanOneLabel2(String t) {

StringBuilder sb2 = new StringBuilder("");

String[] lines = t.split(System.getProperty("line.separator"));

for(int i = 0; i < lines.length; i++) {

if(lines[i].contains("Post to:")) {

lines[i] = lines[i].replace("Post to:", "pakbay-Post to:");

}

}

for(String s:lines) {

if(!s.equals(""))

sb2.append(s).append("\n");

}

String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";

String replacement = "";

return sb2.toString().replaceAll(pattern, replacement);// ??? s = s.replaceAll("\n+", "\n");

}

public List getListOfAddresses(){

return this.listOfAddresses;

}

public void printListOfAddresses(){

for(int i = 0; i < listOfAddresses.size(); i++){

System.out.print(listOfAddresses.get(i));

}

}

public void addOneAddressToListOfAddresses(String oneAddress) {

//clean the string before adding it to the list of addresses.

//Remove extra spaces, tabs and blank lines from the passed string.

String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)";

String replacement = "";

oneAddress = oneAddress.replaceAll(pattern, replacement);

//Add the cleaned address to the list of addresses.

this.listOfAddresses.add(oneAddress);

}

}//class ReadCertainLocationOnPageInPdf ends here.

package addressLabels;

import java.io.FileOutputStream;

import java.util.Date;

import com.itextpdf.text.*;

import com.itextpdf.text.pdf.PdfPCell;

import com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfWriter;

public class WritePdf {

private static String FILE = "C:/temp/ebay-output.pdf";

private java.util.List listOfAddresses;

public WritePdf(java.util.List listOfAddresses) {

this.listOfAddresses = listOfAddresses;

System.out.println("Size: " + this.getListOfAddresses().size());

System.out.println("Element at zeroth position in list: " + this.getListOfAddresses().get(0));

System.out.println("Element at nth position in list: " + this.getListOfAddresses().get(this.getListOfAddresses().size()-1));

writeTheListOnPdf();

}

private void writeTheListOnPdf() {

try {

Document document = new Document();

PdfWriter.getInstance(document, new FileOutputStream(FILE));

document.open();

addMetaData(document);

//addTitlePage(document);

addContent(document);

document.close();

} catch (Exception e) {

e.printStackTrace();

}

}

private void addContent(Document document) throws DocumentException{

PdfPTable table = makeTable();

for (int i = 0; i < this.getListOfAddresses().size() ; i++) {

PdfPCell cell = makeCell();

cell.addElement(new Phrase(this.getListOfAddresses().get(i)));

table.addCell(cell);

}

/* we have three columns in the table. If the number of addresses is not exactly equal to the number of

* cells created then the pdf file is corrupt and the program throws error. So we have to add some extra cells

* to complete a row. */

calculateAndAddExtraCells(table);

document.add(table);

}

private void calculateAndAddExtraCells(PdfPTable table) {

int numOfAddresses = this.getListOfAddresses().size();

int numOfExtraCells = this.getListOfAddresses().size()%3;

int loopCounter = 0;

if (numOfExtraCells == 0)

loopCounter = 3;

else if (numOfExtraCells == 1)

loopCounter = 2;

else if (numOfExtraCells == 2)

loopCounter = 1;

for (int i = 1; i <= loopCounter ; i++) {

PdfPCell blankCell = this.makeCell();

blankCell.addElement(new Phrase("."));

table.addCell(blankCell);

}

}

private PdfPCell makeCell() {

PdfPCell cell = new PdfPCell();

cell.setPadding(4);

//cell.setNoWrap(true);

cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

cell.setVerticalAlignment(PdfPCell.ALIGN_CENTER);

cell.setBorder(Rectangle.NO_BORDER);

return cell;

}

private PdfPTable makeTable() {

PdfPTable table = new PdfPTable(3);

table.setWidthPercentage(100);

table.setSplitRows(false);

return table;

}

private void addMetaData(Document document) {

document.addTitle("Address labels for the input pdf file");

document.addSubject("Address labels");

document.addKeywords("ebay, amazon, addresses, labels");

document.addAuthor("Ajmal Khan");

document.addCreator("Ajmal Khan");

}

public java.util.List getListOfAddresses() {

return listOfAddresses;

}

public void setListOfAddresses(java.util.List listOfAddresses) {

this.listOfAddresses = listOfAddresses;

}

}//writePdf ends here.

这是pom.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.swedishnow

ebayAddresses

1.0-SNAPSHOT

org.apache.maven.plugins

maven-jar-plugin

true

addressLabels.Driver

com.itextpdf

kernel

7.0.0

com.itextpdf

layout

7.0.0

org.slf4j

slf4j-log4j12

1.7.18

com.itextpdf

itext-xtra

5.5.4

com.itextpdf

itextpdf

5.5.9

我使用此视频中推荐的方法在intellij Community 2018.1.5 Edition中创建可执行jar。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值