PDF阅读器不工作

时间:2018-01-07 20:42:00

标签: pdf reader

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.nio.file.Files;

    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
    import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
    import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
`    
public class Test{


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

        String  pdf= "c:\\sample.pdf";

         PdfReader reader = new PdfReader(pdf);

    }


}
它不起作用 喜欢它我应该运行Windows 需要帮助请帮助我尝试了很多东西,但仍然收到相同的消息

这是错误消息

这是我在尝试你的代码时获得的输出

文件存在:true

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/bouncycastle/asn1/ASN1Encodable
at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:148)
at com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj(PdfReader.java:1024)
at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1430)
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:732)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:181)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:219)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)
at pdfconverter.Test.main(Test.java:37)
Caused by: java.lang.ClassNotFoundException: 
org.bouncycastle.asn1.ASN1Encodable
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more

2 个答案:

答案 0 :(得分:1)

代码对我来说很好看。您可能有关于错误的更多信息?例如,它可能是类路径错误 - 类似于未找到的itextpdf类...

如果它有助于作为基线 - 以下代码适用于我。我删除了无关的内容,但留下它们不会有什么坏处。

package entities;
// Generated 4-gen-2018 15.43.44 by Hibernate Tools 4.3.1

import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="MAGAZZINO"
,schema="ALBI"
)
public class Magazzino implements java.io.Serializable{

private final SimpleIntegerProperty itemId;
private final SimpleStringProperty itemNome;
private final SimpleStringProperty itemCodice;
private final SimpleStringProperty itemBrand;
private final SimpleStringProperty itemDescrizione;
private final SimpleStringProperty itemTipo;
private final SimpleStringProperty itemSottotipo;
private final SimpleStringProperty itemFornitore1;
private final SimpleStringProperty itemFornitore2;
private final SimpleDoubleProperty itemPrezzo;
private final SimpleIntegerProperty itemQta;
private final SimpleStringProperty itemQtaSoglia;
private final SimpleStringProperty itemLocation;
private final SimpleDoubleProperty itemValoreTot;     

private Magazzino(int itemId, String itemNome, String itemCodice, String itemBrand, String itemDescrizione, String itemTipo, String itemSottotipo, String itemFornitore1, String itemFornitore2, Double itemPrezzo, Integer itemQta, String itemQtaSoglia, String itemLocation, Double itemValoreTot) {
   this.itemId = new SimpleIntegerProperty(itemId);
   this.itemNome = new SimpleStringProperty(itemNome);
   this.itemCodice = new SimpleStringProperty(itemCodice);
   this.itemBrand = new SimpleStringProperty(itemBrand);
   this.itemDescrizione = new SimpleStringProperty(itemDescrizione);
   this.itemTipo = new SimpleStringProperty(itemTipo);
   this.itemSottotipo = new SimpleStringProperty(itemSottotipo);
   this.itemFornitore1 = new SimpleStringProperty(itemFornitore1);
   this.itemFornitore2 = new SimpleStringProperty(itemFornitore2);
   this.itemPrezzo = new SimpleDoubleProperty(itemPrezzo);
   this.itemQta = new SimpleIntegerProperty(itemQta);
   this.itemQtaSoglia = new SimpleStringProperty(itemQtaSoglia);
   this.itemLocation = new SimpleStringProperty(itemLocation);
   this.itemValoreTot = new SimpleDoubleProperty(itemValoreTot);
   NumberBinding multiplication = Bindings.multiply(this.itemPrezzoProperty(), this.itemQtaProperty());
   this.itemValoreTotProperty().bind(multiplication);
}


@Id
@Column(name="ITEM_ID", unique=true, nullable=false)
public int getItemId() {
    return itemId.get();
}

public SimpleIntegerProperty itemIdProperty() {
    return itemId;
}

public void setItemId(int itemId) {
    this.itemId.set(itemId);
}

@Column(name="ITEM_NOME", nullable=false, length=100)
public String getItemNome() {
    return itemNome.get();
}

public SimpleStringProperty itemNomeProperty() {
    return itemNome;
}

public void setItemNome(String itemNome) {
    this.itemNome.set(itemNome);
}

@Column(name="ITEM_CODICE", length=100)
public String getItemCodice() {
    return itemCodice.get();
}

public SimpleStringProperty itemCodiceProperty() {
    return itemCodice;
}

public void setItemCodice(String itemCodice) {
    this.itemCodice.set(itemCodice);
}

@Column(name="ITEM_BRAND", length=100)
public String getItemBrand() {
    return itemBrand.get();
}

public SimpleStringProperty itemBrandProperty() {
    return itemBrand;
}

public void setItemBrand(String itemBrand) {
    this.itemBrand.set(itemBrand);
}

@Column(name="ITEM_DESCRIZIONE", length=100)    
public String getItemDescrizione() {
    return itemDescrizione.get();
}

public SimpleStringProperty itemDescrizioneProperty() {
    return itemDescrizione;
}

public void setItemDescrizione(String itemDescrizione) {
    this.itemDescrizione.set(itemDescrizione);
}  

@Column(name="ITEM_TIPO", length=100)    
public String getItemTipo() {
    return itemTipo.get();
}

public SimpleStringProperty itemTipoProperty() {
    return itemTipo;
}

public void setItemTipo(String itemTipo) {
    this.itemTipo.set(itemTipo);
} 

@Column(name="ITEM_SOTTOTIPO", length=100)
public String getItemSottotipo() {
    return itemSottotipo.get();
}

public SimpleStringProperty itemSottotipoProperty() {
    return itemSottotipo;
}

public void setItemSottotipo(String itemSottotipo) {
    this.itemSottotipo.set(itemSottotipo);
} 

@Column(name="ITEM_FORNITORE_1", length=100)
public String getItemFornitore1() {
    return itemFornitore1.get();
}

public SimpleStringProperty itemFornitore1Property() {
    return itemFornitore1;
}

public void setItemFornitore1(String itemFornitore1) {
    this.itemFornitore1.set(itemFornitore1);
}

@Column(name="ITEM_FORNITORE_2", length=100)
public String getItemFornitore2() {
    return itemFornitore2.get();
}

public SimpleStringProperty itemFornitore2Property() {
    return itemFornitore2;
}

public void setItemFornitore2(String itemFornitore2) {
    this.itemFornitore2.set(itemFornitore2);
}

@Column(name="ITEM_PREZZO")
public Double getItemPrezzo() {
    return itemPrezzo.get();
}

public SimpleDoubleProperty itemPrezzoProperty() {
    return itemPrezzo;
}

public void setItemPrezzo(Double itemPrezzo) {
    this.itemPrezzo.set(itemPrezzo);
} 

@Column(name="ITEM_QTA")
public Integer getItemQta() {
    return itemQta.get();
}

public SimpleIntegerProperty itemQtaProperty() {
    return itemQta;
}

public void setItemQta(Integer itemQta) {
    this.itemQta.set(itemQta);
} 

@Column(name="ITEM_QTA_SOGLIA")
public String getItemQtaSoglia() {
    return itemQtaSoglia.get();
}

public SimpleStringProperty itemQtaSogliaProperty() {
    return itemQtaSoglia;
}

public void setItemQtaSoglia(String itemQtaSoglia) {
    this.itemQtaSoglia.set(itemQtaSoglia);
} 

@Column(name="ITEM_LOCATION", length=100)
public String getItemLocation() {
    return itemLocation.get();
}

public SimpleStringProperty itemLocationProperty() {
    return itemLocation;
}

public void setitemLocation(String itemLocation) {
    this.itemLocation.set(itemLocation);
} 

@Column(name="ITEM_VALORE_TOT")
public Double getItemValoreTot() {
    return itemValoreTot.get();
}

public SimpleDoubleProperty itemValoreTotProperty() {
    return itemValoreTot;
}

public void setItemValoreTot(Double itemValoreTot) {
    this.itemValoreTot.set(itemValoreTot);
}
}

输出结果为:

import java.io.File;
import com.itextpdf.text.pdf.PdfReader;

public class Test {

    public static void main(String[] args) {

        String  pdf= "C:\\Java-Design-Patterns.pdf";

        try {
            System.out.println("File Exists: "+new File(pdf).exists());
            PdfReader reader = new PdfReader(pdf);
            int count = reader.getNumberOfPages();
            System.out.println("PDF has "+count+" pages.");
         } catch (Exception e) {
            System.out.println("Failed to open PDF ["+pdf+"]: "+e);
            e.printStackTrace();
         }

    }
}

我使用的itext jar是:itextpdf-5.5.12.jar(通过maven包含)。

我使用的pdf(谷歌提供:java设计模式pdf)在这里:http://enos.itcollege.ee/~jpoial/java/naited/Java-Design-Patterns.pdf 我还没看过,但第一页看起来不错;)

那就是说,itextpdf非常棒。

答案 1 :(得分:0)

java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable

清楚地表明了问题:Bouncy Castle遗失了! (或者至少缺少所需的版本。)

Bouncy Castle是iText用于加密,解密,签名和签名验证的库,here是他们的网络代表。

因此,将Bouncy Castle库添加到类路径中。但请注意,所需的BC版本取决于相关的iText版本。您为itext7版本提供的maven链接表示需要BC 1.49。