简体   繁体   English

如何使用 PDFBox 使用 Header 字体大小阅读 PDF 部分?

[英]How to read PDF sections using Header font size using PDFBox?

I am trying to read PDF documents and I need them to be separated by sections using header font size or font and font size I currently have it implemented based on the answer of this post .我正在尝试阅读 PDF 文档,我需要使用 header 字体大小或字体和字体大小将它们按部分分隔,我目前根据这篇文章的答案实现了它。 But due to my PDF having the same font for header and the sub-header I need to modify the code so it would search based on font size or both.但是由于我的 PDF 具有与 header 和子标题相同的字体,我需要修改代码以便它可以根据字体大小或两者进行搜索。

    List<TextSectionDefinition> sectionDefinitions = Arrays.asList(
            new TextSectionDefinition("Section", x -> x.get(0).get(0).getFont().getName().contains("Calibri,Bold"), TextSectionDefinition.MultiLine.multiLineHeader, true)
    );

    document.getClass();
    PDFTextSectionStripper stripper = new PDFTextSectionStripper(sectionDefinitions);
    stripper.getText(document);

    System.out.println("Sections:");
    List<String> texts = new ArrayList<>();
    for (TextSection textSection : stripper.getSections()) {
        String text = textSection.toString();
        System.out.println(text);
        texts.add(text);
    }

    return ResponseEntity.ok(texts);

My problem stems if I try to use getFontSize instead of getFont it doesn't allow any parameters to be entered, in my case 16 (font size).如果我尝试使用 getFontSize 而不是 getFont,我的问题就在于它不允许输入任何参数,在我的情况下为 16(字体大小)。

In the answer you refer to there are text section definitions like this:在您提到的答案中,有如下文本部分定义:

new TextSectionDefinition("Titel",
    x->x.get(0).get(0).getFont().getName().contains("CMBX12"),
    MultiLine.singleLine,
    false)

I assume your remark我假设你的话

if I try to use getFontSize instead of getFont it doesn't allow any parameters to be entered, in my case 16如果我尝试使用 getFontSize 而不是 getFont 它不允许输入任何参数,在我的情况下是 16

indicates that you want to exchange the lambda expression in the second parameter表示要交换第二个参数中的lambda表达式

x->x.get(0).get(0).getFont().getName().contains("CMBX12")

by something that tests the font size.通过测试字体大小的东西。 Thus, have you tried replacing it by因此,您是否尝试过将其替换为

x->x.get(0).get(0).getFontSize() == 16

or或者

x->x.get(0).get(0).getFontSizeInPt() == 16

or或者

x-> {
    float size = x.get(0).get(0).getFontSizeInPt();
    return size > 15 && size < 17;
}

yet?然而?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM