Adding a Watermark to XHTML and PDF output

Oxygen general issues.
OxUser
Posts: 18
Joined: Tue Mar 12, 2013 1:39 am

Adding a Watermark to XHTML and PDF output

Post by OxUser »

How can I add a Watermark (such as Draft) to the XHTML and PDF output?

Thanks
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

Hi,

Please follow these steps for adding a watermark to the PDF output of the DITA map transformation:
  • create a custom XSL stylesheet called custom.xsl in the directory [Oxygen-install-dir]\frameworks\dita\DITA-OT\plugins\org.dita.pdf2\Customization\fo\attrs that sets the path of the watermark image, for example:

    Code: Select all

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:variable name="imageDir" select="'/common/artwork/'"/>
    <xsl:variable name="imageWatermarkPath"><xsl:value-of select="$imageDir"/>draft.png</xsl:variable>
    </xsl:stylesheet>
  • rename the file catalog.xml.orig to catalog.xml in directory [Oxygen-install-dir]\frameworks\dita\DITA-OT\plugins\org.dita.pdf2\Customization
  • uncomment the following line in the file catalog.xml:

    Code: Select all

    <!--uri name="cfg:fo/attrs/custom.xsl" uri="fo/attrs/custom.xsl"/-->
  • add the following template in file [Oxygen-install-dir]\frameworks\dita\DITA-OT\plugins\org.dita.pdf2\xsl\fo\static-content.xsl:

    Code: Select all

      <xsl:template name="insertImage">
    <fo:block-container absolute-position="fixed" top="160mm">
    <fo:block>
    <fo:external-graphic src="url({concat($customizationDir.url, $imageWatermarkPath)})" xsl:use-attribute-sets="image" />
    </fo:block>
    </fo:block-container>
    </xsl:template>
  • add a call to this template:

    Code: Select all

    <xsl:call-template name="insertImage"/>
    before the </fo:block> tag in each of the following templates in the same file [Oxygen-install-dir]\frameworks\dita\DITA-OT\plugins\org.dita.pdf2\xsl\fo\static-content.xsl: insertBodyOddHeader, insertBodyEvenHeader, insertBodyFirstHeader, insertBodyFirstFooter, insertBodyLastHeader, insertBodyLastFooter, insertBodyFootnoteSeparator, insertBodyOddFooter, insertBodyEvenFooter, insertTocOddHeader, insertTocEvenHeader, insertTocOddFooter, insertTocEvenFooter.
  • set the parameter customization.dir to value ${frameworksDir}/dita/DITA-OT/plugins/org.dita.pdf2/Customization in a customized version of the DITA Map PDF transform
  • copy the image file draft.png in directory [Oxygen-install-dir]\frameworks\dita\DITA-OT\plugins\org.dita.pdf2\Customization\common\artwork, that is the image file name and directory that we set up above in the first step
  • run the customized version of the DITA Map PDF transform

For the XHTML output just set a custom CSS with the parameter args.css and set the parameter args.copycss to true in the DITA Map XHTML transform. The custom CSS should contain:

Code: Select all

 body {
background-image: url(images/draft.png);
}

Regards,
Sorin
gwhite
Posts: 70
Joined: Fri Jul 27, 2012 1:46 am

Re: Adding a Watermark to XHTML and PDF output

Post by gwhite »

This is exactly what I have been looking for today. It works well, except that it printed the image twice on every page, which I think is caused by including the call in the footer as well as the header templates. When I took the call out of the footer templates, the image printed once per page as expected.

Is it possible to tie printing of the watermark to the args.draft parameter in the PDF transformation, so that I can turn it on and off easily? As it is, it always prints. (I'm not sure I understood your mechanism for invoking the watermark, but I think it may be indicated in your step:
set the parameter customization.dir to value ${frameworksDir}/dita/DITA-OT/plugins/org.dita.pdf2/Customization in a customized version of the DITA Map PDF transform.
If so, does that mean you are to use that transform only for publishing to draft, and that you must have a specially named Customization directory only for that purpose? Otherwise how does the DITA-OT know when to print the image?)

Thanks.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

gwhite wrote:Is it possible to tie printing of the watermark to the args.draft parameter in the PDF transformation, so that I can turn it on and off easily? As it is, it always prints.
The args.draft parameter is used in DITA-OT for draft-comment elements, not for watermark images. It is not easy to extend this parameter to include watermark images too.
gwhite wrote:If so, does that mean you are to use that transform only for publishing to draft, and that you must have a specially named Customization directory only for that purpose? Otherwise how does the DITA-OT know when to print the image?)
Having two customization directories just for turning the watermark image on and off would be overkill. You should have only one customization directory, create a new parameter that turns the watermark on and off (call it args.watermark for example), set that parameter in the DITA PDF transformation and pass it to the XSL transformation by adding it in the DITA-OT build.xml file. In fact I think you should add it in the DITA-OT/plugins/org.dita.pdf2/build.xml file and run the DITA-OT Integrator after that. In the customized XSL transformation from the org.dita.pdf2 plugin the watermark should be added to the PDF page only if the parameter is set to true.


Regards,
Sorin
gwhite
Posts: 70
Joined: Fri Jul 27, 2012 1:46 am

Re: Adding a Watermark to XHTML and PDF output

Post by gwhite »

I attempted to do what you suggested, setting up a parameter in the transformation scenario called args.watermark and setting it to "Draft" to print the watermark image or "" in order not to print. Then to ../plugins/org.dita.pdf2/build.xml I added:

Code: Select all

      <param name="args.watermark" expression="${args.watermark}"></param>
...and I ran DITA Integrator, but when I run the transform, the image still prints regardless. Is it conditional logic that's missing from build.xml? Something like:

Code: Select all

    <condition property="args.watermark" value="">
<not><isset property="args.watermark"></isset></not>
</condition>
Is that closer to the mark? I'm not getting how the scenario distinguishes that the watermark is or is not wanted.

Gary
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

It's closer to the mark but you did not check the value of the parameter in the XSL file. You are just receiving the parameter args.watermark in the Ant build.xml file but you are not receiving it and handling it in the XSL file.

First you should change the parameter in the Ant file to:

Code: Select all

 <param name="WATERMARK" expression="${args.watermark}" if="args.watermark"/>
Did you place it in the same <xslt> element as the following existing parameter?

Code: Select all

<param name="DRAFT" expression="${args.draft}"></param>
After that you should declare the watermark parameter in the file OXYGEN_INSTALL_DIR/frameworks/dita/DITA-OT/plugins/org.dita.pdf2/xsl/fo/topic2fo.xsl:

Code: Select all

<xsl:param name="WATERMARK" select="'no'"/>
next to the following existing line:

Code: Select all

<xsl:param name="DRAFT"/>
And of course you should check the value of the args.watermark parameter in the file static-content.xsl (or a customization of this file in your customization.dir), something like:

Code: Select all

  <xsl:template name="insertImage">
<xsl:if test="$WATERMARK = 'yes'">
<fo:block-container absolute-position="fixed" top="160mm">
<fo:block>
<fo:external-graphic src="url({concat($customizationDir.url, $imageWatermarkPath)})" xsl:use-attribute-sets="image" />
</fo:block>
</fo:block-container>
</xsl:if>
</xsl:template>

Regards,
Sorin
gwhite
Posts: 70
Joined: Fri Jul 27, 2012 1:46 am

Re: Adding a Watermark to XHTML and PDF output

Post by gwhite »

Works great. Thank you!
gwhite
Posts: 70
Joined: Fri Jul 27, 2012 1:46 am

Re: Adding a Watermark to XHTML and PDF output

Post by gwhite »

I have run into a new problem with the functioning of the draft watermark solution you communicated in this thread. I have begun moving toward creating a separate plugin in the DITA-OT plugins folder for the different kinds of PDF output my company requires -- such as one that prints a different title page, headers and footers, and so on for a client company. I've been following the examples in Leigh White's DITA For Print, and my PDF plugin transforms have been choking on this draft watermark. If you look at your original example, you say to put the following in the Customization/fo/attr/custom.xsl file:

Code: Select all

  <xsl:variable name="imageDir" select="'/common/artwork/'"/>
<xsl:variable name="imageWatermarkPath"><xsl:value-of select="$imageDir"/>draft.png</xsl:variable>
But adopting the concept of separate plugins for different purposes, there is no custom.xsl file in a Customization folder. There are custom.xsl files in the ..cfg/attr and ..cfg/xsl folders. I've tried placing the variable declaration in various files in those folders, but the transform always fails. I believe everything else is set up properly. Any ideas where the problem is occurring? BTW, the error I get is:
transform.topic2fo.main:
[xslt] Processing C:\Users\gwhite\Documents\Publishing\temp\pdf-ndo\oxygen_dita_temp\stage1a.xml to C:\Users\gwhite\Documents\Publishing\temp\pdf-ndo\oxygen_dita_temp\stage2.fo
[xslt] Loading stylesheet C:\DITA-OTProjectSettings\DITA-OT\plugins\org.dita.pdf2\xsl\fo\topic2fo_shell_fop.xsl
[xslt] C:\DITA-OTProjectSettings\DITA-OT\plugins\org.dita.pdf2\xsl\fo\static-content.xsl:888:133: Fatal Error! Variable imageWatermarkPath has not been declared (or its declaration is not in scope)
[xslt] Failed to process C:\Users\gwhite\Documents\Publishing\temp\pdf-ndo\oxygen_dita_temp\stage1a.xml
BUILD FAILED
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

Hello,
gwhite wrote:I have run into a new problem with the functioning of the draft watermark solution you communicated in this thread. I have begun moving toward creating a separate plugin in the DITA-OT plugins folder for the different kinds of PDF output my company requires -- such as one that prints a different title page, headers and footers, and so on for a client company.

. . .

But adopting the concept of separate plugins for different purposes, there is no custom.xsl file in a Customization folder.
I suggest keeping only one plugin, the one that comes with DITA-OT and creates PDF output, and setting different customization directories in the parameter customization.dir for the different kinds of PDF output that you need. Did you try this?
Regards,
Sorin

<oXygen/> XML Editor Support
gwhite
Posts: 70
Joined: Fri Jul 27, 2012 1:46 am

Re: Adding a Watermark to XHTML and PDF output

Post by gwhite »

Sorin,

The approach of multiple Customization folders would work if it were only me, but it's not as good a solution for a documentation team who have to produce many different types of PDF output. Maintaining all those Customization folders for multiple team members wouldn't work as well as adding plugins to the plugin folder. I think I would be able to make progress on this if I understood the answer to the specific question: in what xsl file do the variable declarations belong that invoke the watermark? And then, does that affect the way the referenced directory location is expressed?

<xsl:variable name="imageDir" select="'/common/artwork/'"/>
<xsl:variable name="imageWatermarkPath"><xsl:value-of select="$imageDir"/>draft.png</xsl:variable>

The custom.xsl files sort of stand in for and override the other xsl files that affect PDF processing, right? Changes introduced to them often amount to a tweak of another file, such as root-processing.xsl, or tables.xsl. If I knew which file to add the variable declaration to, I think that would be the best solution.

Gary
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

Gary,
gwhite wrote:But adopting the concept of separate plugins for different purposes, there is no custom.xsl file in a Customization folder. There are custom.xsl files in the ..cfg/attr and ..cfg/xsl folders. I've tried placing the variable declaration in various files in those folders, but the transform always fails. I believe everything else is set up properly.
Yes, there is a custom.xsl file in a Customization folder. This is true for any PDF plugin added to DITA-OT. It does not matter if it is the PDF plugin that comes with DITA-OT or a new plugin that you add to DITA-OT, provided your plugin is based on the one that comes with DITA-OT.
gwhite wrote:The custom.xsl files sort of stand in for and override the other xsl files that affect PDF processing, right? Changes introduced to them often amount to a tweak of another file, such as root-processing.xsl, or tables.xsl.
Right.
gwhite wrote:If I knew which file to add the variable declaration to, I think that would be the best solution.
You have to modify the same files in every PDF plugin, just like you did in the above customization (4 months ago) when you said that it worked great.
Regards,
Sorin

<oXygen/> XML Editor Support
Boreas
Posts: 86
Joined: Wed Feb 09, 2011 10:43 pm

Re: Adding a Watermark to XHTML and PDF output

Post by Boreas »

Hello,

I am able to display the watermark on all pages. I implemented the changes in my customPDF plugin.

I am trying to implement the parameter in the scenario so that I can choose when to display the watermark, but with no success. :(

To implement the parameter args.watermark, I used the files in the org.dita.pdf2. The build.xml and topic2fo.xsl file I have in my custom plugin are almost empty. I was expecting the scenario to pick up the info from the org.dita.pdf2 folder, since nothing was defined in my plugin.

I am not sure what the build.xml and topic2fo.xsl files of my plugin should contain. The same as the org.dita.pdf2? But I was told for another issue to only transfer in the plugin what was modified.

Not quite sure how to handle the build.xml and topic2fo.xsl files of my plugin.

Regards
Carole
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

Hello Carole,
Boreas wrote:I am not sure what the build.xml and topic2fo.xsl files of my plugin should contain. The same as the org.dita.pdf2? But I was told for another issue to only transfer in the plugin what was modified.

Not quite sure how to handle the build.xml and topic2fo.xsl files of my plugin.
For adding to the Oxygen DITA PDF transformation a parameter args.watermark for controlling the addition of a watermark image please follow the above instructions in this post which give you an example of what code to add in each of the following files:

OXYGEN_INSTALL_DIR/frameworks/dita/DITA-OT/plugins/org.dita.pdf2/build.xml

OXYGEN_INSTALL_DIR/frameworks/dita/DITA-OT/plugins/org.dita.pdf2/xsl/fo/topic2fo.xsl

OXYGEN_INSTALL_DIR/frameworks/dita/DITA-OT/plugins/org.dita.pdf2/xsl/fo/static-content.xsl


Did you add the parameter in the build.xml file as instructed above, like the following?

Code: Select all

<param name="WATERMARK" expression="${args.watermark}" if="args.watermark"/>
Did you add the parameter in topic2fo.xsl as instructed above, like the following?

Code: Select all

<xsl:param name="WATERMARK" select="'no'"/>
Did you check the value of the parameter (possible values 'yes' and 'no') in the file static-content.xsl as in the above example?
Regards,
Sorin

<oXygen/> XML Editor Support
Boreas
Posts: 86
Joined: Wed Feb 09, 2011 10:43 pm

Re: Adding a Watermark to XHTML and PDF output

Post by Boreas »

Hello,

you are suggesting that all changes should be done in the org.dita.pdf2, regardless if I have custom plugin to generate my PDF?
At one point in the post, it is said
You have to modify the same files in every PDF plugin, just like you did in the above customization (4 months ago) when you said that it worked great.
I used the build and topic2fo of the org.dita.pdf2 and the static-content.xsl from my plugin, is this the mistake?

Carole
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Adding a Watermark to XHTML and PDF output

Post by sorin_ristache »

Hello Carole,

In a PDF transformation only one plugin is used: either the org.dita.pdf2 plugin or your own custom plugin. This is why all changes should be done in one plugin, the one used in the transformation. So if you are using your own custom plugin for generating the PDF output then you should add all the changes in that plugin, otherwise the changes should go in the org.dita.pdf2 plugin.
Regards,
Sorin

<oXygen/> XML Editor Support
johnmacleren
Posts: 1
Joined: Thu Jan 14, 2016 11:12 am

Re: Adding a Watermark to XHTML and PDF output

Post by johnmacleren »

An online free tool is available which I used earlier to add watermark into PDF documents. You can also try it: http://onlinefreeware.net/pdf/watermark.aspx
shilpah123
Posts: 9
Joined: Wed Mar 13, 2019 6:37 am

Re: Adding a Watermark to XHTML and PDF output

Post by shilpah123 »

Hi,
If we use the watermark image for XHTML output, it prints multiple times on the page. Is there a way to print the watermark only once per HTML page? Thanks.
shilpah123
Posts: 9
Joined: Wed Mar 13, 2019 6:37 am

Re: Adding a Watermark to XHTML and PDF output

Post by shilpah123 »

Hello,
I have found a solution to print watermark once per page. Pasting the code to be used in CSS below -
body {
background-image: url(watermark.png);
vertical-align: middle;
background-repeat: no-repeat;
}
Post Reply