簡體   English   中英

使用Itext找出PDf中未嵌入Font的位置或頁面

[英]Find out the location or page where the Font was not embedded in PDf using Itext

我正在使用Itext庫來操作我的PDF。

我正在使用此示例http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-16#616-listusedfonts.java來查找未嵌入PDF中的字體。

庫是否提供任何選項來檢查PDF中未嵌入字體的確切位置?

OP引用示例僅檢查頁面和從中引用的表單xobjects,並輸出有關這些實體資源中提供的字體的信息。

如果需要確定使用哪種字體的確切位置 ,則必須使用不同的機制,解析器包類具有自定義渲染偵聽器。 然后,當使用這種非嵌入字體時,該監聽器可以對文本繪制操作起作用。

解析器框架

要找出頁面上實際使用某些資源的位置,您必須解析頁面內容流並檢查其中的PDF說明。

iText通過提供一個解析內容流並對其進行預分析的解析器框架來幫助您實現這一目標。 第一次分析的結果將轉發給您提供的渲染偵聽器。

您可以像這樣使用解析器框架:

PdfReader reader = new PdfReader(SOURCE);
for (int page = from; page <= to; page++)
{
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    RenderListener renderListener = YOUR_RENDER_LISTENER_IMPLEMENTATION;
    parser.processContent(page, renderListener);
    // after the page has been processed, probably 
    // some render listener related post-processing
}

對於例如文本提取,通常使用渲染偵聽器實現LocationTextExtractionStrategySimpleTextExtractionStrategy (隨iText一起提供),並在頁面處理完成后,從您從頁面中的事件中提取的策略中檢索文本String

要自定義的渲染偵聽器

iText 5中的渲染偵聽器必須實現RenderListener接口:

public interface RenderListener {
    /**
     * Called when a new text block is beginning (i.e. BT)
     */
    public void beginTextBlock();

    /**
     * Called when text should be rendered
     * @param renderInfo information specifying what to render
     */
    public void renderText(TextRenderInfo renderInfo);

    /**
     * Called when a text block has ended (i.e. ET)
     */
    public void endTextBlock();

    /**
     * Called when image should be rendered
     * @param renderInfo information specifying what to render
     */
    public void renderImage(ImageRenderInfo renderInfo);
}

ExtRenderListener ,它聲明了一些額外的偵聽器方法。

您的任務的渲染偵聽器,即渲染偵聽器,用於查找確切地使用給定字體繪制文本的位置,只需要非常簡單地實現renderText ,例如:

public void renderText(TextRenderInfo renderInfo)
{
    DocumentFont documentFont = renderInfo.getFont();
    PdfDictionary font = documentFont.getFontDictionary();
    // Check the font dictionary like in your example code
    if (font FULFILLS SOME CRITERIA)
    {
        // The text
        String text = renderInfo.getText();
        // is rendered on the current page on the base line
        LineSegment baseline = renderInfo.getBaseline();
        // using a font fulfilling the given criteria
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM