Whenever you try to extract text (plain or with styling information) from a PDF using PDFBox, you generally should start trying using the PDFTextStripper
class or one of its relatives. This class already does all the heavy lifting involved in PDF content parsing for you.
You use the plain PDFTextStripper
class like this:
PDDocument document = ...;
PDFTextStripper stripper = new PDFTextStripper();
// set stripper start and end page or bookmark attributes unless you want all the text
String text = stripper.getText(document);
This returns merely the plain text, e.g. from some R40 form:
Claim for repayment of tax deducted
from savings and investments
How to fill in this form
Please fill in this form with details of your income for the
above tax year. The enclosed Notes will help you (but there is
not a note for every box on the form). If you need more help
with anything on this form, please phone us on the number
shown above.
If you are not a UK resident, do not use this form – please
contact us.
Please do not send us any personal records, or tax
certificates or vouchers with your form. We will contact
you if we need these.
Please allow four weeks before contacting us about your
repayment. We will pay you as quickly as possible.
Use black ink and capital letters
Cross out any mistakes and write the
correct information below
...
You can, on the other hand, overwrite its method writeString(String, List<TextPosition>)
and process more information than the mere text. To add information on the name of the used font wherever the font changes, you can use this:
PDFTextStripper stripper = new PDFTextStripper() {
String prevBaseFont = "";
protected void writeString(String text, List<TextPosition> textPositions) throws IOException
{
StringBuilder builder = new StringBuilder();
for (TextPosition position : textPositions)
{
String baseFont = position.getFont().getBaseFont();
if (baseFont != null && !baseFont.equals(prevBaseFont))
{
builder.append('[').append(baseFont).append(']');
prevBaseFont = baseFont;
}
builder.append(position.getCharacter());
}
writeString(builder.toString());
}
};
For the same form you get
[DHSLTQ+IRModena-Bold]Claim for repayment of tax deducted
from savings and investments
How to fill in this form
[OIALXD+IRModena-Regular]Please fill in this form with details of your income for the
above tax year. The enclosed Notes will help you (but there is
not a note for every box on the form). If you need more help
with anything on this form, please phone us on the number
shown above.
If you are not a UK resident, do not use this form – please
contact us.
[DHSLTQ+IRModena-Bold]Please do not send us any personal records, or tax
certificates or vouchers with your form. We will contact
you if we need these.
[OIALXD+IRModena-Regular]Please allow four weeks before contacting us about your
repayment. We will pay you as quickly as possible.
Use black ink and capital letters
Cross out any mistakes and write the
correct information below
...
If you don't want the font information to be merged with the text, simply create separate structures in your method overwrite.
TextPosition
offers a lot more information on the piece of text it represents. Inspect it!