Java를 사용한 지역 기반 추출
Contents
[
Hide
]
직사각형 페이지 영역에서 텍스트 추출
페이지의 정의된 영역으로 추출을 제한하려면 TextSearchOptions를 Rectangle과 함께 사용하세요.
Document인스턴스에서 소스 PDF를 여세요.- 선택한 페이지 영역에서 텍스트를 수집하려면
TextAbsorber를 생성하세요. - 대상
Rectangle에 대해TextSearchOptions를 만들고setLimitToPageBounds(true)를 활성화하세요. 추출이 표시되는 페이지 상자 내에 유지됩니다. - 구성된 검색 옵션을 흡수체에 적용하고 대상
Page를 방문하세요. - 추출된 텍스트 버퍼를 출력 파일에 쓰세요.
public static void extractTextFromRegion(Path inputFile, Path outputFile, int pageNumber, Rectangle rectangle)
throws Exception {
try (Document document = new Document(inputFile.toString())) {
TextAbsorber absorber = new TextAbsorber();
TextSearchOptions options = new TextSearchOptions(rectangle);
options.setLimitToPageBounds(true);
absorber.setTextSearchOptions(options);
document.getPages().get_Item(pageNumber).accept(absorber);
Files.writeString(outputFile, absorber.getText());
}
}
기하학 정보가 포함된 단락 추출
추출된 텍스트와 함께 섹션 직사각형 및 단락 다각형을 검사하려면 ParagraphAbsorber를 사용하세요.
Document인스턴스에서 소스 PDF를 여세요.ParagraphAbsorber를 생성하고 대상Page에 방문하여 페이지 마크업 정보를 구축하세요.- 첫 번째 페이지 마크업 결과를 읽고 해당 섹션과 단락을 반복하세요.
TextFragment라인에서 재구성된 각 섹션 직사각형, 단락 다각형 및 단락 텍스트를 수집하세요.- 기하학과 추출된 텍스트 세부정보를 사용하여 출력 보고서를 작성하세요.
- 추출된 세부정보를 출력 파일에 쓰세요.
public static void extractParagraphsWithGeometry(Path inputFile, Path outputFile) throws Exception {
try (Document document = new Document(inputFile.toString())) {
ParagraphAbsorber absorber = new ParagraphAbsorber();
absorber.visit(document.getPages().get_Item(1));
PageMarkup pageMarkup = absorber.getPageMarkups().get(0);
StringBuilder text = new StringBuilder();
int sectionIndex = 1;
for (MarkupSection section : pageMarkup.getSections()) {
text.append("Section ").append(sectionIndex)
.append(": rectangle = ").append(section.getRectangle()).append("\n");
int paragraphIndex = 1;
for (MarkupParagraph paragraph : section.getParagraphs()) {
text.append(" Paragraph ").append(paragraphIndex)
.append(": polygon = ").append(Arrays.toString(paragraph.getPoints())).append("\n");
StringBuilder paragraphText = new StringBuilder();
for (List<TextFragment> line : paragraph.getLines()) {
for (TextFragment fragment : line) {
paragraphText.append(fragment.getText());
}
paragraphText.append("\r\n");
}
text.append(" Text: ").append(paragraphText).append("\n\n");
paragraphIndex++;
}
sectionIndex++;
}
Files.writeString(outputFile, text.toString());
}
}