XSL-FO 高级技术6

来源:互联网 发布:手机分辨率修改软件 编辑:程序博客网 时间:2024/05/17 02:49
PDF 书签 第 1 页(共5 页)

最新的 FOP 工具版本支持 PDF 书签。这种特性很有用,它不是 XSL-FO 标准的一部分。PDF 书签允许您创建文档结构的大纲。下面是一组书签的抓屏:

Adobe Acrobat Reader 中书签的抓屏

在上面的图像中,主标题(边上有加号或减号图标的标题)是文档的主要章节,出现在它们下面的项是特定主题中的副标题。可以单击其中任何一个书签来直接转至 PDF 文件的那个部分。


书签扩展元素 第 2 页(共5 页)

FOP 工具提供了 <fox:outline><fox:label> 这两个元素来允许将书签添加到 PDF 文件。这两个元素都是名称空间http://xml.apache.org/fop/extensions 的一部分。<fox:outline> 元素定义书签,<fox:label> 元素定义书签的文本。<fox:outline> 元素的 internal-destination 属性是链接的 id。要将一个书签嵌套在另一个书签中,只要将一个 <fox:outline> 元素放到另一个 <fox:outline> 元素中。下面是一些分层的书签:

<fox:outline internal-destination="toc">  <fox:label>Table of Contents</fox:label></fox:outline><fox:outline internal-destination="att">  <fox:label>About this tutorial</fox:label>  <fox:outline internal-destination="take">    <fox:label>Should I take this tutorial?</fox:label>  </fox:outline>  <fox:outline internal-destination="help">    <fox:label>Getting help</fox:label>  </fox:outline>  <fox:outline internal-destination="samples">    <fox:label>A word about the samples</fox:label>  </fox:outline></fox:outline>

因为所有这些元素都来自另一个名称空间,所以需要确保声明该名称空间。做到这一点的最简单方法是在 <fo:root> 元素上声明该名称空间:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"  xmlns:fox="http://xml.apache.org/fop/extensions">
用 XSLT 生成的书签扩展元素 第 3 页(共5 页)

要用 XSLT 样式表生成书签扩展元素,需要从 XML 源代码生成 <fox:outline><fox:label> 元素。如果 XML 源代码有类似 HTML 的标题,那么最简单的方法是为每个标题创建书签:

<xsl:template match="h1|h2|h3|h4">  <fox:outline internal-destination="{@id}">    <fox:label>      <xsl:value-of select="."/>    </fox:label>  </fox:outline></xsl:template>

该方法会产生两个问题:书签不是分层的,HTML 标题必须使用在 XHTML 中引入的 id 属性。当原始 XML 标记具有某种结构时,生成分层书签会容易得多。例如,生成本教程的 XML 标记具有这种标题结构:

<tutorial>  <title>XSL Formatting Objects advanced techniques</title>  <section>    <title>Tutorial introduction and preparation</title>    <panel>      <title>What this tutorial covers</title>    </panel>    <panel>      <title>What you need to know already to benefit from this tutorial</title>    </panel>  </section>  <section>    <title>Handling lists</title>  ...</tutorial>

对于这种类型的标记,您可以为节标题生成 <fox:outline> 元素,然后为该 <section> 中的每个 <panel> 元素生成 <fox:outline> 元素。一旦完成这一步,就继续处理下一个<section>。这为您提供了一组分层书签,它们反映了文档结构。

最后要注意一点:不是每个 <section><panel> 都必须有一个 id。如果缺少 id,会根据文档结构生成一个。例如,为第四个 <section> 元素生成的 idsec4,为第四个 <section> 元素中的第三个 <panel> 生成的 idsec4-3


目录 第 4 页(共5 页)

既然您已经知道如何构建一组书签,那么为文档构建目录就很简单。正如您所料想的,您可以使用 id 特性来引用文件中的各种格式化对象,然后使用 <fo:page-number-citation> 元素来插入页号。最后一个方法就是在标题及其页号之间插入 <fo:leader> 元素来创建虚线。下面演示了格式化对象如何表示本教程的目录:

<fo:block font-size="14pt" line-height="17pt"  space-after="3pt"   text-align="start" text-align-last="justify">  <fo:basic-link color="blue"     internal-destination="att">    1. Tutorial introduction and preparation  </fo:basic-link>  <fo:leader leader-pattern="dots"     leader-pattern-width="5pt"/>  <fo:page-number-citation ref-id="att"/></fo:block>

该示例创建一块两端对齐的文本,左边包含至本教程某一节的链接,右边包含该节的起始页号,它们之间是一条虚线。下面是创建了该标记的 XSLT 模板:

<xsl:template match="section">  <fo:block font-size="14pt" line-height="17pt"    space-after="3pt"    text-align="start" text-align-last="justify">    <fo:basic-link color="blue"      internal-destination="{@id}">      <xsl:value-of select="position()"/>      <xsl:text>. </xsl:text>      <xsl:value-of select="title"/>    </fo:basic-link>    <fo:leader leader-pattern="dots"      leader-pattern-width="5pt"/>    <fo:page-number-citation ref-id="{@id}"/>  </fo:block></xsl:template>

该模板使用 XSLT 的 position() 函数来确定每个 <section> 元素的位置,它使用 <xsl:value-of> 元素来检索节标题(<title> 元素)的文本。


书签和目录示例 第 5 页(共5 页)

与前面一样,可以通过研究示例文件来结束对这一主题的研究,该示例文件包含页号、目录、三种不同的页面布局和一组分层书签。下面是该目录的抓屏:

advanced.pdf 样本文件的抓屏

正如您所期望的,您可以查看文件来了解格式化对象是如何显示的。