14

の長方形はどのような寸法と方向にありますか

PDFTextStripperByAreaの関数addRegion(String regionName, Rectangle2D rect)

言い換えると、長方形Rはどこから始まり、どのくらいの大きさ(原点の値の寸法、長方形の寸法)で、どの方向に進むか(図の青い矢印の方向)、new Rectangle(10,10,100,100)秒として指定されている場合パラメータ?

PdfBox長方形

4

3 に答える 3

12
new Rectangle(10,10,100,100)

長方形の左上隅が(10、10)の位置にあるため、PDFドキュメントの左と上部から10単位離れていることを意味します。ここで、「単位」は1 pt=1/72インチです。

最初の100は長方形の幅を表し、2番目の100は長方形の高さを表します。要約すると、右の写真が最初の写真です。

関数への引数として指定されたページの一部の領域を抽出するために、このコードを作成しました。

Rectangle2D region = new Rectangle2D.Double(x, y, width, height);
String regionName = "region";
PDFTextStripperByArea stripper;

stripper = new PDFTextStripperByArea();
stripper.addRegion(regionName, region);
stripper.extractRegions(page);

したがって、xとyは長方形の左上隅の絶対座標であり、その幅と高さを指定します。pageは、この関数の引数として指定されたPDPage変数です。

于 2012-07-16T09:01:10.167 に答える
1

こういうことを考えていたので、見つけたものを渡すと思いました。

itextを使用して元のPDFを作成するためのコードは次のとおりです。

import com.lowagie.text.Document
import com.lowagie.text.Paragraph
import com.lowagie.text.pdf.PdfWriter

class SimplePdfCreator {
    void createFrom(String path) {
        Document d = new Document()
        try {
            PdfWriter writer = PdfWriter.getInstance(d, new FileOutputStream(path))
            d.open()
            d.add(new Paragraph("This is a test."))
            d.close()
        } catch (Exception e) {
            e.printStackTrace()
        }
    }
}

PDFをクラックして開くと、左上隅にテキストが表示されます。これがあなたが探しているものを示すテストです。

@Test
void createFrom_using_pdf_box_to_extract_text_targeted_extraction() {
    new SimplePdfCreator().createFrom("myFileLocation")
    def doc = PDDocument.load("myFileLocation")
    Rectangle2D.Double d = new Rectangle2D.Double(0, 0, 120, 100)
    def stripper = new PDFTextStripperByArea()
    def pages = doc.getDocumentCatalog().allPages
    stripper.addRegion("myRegion", d)
    stripper.extractRegions(pages[0])
    assert stripper.getTextForRegion("myRegion").contains("This is a test.")
}

位置(0、0)は、ドキュメントの左上隅です。幅と高さは右下に向かっています。範囲を少し(35、52、120、3)に縮小しても、テストに合格することができました。

すべてのコードはGroovyで書かれています。

于 2012-04-01T21:50:35.370 に答える
1
Code in java using PDFBox.

 public String fetchTextByRegion(String path, String filename, int pageNumber) throws IOException {
        File file = new File(path + filename);
        PDDocument document = PDDocument.load(file);
        //Rectangle2D region = new Rectangle2D.Double(x,y,width,height);
        Rectangle2D region = new Rectangle2D.Double(0, 100, 550, 700);
        String regionName = "region";
        PDFTextStripperByArea stripper;
        PDPage page = document.getPage(pageNumber + 1);
        stripper = new PDFTextStripperByArea();
        stripper.addRegion(regionName, region);
        stripper.extractRegions(page);
        String text = stripper.getTextForRegion(regionName);
        return text;
    }
于 2018-08-10T11:38:58.027 に答える