You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by 二川村田 <su...@gmail.com> on 2017/06/03 23:49:50 UTC

space between words

Hello

I tried to extract below pdf.
http://jpdb.nihs.go.jp/jp17e/000217650.pdf
http://jpdb.nihs.go.jp/jp17e/

But no spaces between words.
What should I do to extract correctly?

=================
I search the solutions on google using below words.
"pdfbox space between words"
But I couldn't find the solutions.

=================
I used below method, but result is not changed.
PDFTextStripper.setSpacingTolerance(float spacingToleranceValue)

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by 二川村田 <su...@gmail.com>.
Hello, Mr. Hausherr.
Thanks for you, now I can get space information.

I corrected my code that I sent 2 days ago.
========================
String p_text = stripper.getText(doc);

Iterator<String> it_str = Arrays.asList(p_text.split("")).iterator();
int ind_tp = 0;
List<TextPosition> list_tp = stripper.list_text;
int len_list_tp = list_tp.size();
while (it_str.hasNext()) {
    String ch = it_str.next();
    String str_rec = "Text: " + ch;

    if (ind_tp < len_list_tp) {
        TextPosition tp = list_tp.get(ind_tp);
        if (ch.equals(tp.toString())){
            str_rec += "\tx: " + tp.getX()
                    + "\ty: " + tp.getY()
                    + "\tw: " + tp.getWidth()
                    + "\th: " + tp.getHeight()
                    + "\tfont_size: " + tp.getFontSizeInPt();
            ind_tp++;
        }
    }

    System.out.println(str_rec);
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 05.06.2017 um 06:55 schrieb 二川村田:
> Thank you for your reply, Mr. Hausherr.
>
> I could understand you said.
>
> Can I get DrawPrintImageLocations.java?
>

It is in the source code download at 
https://pdfbox.apache.org/download.cgi , or here:

https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java?view=markup&sortby=date

Tilman

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by 二川村田 <su...@gmail.com>.
Thank you for your reply, Mr. Hausherr.

I could understand you said.

Can I get DrawPrintImageLocations.java?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 04.06.2017 um 16:45 schrieb 二川村田:
> Thank you for your reply, Mr. Hausherr.
>
> I send my codes.
>
> It looks similar to the codes you sent.

Hi,

The difference is, you're subclassing PDFTextStripper to get the actual 
text position from the PDF. And this way you won't get any spaces 
because there are none in the PDF.

To illustrate this, I've uploaded page 3 treated with the 
DrawPrintImageLocations.java example from the source code download. See 
its source code for explanation on the colors.

http://imgur.com/a/H5CNR

The spaces from text extraction (that you get e.g. with 
"stripper.getText(doc);" ) are added by PDFBox but these have no 
TextPosition object.

Tilman

>
> I want to use Java program, not commandline application.
>
> I use the library pdfbox-2.0.6.jar
>
> =====================
> //class extends PDFTextStripper
> class PDFTextCordinateStripper extends PDFTextStripper {
>
> public List<TextPosition> list_text = new ArrayList<TextPosition>();
>
> public PDFTextCordinateStripper() throws IOException {
> super();
> }
>
> protected void processTextPosition(TextPosition text) {
> super.processTextPosition(text);
> list_text.add(text);
> }
>
> }
>
>
> =====================
> // main(omited)
> PDFTextCordinateStripper stripper = new PDFTextCordinateStripper();
>
> int len_page = doc.getNumberOfPages();
> for (int ind = 1; ind <= len_page; ind++) {
>
> PDPage pg = doc.getPage(ind - 1);
>
> String str_page_num = "PageNum: " + ind;
>
> String str_page_size =
> "Width: " + pg_w
> + "\tHeight: " + pg_h;
>
> System.out.println(str_page_num + "\t" + str_page_size);
>
> stripper.list_text.clear();
> stripper.setStartPage(ind);
> stripper.setEndPage(ind);
> stripper.getText(doc);
>
> Iterator<TextPosition> it_text = stripper.list_text.iterator();
> while (it_text.hasNext()) {
> TextPosition rec = it_text.next();
> String str_rec
> = "Text: " + rec.toString()
> + "\tx: " + rec.getX()
> + "\ty: " + rec.getY()
> + "\tw: " + rec.getWidth()
> + "\th: " + rec.getHeight()
> + "\tfont_size: " + rec.getFontSizeInPt();
> System.out.println(str_rec);
> }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by 二川村田 <su...@gmail.com>.
Thank you for your reply, Mr. Hausherr.

I send my codes.

It looks similar to the codes you sent.

I want to use Java program, not commandline application.

I use the library pdfbox-2.0.6.jar

=====================
//class extends PDFTextStripper
class PDFTextCordinateStripper extends PDFTextStripper {

public List<TextPosition> list_text = new ArrayList<TextPosition>();

public PDFTextCordinateStripper() throws IOException {
super();
}

protected void processTextPosition(TextPosition text) {
super.processTextPosition(text);
list_text.add(text);
}

}


=====================
// main(omited)
PDFTextCordinateStripper stripper = new PDFTextCordinateStripper();

int len_page = doc.getNumberOfPages();
for (int ind = 1; ind <= len_page; ind++) {

PDPage pg = doc.getPage(ind - 1);

String str_page_num = "PageNum: " + ind;

String str_page_size =
"Width: " + pg_w
+ "\tHeight: " + pg_h;

System.out.println(str_page_num + "\t" + str_page_size);

stripper.list_text.clear();
stripper.setStartPage(ind);
stripper.setEndPage(ind);
stripper.getText(doc);

Iterator<TextPosition> it_text = stripper.list_text.iterator();
while (it_text.hasNext()) {
TextPosition rec = it_text.next();
String str_rec
= "Text: " + rec.toString()
+ "\tx: " + rec.getX()
+ "\ty: " + rec.getY()
+ "\tw: " + rec.getWidth()
+ "\th: " + rec.getHeight()
+ "\tfont_size: " + rec.getFontSizeInPt();
System.out.println(str_rec);
}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 04.06.2017 um 07:17 schrieb Tilman Hausherr:
> Here's what I got with 2.0.6 using the ExtractText command line 
> application:
>
> JP XVII
> THE JAPANESE PHARMACOPOEIA
> SEVENTEENTH EDITION
> Official from April 1, 2016
> English Version
> THE MINISTRY OF HEALTH, LABOUR AND WELFARE
> Notice: This English Version of the Japanese Pharmacopoeia is published
> for the convenience of users unfamiliar with the Japanese language. When
> and if any discrepancy arises between the Japanese original and its 
> English
> translation, the former is authentic. 



I forgot to mention, if you want text like this, use PDFTextStripper.

PDFTextStripper stripper = new PDFTextStripper();
stripper.setStartPage(1); // 1 based
stripper.setEndPage(3);
String text = stripper.getText(doc);


This will extract pages 1 - 3.

Tilman

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: space between words

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 04.06.2017 um 01:49 schrieb 二川村田:
> Hello
>
> I tried to extract below pdf.
> http://jpdb.nihs.go.jp/jp17e/000217650.pdf
> http://jpdb.nihs.go.jp/jp17e/
>
> But no spaces between words.
> What should I do to extract correctly?
>
> =================
> I search the solutions on google using below words.
> "pdfbox space between words"
> But I couldn't find the solutions.
>
> =================
> I used below method, but result is not changed.
> PDFTextStripper.setSpacingTolerance(float spacingToleranceValue)

Here's what I got with 2.0.6 using the ExtractText command line application:

JP XVII
THE JAPANESE PHARMACOPOEIA
SEVENTEENTH EDITION
Official from April 1, 2016
English Version
THE MINISTRY OF HEALTH, LABOUR AND WELFARE
Notice: This English Version of the Japanese Pharmacopoeia is published
for the convenience of users unfamiliar with the Japanese language. When
and if any discrepancy arises between the Japanese original and its English
translation, the former is authentic.
The Ministry of Health, Labour and
Welfare Ministerial Notification No. 64
Pursuant to Paragraph 1, Article 41 of the Law on Securing Quality, 
Efficacy and
Safety of Products including Pharmaceuticals and Medical Devices (Law 
No. 145,
1960), the Japanese Pharmacopoeia (Ministerial Notification No. 65, 
2011), which
has been established as follows*, shall be applied on April 1, 2016. 
However, in the
case of drugs which are listed in the Pharmacopoeia (hereinafter 
referred to as ``previ-
ous Pharmacopoeia'') [limited to those listed in the Japanese 
Pharmacopoeia whose
standards are changed in accordance with this notification (hereinafter 
referred to as
``new Pharmacopoeia'')] and have been approved as of April 1, 2016 as 
prescribed
under Paragraph 1, Article 14 of the same law [including drugs the 
Minister of
Health, Labour and Welfare specifies (the Ministry of Health and Welfare 
Ministerial
Notification No. 104, 1994) as of March 31, 2016 as those exempted from 
marketing
approval pursuant to Paragraph 1, Article 14 of the Same Law 
(hereinafter referred
to as ``drugs exempted from approval'')], the Name and Standards 
established in the
previous Pharmacopoeia (limited to part of the Name and Standards for 
the drugs
concerned) may be accepted to conform to the Name and Standards 
established in the
new Pharmacopoeia before and on September 30, 2017. In the case of drugs 
which
are listed in the new Pharmacopoeia (excluding those listed in the 
previous Phar-
macopoeia) and drugs which have been approved as of April 1, 2016 as 
prescribed
under Paragraph 1, Article 14 of the same law (including those exempted from
approval), they may be accepted as those being not listed in the new 
Pharmacopoeia
before and on September 30, 2017.

(...)

What code did you use?

Or did you use code to get the TextPosition objects directly? In that 
case, you'll get spaces only if there are in the PDF itself. But that 
PDF doesn't have any, at least not on page 3. The spaces you see in the 
text above are created by PDFBox by using heuristics. Most PDF files 
don't have any spaces, they just position the non-space glyphs. Example 
from the content stream:

   BT
     0 Tr
     0.0011 Tc
     /TT2 1 Tf
     21.8597 0 0 21.8653 151.92 746.5394 Tm
     [ (The) -331.3 (M) -1.5 (inistry) -330.6 (o) -0.4 (f) -329.4 (H) 
-2.6 (ealth,) -329.6 (Labour) -332.1 (and) ] TJ
   ET


Tilman



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org