pdf 中的java运行,java - 从pdf文件读取特定位置的itext在intellij中运行,并提供所需的输出,但是可执行jar抛出错误 - 堆栈内存溢出...
我正在從n個頁面的輸入pdf文件中讀取特定位置,并在這些位置上列出文本。 然后,我編寫一個新的pdf文檔,并將列表中的這些字符串寫入包含單元格的表中。 我提出了兩個主要問題。
我想在表中有三列,但是如果列表中的字符串不是3的倍數(即列數),那么它將留下多余的字符串,并且不會打印它們。 例如,如果我要打印4個字符串,則程序將在第一行的三個單元格中打印前三個字符串,但會保留一個字符串。 我編寫了一些代碼來檢查字符串的數量,并將其設為3的mod( % ),并在其中添加了帶點(。)的空白單元格,以提供多余的單元格來完成該行,從而不留任何字符串。 有更好的方法嗎?
當我運行主類時,該程序在intellij中運行,并為我生成輸出pdf文件。 但是,當我制作可執行jar并通過雙擊運行它時,它什么也沒有做。 為了再次檢查,我在intellij終端中運行了jar,發現它引發了以下錯誤:
現在,為什么在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。
總結
以上是生活随笔為你收集整理的pdf 中的java运行,java - 从pdf文件读取特定位置的itext在intellij中运行,并提供所需的输出,但是可执行jar抛出错误 - 堆栈内存溢出...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 图片透明,PHP怎么把一张图片透
- 下一篇: php 删除 r n,PHP去除换行符'