Adding a Watermark to PDF Output
To add a watermark to the PDF output of a DITA map transformation, create a DITA-OT customization directory and use it from a DITA Map to PDF transformation scenario, as in the following procedure:
- Copy the entire directory:
DITA_OT_DIR\plugins\org.dita.pdf2\Customization
to another location (for instance,C:\Customization
). - Copy your watermark image (for example,
watermark.png
) to:C:\Customization\common\artwork\watermark.png
. - Rename the
C:\Customization\catalog.xml.orig
file to:C:\Customization\catalog.xml
. - Open the
catalog.xml
in Oxygen XML Developer plugin and uncomment this line:<!--uri name="cfg:fo/xsl/custom.xsl" uri="fo/xsl/custom.xsl"/-->
The uncommented line should look like this:
<uri name="cfg:fo/xsl/custom.xsl" uri="fo/xsl/custom.xsl"/>
- Rename the file:
C:\Customization\fo\xsl\custom.xsl.orig
to:C:\Customization\fo\xsl\custom.xsl
- Open the
C:\Customization\fo\xsl\custom.xsl
file in Oxygen XML Developer plugin to overwrite two XSLT templates:- The first template is located in the XSLT stylesheet
DITA_OT_DIR\plugins\org.dita.pdf2\xsl\fo\static-content.xsl
and we override it specifying a watermark image for every page in the PDF content, using a block-container element that references the watermark image file:<fo:static-content flow-name="odd-body-header"> <fo:block-container absolute-position="absolute" top="-2cm" left="-3cm" width="21cm" height="29.7cm" background-image="{concat($artworkPrefix, '/Customization/OpenTopic/common/artwork/watermark.png')}"> <fo:block/> </fo:block-container> <fo:block xsl:use-attribute-sets="__body__odd__header"> <xsl:call-template name="insertVariable"> <xsl:with-param name="theVariableID" select="'Body odd header'"/> <xsl:with-param name="theParameters"> <prodname> <xsl:value-of select="$productName"/> </prodname> <heading> <fo:inline xsl:use-attribute-sets="__body__odd__header__heading"> <fo:retrieve-marker retrieve-class-name="current-header"/> </fo:inline> </heading> <pagenum> <fo:inline xsl:use-attribute-sets="__body__odd__header__pagenum"> <fo:page-number/> </fo:inline> </pagenum> </xsl:with-param> </xsl:call-template> </fo:block> </fo:static-content> </xsl:template>
- The second template that we override is located in the XSLT stylesheet
DITA_OT_DIR\plugins\org.dita.pdf2\xsl\fo\commons.xsl
and is used for styling the first page of the output. We also override it by copying the original template content in ourcustom.xsl
and adding theblock-container
element that references the watermark image file:<xsl:template name="createFrontMatter_1.0"> <fo:page-sequence master-reference="front-matter" xsl:use-attribute-sets="__force__page__count"> <xsl:call-template name="insertFrontMatterStaticContents"/> <fo:flow flow-name="xsl-region-body"> <fo:block-container absolute-position="absolute" top="-2cm" left="-3cm" width="21cm" height="29.7cm" background-image="{concat($artworkPrefix, '/Customization/OpenTopic/common/artwork/watermark.png')}"> <fo:block/> </fo:block-container> <fo:block xsl:use-attribute-sets="__frontmatter"> <!-- set the title --> <fo:block xsl:use-attribute-sets="__frontmatter__title"> <xsl:choose> <xsl:when test="$map/*[contains(@class,' topic/title ')][1]"> <xsl:apply-templates select="$map/*[contains(@class,' topic/title ')][1]"/> </xsl:when> <xsl:when test="$map//*[contains(@class,' bookmap/mainbooktitle ')][1]"> <xsl:apply-templates select="$map//*[contains (@class,' bookmap/mainbooktitle ')][1]"/> </xsl:when> <xsl:when test="//*[contains(@class, ' map/map ')]/@title"> <xsl:value-of select="//*[contains(@class, ' map/map ')]/@title"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="/descendant::*[contains (@class, ' topic/topic ')][1]/*[contains(@class, ' topic/title ')]"/> </xsl:otherwise> </xsl:choose> </fo:block> <!-- set the subtitle --> <xsl:apply-templates select="$map//*[contains (@class,' bookmap/booktitlealt ')]"/> <fo:block xsl:use-attribute-sets="__frontmatter__owner"> <xsl:apply-templates select="$map//*[contains (@class,' bookmap/bookmeta ')]"/> </fo:block> </fo:block> <!--<xsl:call-template name="createPreface"/>--> </fo:flow> </fo:page-sequence> <xsl:if test="not($retain-bookmap-order)"> <xsl:call-template name="createNotices"/> </xsl:if> </xsl:template>
- The first template is located in the XSLT stylesheet
- Edit your DITA Map PDF transformation scenario. In the
Parameters tab, set the customization.dir
parameter to
C:\Customization
.