首页 > 解决方案 > 如何按具有特定大小或字体的标题拆分 PDF

问题描述

我正在尝试根据他的大小或字体按他的标题拆分 PDF。

目前我只能提取 PDF 的字体,但我需要知道大小或字体以查看该文本是否是标题。我不知道如何通过具有特定大小或字体的标题阅读 PDF,我知道它可以提取文本,但就是这样,简单的文本,你怎么知道哪个大小或字体有该文本?

顺便说一句,我已经能够通过他的书签以及书签的“孩子”来拆分 PDF

但我需要更深入地拆分 PDF。这就是为什么我试图让标题按它们拆分 PDF 的原因。

我做了一些研究,但我找不到对这种情况非常有用的东西。

这里有一些问题:

一些代码

HashSet<String> fontNames = new HashSet<string>();
PdfDictionary resources;
for (int p = 1; p <= reader.NumberOfPages; p++)
{
    PdfDictionary dic = reader.GetPageN(p);
    resources = dic.GetAsDict(PdfName.RESOURCES);

    if (resources != null)
    {
        //get fonts dictionary
        PdfDictionary fonts = resources.GetAsDict(PdfName.FONT);
        if (fonts != null)
        {
            PdfDictionary font;
            foreach (PdfName key in fonts.Keys)
            {
                font = fonts.GetAsDict(key);
                String name = font.GetAsName(PdfName.BASEFONT).ToString();
                fontNames.Add(name);
            }

        }
    }
}

另一种方式 List<object[]> fonts2 = BaseFont.GetDocumentFonts(reader);

其他代码:获取文本

ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(reader, i, strategy);

words = currentText.Split('\n');
for (int j = 0, len = words.Length; j < len; j++)
{
    line = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(words[j]));
}

标签: c#pdfsplititext

解决方案


我用这个:

public void RenderText(iTextSharp.text.pdf.parser.TextRenderInfo renderInfo)

renderInfo参数有我需要的所有东西。

renderInfo.GetFont().PostscriptFontName;  // Font Name
renderInfo.GetBaseline().GetStartPoint(); // Coordinates - (56.6929 , 727.8466, 1)
renderInfo.GetAscentLine().GetEndPoint(); // Coordinates - (96.78018, 737.1749, 1)

推荐阅读