PDFlib

高度なPDFアプリケーションの開発を支援する定番プログラムライブラリー Supported by インフォテック株式会社

PDFlib TET サンプル集(クックブック)

本サンプルプログラムは、PDF テキスト抽出ライブラリーの実装である TET の基本的な機能を実際のプログラムで紹介したものです。

本サイトでダウンロードした TET は、一部機能の制限を除き、評価版として無償でお使いいただけます。

フォント検索

PDFlib TET で、ドキュメントに使用されているフォントをページ番号、位置情報と共にリストするサンプルプログラムです。


<?xml version="1.0" encoding="UTF-8"?>
<!--
    (C) PDFlib GmbH 2008 www.pdflib.com

    Purpose: For all fonts in a document, list all occurrences along with
    page number and position information.

    Expected input: TETML in "glyph" or "wordplus" mode.

    Stylesheet parameters: none

    Version: $Id: fontfinder.xsl,v 1.2 2008/11/19 10:37:32 stm Exp $
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:tet="http://www.pdflib.com/XML/TET3/TET-3.0">
  <xsl:output method="text" />

  <xsl:template match="/">
    <!-- Make sure that font information is present in the input TETML. -->
    <xsl:if test="tet:TET/tet:Document/tet:Pages//tet:Content[not(@font = 'true')]">
      <xsl:message terminate="yes">
        <xsl:text>Stylesheet fontfinder.xsl processing TETML for document '</xsl:text>
        <xsl:value-of select="tet:TET/tet:Document/@filename" />
        <xsl:text>': this stylesheet requires font info in TETML. </xsl:text>
        <xsl:text>Create the input in page mode "glyph" or "wordplus".</xsl:text>
      </xsl:message>
    </xsl:if>

    <!-- Iterate over all fonts in the document. -->
    <xsl:for-each select="tet:TET/tet:Document/tet:Pages/tet:Resources/tet:Fonts/tet:Font">
      <xsl:variable name="current-font-id" select="@id" />

      <!-- Leave an empty line after each font block. -->
      <xsl:if test="position() != 1">
        <xsl:text>&#xa;</xsl:text>
      </xsl:if>

      <!-- Print header for current font. -->
      <xsl:value-of select="@name" />
      <xsl:text> used on:&#xa;</xsl:text>

      <!-- Select all pages in the document where the current font occurs. -->
      <xsl:for-each select="ancestor::tet:Pages[1]/tet:Page[.//tet:Word[tet:Box/tet:Glyph/@font = $current-font-id]]">
        <!-- Header for each page. -->
        <xsl:text>page </xsl:text>
        <xsl:value-of select="@number" />
        <xsl:text>:&#xa;</xsl:text>

        <!-- Select all words in the page that refer to the current font. -->
        <xsl:for-each select=".//tet:Word[tet:Box/tet:Glyph/@font = $current-font-id]">
          <xsl:variable name="font-index" select="position() - 1" />
          <!--
            Separate coordinates within line by commata, and begin a
            new line after each eighth coordinate.
          -->
          <xsl:choose>
            <xsl:when test="$font-index = 0" />
            <xsl:when test="$font-index mod 8 = 0">
              <xsl:text>,&#xa;</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>, </xsl:text>
            </xsl:otherwise>
          </xsl:choose>

          <!-- Print lower left coordinates. -->
          <xsl:text>(</xsl:text>
          <xsl:value-of select="round(tet:Box/@llx)" />
          <xsl:text>, </xsl:text>
          <xsl:value-of select="round(tet:Box/@lly)" />
          <xsl:text>)</xsl:text>
        </xsl:for-each>

        <xsl:text>&#xa;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
(May 6, 2010 - Feb 21, 2014)