Java 8 PDFbox can't getText of pdf file
I am working on an application in Java 8 to process text from PDFs. I am using PDFbox version 2.0.12 in a Maven project. I am using the following method to extract text from a number of PDFs:
for(File file : fileEntry){
PDDocument pdd = PDDocument.load(file);
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(pdd);
System.out.println(text);
}
However, on this particular file:
http://www.o-cha.net/english/cup/pdf/29.pdf
I'm getting the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException: TTF fonts do not have a CFF table
at org.apache.fontbox.ttf.OpenTypeFont.getCFF(OpenTypeFont.java:56)
at org.apache.pdfbox.pdmodel.font.PDCIDFontType0.<init>(PDCIDFontType0.java:136)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createDescendantFont(PDFontFactory.java:121)
at org.apache.pdfbox.pdmodel.font.PDType0Font.<init>(PDType0Font.java:192)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:83)
at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:146)
at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:848)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:503)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:477)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:150)
at org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:139)
at org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:391)
at org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:319)
at org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:266)
at org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:227)
I'm not sure, but I believe this is because the PDF is using some font which PDFbox is not familiar with. I'm not sure this is the case, because as far as I'm aware the getText
method of the PDFTextStripper class should ignore all styling. The documentation states:
This class will take a pdf document and strip out all of the text and ignore the formatting and such.
If this is the case, why am I getting this exception?
Thank you in advanced.
java pdf fonts java-8 pdfbox
add a comment |
I am working on an application in Java 8 to process text from PDFs. I am using PDFbox version 2.0.12 in a Maven project. I am using the following method to extract text from a number of PDFs:
for(File file : fileEntry){
PDDocument pdd = PDDocument.load(file);
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(pdd);
System.out.println(text);
}
However, on this particular file:
http://www.o-cha.net/english/cup/pdf/29.pdf
I'm getting the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException: TTF fonts do not have a CFF table
at org.apache.fontbox.ttf.OpenTypeFont.getCFF(OpenTypeFont.java:56)
at org.apache.pdfbox.pdmodel.font.PDCIDFontType0.<init>(PDCIDFontType0.java:136)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createDescendantFont(PDFontFactory.java:121)
at org.apache.pdfbox.pdmodel.font.PDType0Font.<init>(PDType0Font.java:192)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:83)
at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:146)
at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:848)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:503)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:477)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:150)
at org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:139)
at org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:391)
at org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:319)
at org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:266)
at org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:227)
I'm not sure, but I believe this is because the PDF is using some font which PDFbox is not familiar with. I'm not sure this is the case, because as far as I'm aware the getText
method of the PDFTextStripper class should ignore all styling. The documentation states:
This class will take a pdf document and strip out all of the text and ignore the formatting and such.
If this is the case, why am I getting this exception?
Thank you in advanced.
java pdf fonts java-8 pdfbox
I try your code and works fine, check if you file is not corrupt.
– crack81
Nov 19 '18 at 21:45
Works fine for me when using the ExtractText command line utility of pdfbox-app. I do get three warn messages like "Using fallback ArialUnicodeMS for CID-keyed font FutoMinA101-Bold" but these are harmless in this context. Please test with the command line utility too. And check if you have several different versions in your classpath.
– Tilman Hausherr
Nov 20 '18 at 7:07
I cannot reproduce the issue either, not with the current PDFBox snapshot and not with the release 2.0.12. Probably your download is corrupt, probably your runtime class path does contain a different PDFBox (or dependency) version
– mkl
Nov 20 '18 at 11:16
1
Always remember that in cases like these, you want to create a Minimal, Complete, and Verifiable example, not just "so others have something to test", but also to help yourself do a sanity check: does it actually go wrong in a new dir with just a Test.java with a singlemain
method, importing just the PDF package, compiled with justjavac
? If so, awesome, that's debuggable. If not: the problem is somewhere else.
– Mike 'Pomax' Kamermans
Nov 20 '18 at 18:22
add a comment |
I am working on an application in Java 8 to process text from PDFs. I am using PDFbox version 2.0.12 in a Maven project. I am using the following method to extract text from a number of PDFs:
for(File file : fileEntry){
PDDocument pdd = PDDocument.load(file);
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(pdd);
System.out.println(text);
}
However, on this particular file:
http://www.o-cha.net/english/cup/pdf/29.pdf
I'm getting the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException: TTF fonts do not have a CFF table
at org.apache.fontbox.ttf.OpenTypeFont.getCFF(OpenTypeFont.java:56)
at org.apache.pdfbox.pdmodel.font.PDCIDFontType0.<init>(PDCIDFontType0.java:136)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createDescendantFont(PDFontFactory.java:121)
at org.apache.pdfbox.pdmodel.font.PDType0Font.<init>(PDType0Font.java:192)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:83)
at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:146)
at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:848)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:503)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:477)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:150)
at org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:139)
at org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:391)
at org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:319)
at org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:266)
at org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:227)
I'm not sure, but I believe this is because the PDF is using some font which PDFbox is not familiar with. I'm not sure this is the case, because as far as I'm aware the getText
method of the PDFTextStripper class should ignore all styling. The documentation states:
This class will take a pdf document and strip out all of the text and ignore the formatting and such.
If this is the case, why am I getting this exception?
Thank you in advanced.
java pdf fonts java-8 pdfbox
I am working on an application in Java 8 to process text from PDFs. I am using PDFbox version 2.0.12 in a Maven project. I am using the following method to extract text from a number of PDFs:
for(File file : fileEntry){
PDDocument pdd = PDDocument.load(file);
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(pdd);
System.out.println(text);
}
However, on this particular file:
http://www.o-cha.net/english/cup/pdf/29.pdf
I'm getting the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException: TTF fonts do not have a CFF table
at org.apache.fontbox.ttf.OpenTypeFont.getCFF(OpenTypeFont.java:56)
at org.apache.pdfbox.pdmodel.font.PDCIDFontType0.<init>(PDCIDFontType0.java:136)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createDescendantFont(PDFontFactory.java:121)
at org.apache.pdfbox.pdmodel.font.PDType0Font.<init>(PDType0Font.java:192)
at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:83)
at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:146)
at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:848)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:503)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:477)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:150)
at org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:139)
at org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:391)
at org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:319)
at org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:266)
at org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:227)
I'm not sure, but I believe this is because the PDF is using some font which PDFbox is not familiar with. I'm not sure this is the case, because as far as I'm aware the getText
method of the PDFTextStripper class should ignore all styling. The documentation states:
This class will take a pdf document and strip out all of the text and ignore the formatting and such.
If this is the case, why am I getting this exception?
Thank you in advanced.
java pdf fonts java-8 pdfbox
java pdf fonts java-8 pdfbox
asked Nov 19 '18 at 21:20
Jonathan HindsJonathan Hinds
658
658
I try your code and works fine, check if you file is not corrupt.
– crack81
Nov 19 '18 at 21:45
Works fine for me when using the ExtractText command line utility of pdfbox-app. I do get three warn messages like "Using fallback ArialUnicodeMS for CID-keyed font FutoMinA101-Bold" but these are harmless in this context. Please test with the command line utility too. And check if you have several different versions in your classpath.
– Tilman Hausherr
Nov 20 '18 at 7:07
I cannot reproduce the issue either, not with the current PDFBox snapshot and not with the release 2.0.12. Probably your download is corrupt, probably your runtime class path does contain a different PDFBox (or dependency) version
– mkl
Nov 20 '18 at 11:16
1
Always remember that in cases like these, you want to create a Minimal, Complete, and Verifiable example, not just "so others have something to test", but also to help yourself do a sanity check: does it actually go wrong in a new dir with just a Test.java with a singlemain
method, importing just the PDF package, compiled with justjavac
? If so, awesome, that's debuggable. If not: the problem is somewhere else.
– Mike 'Pomax' Kamermans
Nov 20 '18 at 18:22
add a comment |
I try your code and works fine, check if you file is not corrupt.
– crack81
Nov 19 '18 at 21:45
Works fine for me when using the ExtractText command line utility of pdfbox-app. I do get three warn messages like "Using fallback ArialUnicodeMS for CID-keyed font FutoMinA101-Bold" but these are harmless in this context. Please test with the command line utility too. And check if you have several different versions in your classpath.
– Tilman Hausherr
Nov 20 '18 at 7:07
I cannot reproduce the issue either, not with the current PDFBox snapshot and not with the release 2.0.12. Probably your download is corrupt, probably your runtime class path does contain a different PDFBox (or dependency) version
– mkl
Nov 20 '18 at 11:16
1
Always remember that in cases like these, you want to create a Minimal, Complete, and Verifiable example, not just "so others have something to test", but also to help yourself do a sanity check: does it actually go wrong in a new dir with just a Test.java with a singlemain
method, importing just the PDF package, compiled with justjavac
? If so, awesome, that's debuggable. If not: the problem is somewhere else.
– Mike 'Pomax' Kamermans
Nov 20 '18 at 18:22
I try your code and works fine, check if you file is not corrupt.
– crack81
Nov 19 '18 at 21:45
I try your code and works fine, check if you file is not corrupt.
– crack81
Nov 19 '18 at 21:45
Works fine for me when using the ExtractText command line utility of pdfbox-app. I do get three warn messages like "Using fallback ArialUnicodeMS for CID-keyed font FutoMinA101-Bold" but these are harmless in this context. Please test with the command line utility too. And check if you have several different versions in your classpath.
– Tilman Hausherr
Nov 20 '18 at 7:07
Works fine for me when using the ExtractText command line utility of pdfbox-app. I do get three warn messages like "Using fallback ArialUnicodeMS for CID-keyed font FutoMinA101-Bold" but these are harmless in this context. Please test with the command line utility too. And check if you have several different versions in your classpath.
– Tilman Hausherr
Nov 20 '18 at 7:07
I cannot reproduce the issue either, not with the current PDFBox snapshot and not with the release 2.0.12. Probably your download is corrupt, probably your runtime class path does contain a different PDFBox (or dependency) version
– mkl
Nov 20 '18 at 11:16
I cannot reproduce the issue either, not with the current PDFBox snapshot and not with the release 2.0.12. Probably your download is corrupt, probably your runtime class path does contain a different PDFBox (or dependency) version
– mkl
Nov 20 '18 at 11:16
1
1
Always remember that in cases like these, you want to create a Minimal, Complete, and Verifiable example, not just "so others have something to test", but also to help yourself do a sanity check: does it actually go wrong in a new dir with just a Test.java with a single
main
method, importing just the PDF package, compiled with just javac
? If so, awesome, that's debuggable. If not: the problem is somewhere else.– Mike 'Pomax' Kamermans
Nov 20 '18 at 18:22
Always remember that in cases like these, you want to create a Minimal, Complete, and Verifiable example, not just "so others have something to test", but also to help yourself do a sanity check: does it actually go wrong in a new dir with just a Test.java with a single
main
method, importing just the PDF package, compiled with just javac
? If so, awesome, that's debuggable. If not: the problem is somewhere else.– Mike 'Pomax' Kamermans
Nov 20 '18 at 18:22
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382793%2fjava-8-pdfbox-cant-gettext-of-pdf-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382793%2fjava-8-pdfbox-cant-gettext-of-pdf-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I try your code and works fine, check if you file is not corrupt.
– crack81
Nov 19 '18 at 21:45
Works fine for me when using the ExtractText command line utility of pdfbox-app. I do get three warn messages like "Using fallback ArialUnicodeMS for CID-keyed font FutoMinA101-Bold" but these are harmless in this context. Please test with the command line utility too. And check if you have several different versions in your classpath.
– Tilman Hausherr
Nov 20 '18 at 7:07
I cannot reproduce the issue either, not with the current PDFBox snapshot and not with the release 2.0.12. Probably your download is corrupt, probably your runtime class path does contain a different PDFBox (or dependency) version
– mkl
Nov 20 '18 at 11:16
1
Always remember that in cases like these, you want to create a Minimal, Complete, and Verifiable example, not just "so others have something to test", but also to help yourself do a sanity check: does it actually go wrong in a new dir with just a Test.java with a single
main
method, importing just the PDF package, compiled with justjavac
? If so, awesome, that's debuggable. If not: the problem is somewhere else.– Mike 'Pomax' Kamermans
Nov 20 '18 at 18:22