Trouble opening file with Java to use with PDFBox

조회 수: 21 (최근 30일)
Michael Boeckel
Michael Boeckel 2016년 5월 9일
댓글: Michael Boeckel 2016년 5월 10일
I am trying to use the PDFBox library to read the contents of PDF files, but I can't seem to open any of the files in the correct format for PDFBox to use. I'm using the following code to open each document:
javaaddpath('...\pdfParseDemo\pdfbox-2.0.0.jar')
javaaddpath('...\FontBox-0.1.0\FontBox-0.1.0\lib\FontBox-0.1.0.jar')
pdfname = '...\example.pdf';
import java.io.*;
pdfdoc = org.apache.pdfbox.pdmodel.PDDocument; %Define a PDDocument object placeholder
pdfdoc.load(FileInputStream(pdfname)); %Load the PDF file
However, this seems to return an empty object. When I try to query any of the file's properties or contents, it always returns an empty or zero value. I suspect the problem is with how I'm opening the file, because I know PDFBox has been successfully used natively with Java in many cases. Unfortunately the documentation for interfacing with Matlab is very sparse, so I'm not sure what I should be doing differently. Is there some kind of weirdness with how Matlab handles Java file input calls?

채택된 답변

Elias Gule
Elias Gule 2016년 5월 9일
Try wrapping your pdfname variable in a java.lang.String variable. This sometimes works:
pdfname = java.lang.String('...\example.pdf');
  댓글 수: 2
Michael Boeckel
Michael Boeckel 2016년 5월 9일
Good suggestion, but no joy, sadly. I tried this with several different variations on the loading call, but none worked.
Michael Boeckel
Michael Boeckel 2016년 5월 10일
Disregard! I tried your suggestion with the Java "File" constructor instead of the FileInputStream constructor, and with a bit of coaxing, that worked! Many thanks good sir!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by