带有PDFBox的Lucene for Java获取空指针异常

带有PDFBox的Lucene for Java获取空指针异常,java,jar,lucene,pdfbox,Java,Jar,Lucene,Pdfbox,我对PDFBoxAPI感到失望 我已经做了: PDDocument pdfDocument = PDDocument.load(new File("text.pdf")); PDFTextStripper stripper = new PDFTextStripper(); String s = stripper.getText(pdfDocument); pdfDocument.close(); 但是我有一个 Exception in thread "main" java.lang.Null

我对PDFBoxAPI感到失望

我已经做了:

PDDocument pdfDocument = PDDocument.load(new File("text.pdf"));
PDFTextStripper stripper = new PDFTextStripper();
String s =  stripper.getText(pdfDocument);
pdfDocument.close();
但是我有一个

Exception in thread "main" java.lang.NullPointerException
at org.pdfbox.pdmodel.PDPageNode.getAllKids(PDPageNode.java:194)
at org.pdfbox.pdmodel.PDPageNode.getAllKids(PDPageNode.java:182)
at org.pdfbox.pdmodel.PDDocumentCatalog.getAllPages(PDDocumentCatalog.java:226)
at org.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:216)
at org.pdfbox.util.PDFTextStripper.getText(PDFTextStripper.java:149)
at lucene.test.main(test.java:47)

我完全不知道为什么。使用教程创建PDF非常有用()。但是这个文本提取没有。已经搜索了很多,但没有任何帮助

顺便说一句,我仍然使用“pdfbox-0.7.3.jar”,因为新的“pdfbox-1.8.2.jar”不适合我。这可能是原因吗

谢谢你的帮助


PS:我在使用“stripper.writeText()”时遇到了相同的错误。

这行代码有问题

PDDocument pdfDocument=PDDocument.load(新文件(“text.pdf”)

在那里指定
text.pdf
的路径,即与路径一起

在不知道文件所在位置的情况下,JVM应该如何创建文件对象,这就是发生异常的原因。给那边的路,你就可以走了

更新


这似乎是一个错误,并已在以后的版本中修复

问题在于这条线

PDDocument pdfDocument=PDDocument.load(新文件(“text.pdf”)

在那里指定
text.pdf
的路径,即与路径一起

在不知道文件所在位置的情况下,JVM应该如何创建文件对象,这就是发生异常的原因。给那边的路,你就可以走了

更新


这似乎是一个错误,并已在以后的版本中修复

问题在于这条线

PDDocument pdfDocument=PDDocument.load(新文件(“text.pdf”)

在那里指定
text.pdf
的路径,即与路径一起

在不知道文件所在位置的情况下,JVM应该如何创建文件对象,这就是发生异常的原因。给那边的路,你就可以走了

更新


这似乎是一个错误,并已在以后的版本中修复

问题在于这条线

PDDocument pdfDocument=PDDocument.load(新文件(“text.pdf”)

在那里指定
text.pdf
的路径,即与路径一起

在不知道文件所在位置的情况下,JVM应该如何创建文件对象,这就是发生异常的原因。给那边的路,你就可以走了

更新


这似乎是一个错误,并已在以后的版本中修复

为此,请始终使用pdfbox 1.8.6和fop0.93

PDDocument=null; 尝试 { doc=新的PDDocument(); PDPage page=新PDPage(); 文件添加页(第页); PDPageContentStream contentStream=新的PDPageContentStream(文档,页面)

PDFont-pdfFont=PDType1Font.HELVETICA;
浮点数=25;
浮动前导=1.5f*fontSize;
PDRectangle mediabox=page.findMediaBox();
浮动保证金=72;
float width=mediabox.getWidth()-2*边距;
float startX=mediabox.getLowerLeftX()+边距;
float startY=mediabox.getUpperRightY()-边距;
String text=“你好,先生,终于创建了PDF:Thank”;
列表行=新的ArrayList();
int lastSpace=-1;
while(text.length()>0)
{
int spaceIndex=text.indexOf(“”,lastSpace+1);
if(spaceIndex<0)
{
行。添加(文本);
text=“”;
}
其他的
{
String subString=text.subString(0,spaceIndex);
float size=fontSize*pdfFont.getStringWidth(子字符串)/1000;
如果(尺寸>宽度)
{
if(lastSpace<0)//所以我们有一个单词比这条线长……无论如何画出来
lastSpace=spaceIndex;
subString=text.subString(0,lastSpace);
行。添加(子字符串);
text=text.substring(lastSpace.trim();
lastSpace=-1;
}
其他的
{
lastSpace=spaceIndex;
}
}
}
contentStream.beginText();
setFont(pdfFont,fontSize);
contentStream.moveTextPositionByAmount(startX,startY);
用于(字符串行:行)
{
contentStream.抽绳(线);
contentStream.moveTextPositionByAmount(0,-前导);
}
contentStream.endText();
contentStream.close();
doc.save(“E:\\document.pdf”);
}捕获(异常扩展){
logger.error(“[GetInformation]电子邮件id为”+exp);
}
最后
{
如果(doc!=null)
{
试一试{
doc.close();
}捕获(例外费用){
logger.错误(“[GetInformation]电子邮件id为”+expe);
}
}
}

为此始终使用此始终使用pdfbox 1.8.6和fop0.93

PDDocument=null; 尝试 { doc=新的PDDocument(); PDPage page=新PDPage(); 文件添加页(第页); PDPageContentStream contentStream=新的PDPageContentStream(文档,页面)

PDFont-pdfFont=PDType1Font.HELVETICA;
浮点数=25;
浮动前导=1.5f*fontSize;
PDRectangle mediabox=page.findMediaBox();
浮动保证金=72;
float width=mediabox.getWidth()-2*边距;
float startX=mediabox.getLowerLeftX()+边距;
float startY=mediabox.getUpperRightY()-边距;
String text=“你好,先生,终于创建了PDF:Thank”;
列表行=新的ArrayList();
int lastSpace=-1;
while(text.length()>0)
{
int spaceIndex=text.indexOf(“”,lastSpace+1);
if(spaceIndex<0)
{
行。添加(t
String s =  stripper.getText(pdfDocument);
        PDFont pdfFont = PDType1Font.HELVETICA;
        float fontSize = 25;
        float leading = 1.5f * fontSize;

        PDRectangle mediabox = page.findMediaBox();
        float margin = 72;
        float width = mediabox.getWidth() - 2*margin;
        float startX = mediabox.getLowerLeftX() + margin;
        float startY = mediabox.getUpperRightY() - margin;

        String text = "Hello sir finally PDF is created : thanks"; 
        List<String> lines = new ArrayList<String>();
        int lastSpace = -1;
        while (text.length() > 0)
        {
            int spaceIndex = text.indexOf(' ', lastSpace + 1);
            if (spaceIndex < 0)
            {
                lines.add(text);
                text = "";
            }
            else
            {
                String subString = text.substring(0, spaceIndex);
                float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
                if (size > width)
                {
                    if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
                        lastSpace = spaceIndex;
                    subString = text.substring(0, lastSpace);
                    lines.add(subString);
                    text = text.substring(lastSpace).trim();
                    lastSpace = -1;
                }
                else
                {
                    lastSpace = spaceIndex;
                }
            }
        }

        contentStream.beginText();
        contentStream.setFont(pdfFont, fontSize);
        contentStream.moveTextPositionByAmount(startX, startY);            
        for (String line: lines)
        {
            contentStream.drawString(line);
            contentStream.moveTextPositionByAmount(0, -leading);
        }
        contentStream.endText(); 
        contentStream.close();

         doc.save("E:\\document.pdf");
    }catch (Exception exp){
        logger.error("[GetInformation] email id is " +exp);

    }
    finally
    {
        if (doc != null)
        {
            try{
            doc.close();
            }catch (Exception expe){
                logger.error("[GetInformation] email id is " +expe);

            }
        }
    }
import org.apache.pdfbox.multipdf.Splitter;   
import org.apache.pdfbox.pdmodel.PDDocument;  
import java.io.File;   
import java.io.IOException;   
import java.util.List;   
import java.util.Iterator;  

public class PdfSplitting {  

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

          File file = new File("D:/test.pdf");  
          PDDocument document = PDDocument.load(file);   

          Splitter splitter = new Splitter();  

          List<PDDocument>Pages = splitter.split(document);  

          Iterator<PDDocument>iterator = Pages.listIterator();  

    int i = 1;  
    while(iterator.hasNext()) {  
             PDDocument pd = iterator.next();  
    pd.save("D:/test"+ i++ +".pdf");  
          }  
          System.out.println("Pdf spitted successfully");  
    document.close();  
    }  
}  
PDDocument pdfDocument = PDDocument.load(new File("text.pdf"));
PDDocument pdfDocument = PDDocument.load("C:\TEMP\text.pdf");