Wednesday, January 9, 2008

How to insert watermark in LaTeX

For my first post this year, I'll start off with something a bit different. This time we'll take a look at how to insert a watermark in a document using LaTeX. Note here that by watermark I mean some text that will appear on the background of each page of the document.
As with everything else in the wonderful world of LaTeX, there are more ways to do this. I know of two, which I will write about here. The first one is fairly simple, just include the package draftcopy and voila. Here's a minimal example of how it works:


\documentclass{article}
\usepackage{draftcopy}

\title{Lorem ipsum}

\begin{document}
\maketitle

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\end{document}


Running the previous example through LaTeX gives the following output:

Note that running pdflatex on this example will NOT insert the watermark. I use LaTeX->dvi->ps->pdf route to obtain the pdf.
Of course the draftcopy package gives a lot of options to customize the watermark, of which I believe \draftcopyName is the most useful. If you want the word ENTWURF to be printed instead of DRAFT, which is used by default, you would add the line "\draftcopyName{ENTWURF}{155}" in the preamble. The number 155 is the scaling factor for the font---play around with it to get the scaling you want.
For those that like to do it the hard way (like me:) ), here is a piece of code that does pretty much the same thing:

\AddToShipoutPicture{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\makebox(600,-700){\rotatebox{45}{\textcolor[gray]{0.75}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%
}%
}

To use this code, you have to include the following packages: graphicx, eso-pic, and type1cm. Here is a minimal example:

\documentclass{article}

\usepackage{graphicx}
\usepackage{type1cm}
\usepackage{eso-pic}
\usepackage{color}

\makeatletter
\AddToShipoutPicture{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.75}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%
}%
}
\makeatother

\title{Lorem ipsum}

\begin{document}
\maketitle

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\end{document}

Be sure not to forget the \makeatletter and \makeatother commands. Unlike with draftcopy, running pdflatex on this example WILL insert the watermark in the pdf. I find this code nice because I can directly tinker with the various options. For example, if I want to change the angle of the text, I'll change the number inside the \rotatebox, if I want to move the text around the page, I'll change the two numbers inside \makebox, if I want to change the grayscale, I'll change the number inside \textcolor, etc.
UPDATE: The above code, as Ivijay pointed out, puts the word DRAFT on every page of the document. If you want it to appear on only one, you should use the
AddToShipoutPicture*
instead of
AddToShipoutPicture
. This command only adds the watermark to one page (first one if you leave the watermark code in your preamble).

UPDATE: Neno wants to know how to use a picture instead of text. Well, it's just a matter of replacing the lines

\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.75}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%

with something like

\includegraphics{image.eps}

Of course, you might have to play around with adjusting the width and height of the picture, depending on its size. Also, you might want to change tempdimb and tempdimc to properly position the image.

UPDATE: hcr suggests using draftwatermark package to do this sort of thing. I've just had a look at the package, and using it really seems simple. For most of the things I did here, there is a nice command that does that. However, some things are missing from the package. First, the color of the text is always grey (you can choose the grey scale, but can't have the watermark text in, say, red. There does not seem to be a way to change the typeface of the font for the watermark text, and the text is always centered, you can't move it around. And finally, there is no way you could insert a picture as a watermark (which is what Neno wanted to do), and IMHO this could be a big issue for many people who may want their company's logo as a watermark. So anyway, draftwatermark is a great choice if you just want to insert a word (centered) in the background of each page. If you need anything more sophisticated than that, you will have to do something in the line of what I described here.

UPDATE: Amy wants to know is there a way to put the watermark only on some pages, preferably using something like \begin{watermark} and \end{watermark}. Well, Amy, there is no easy solution to your problem. One thing you could do is use \AddToShipoutPicture* that places the watermark only on one page, and then paste the code wherever you need it. Of course, this is ugly. I have managed to hack together some solution that seems to work, but the code is really ugly and I don't advise anyone to use it unless you really need to :). So, here goes:

\documentclass{article}

\usepackage{graphicx}
\usepackage{type1cm}
\usepackage{eso-pic}
\usepackage{color}
\usepackage{everypage}

\newenvironment{water}{\AddEverypageHook{\waterb}}{\AddThispageHook{\waterb}\AddEverypageHook{\watere}}

\makeatletter
\newcommand{\waterb}{
\AddToShipoutPicture*{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{0.75}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%
}%
}}
\makeatother

\makeatletter
\newcommand{\watere}{
\AddToShipoutPicture*{%
\setlength{\@tempdimb}{.5\paperwidth}%
\setlength{\@tempdimc}{.5\paperheight}%
\setlength{\unitlength}{1pt}%
\put(\strip@pt\@tempdimb,\strip@pt\@tempdimc){%
\makebox(0,0){\rotatebox{45}{\textcolor[gray]{1}%
{\fontsize{6cm}{6cm}\selectfont{DRAFT}}}}%
}%
}}
\makeatother

\title{Lorem ipsum}

\begin{document}

page one

\newpage

\begin{water}
First watermarked page
\newpage
Second watermarked page
\newpage
Third watermarked page
\end{water}

\newpage

This page should not be watermarked
\end{document}

Well, Amy, let me know if this works for you...





26 comments:

ivijay said...

The above code(not the draftcopy package) makes the word 'DRAFT' appear in every page in the pdf. How to make it appear only in the current or One page.

ivijay said...

Ok, i figured that out.
I used AddToShipoutPicture* instead of just AddToShipoutPicture which puts it in a single page

Filox said...

LOL, I just put the same solution on the post :)

hcr.net said...

\usepackage{draftwatermark}
%\usepackage[firstpage]{draftwatermark}

% Use the following to make modification
%\SetWatermarkAngle{45}
%\SetWatermarkLightness{0.8}
%\SetWatermarkFontSize{5cm}
%\SetWatermarkScale{1}
%\SetWatermarkText{DRAFT}

% Note: pdflatex also works.
% See draftwatermark.sty

Nino Vessella said...

I'm new to LaTeX, but I enjoyed your code.

How can I use a picture instead of a text?

Filox said...

Neno, I've updated the post with a solution to your problem.

Nino Vessella said...

Thank you very much: it works!

As I said I a beginner, so I tried to use \includegraphics instead of the line \fontsize{6cm}{6cm}\selectfont{DRAFT}, but it didn't work!

Thanks a lot.

Unknown said...

Wow. I really appreciate finding this great code. It worked perfectly. Do you know of an environment (e.g., \begin{watermark}...\end{watermark}) that will watermark a portion of pages of a document?

Filox said...

Amy, I have posted a possible solution on the blog. It's really hacking things together in hope they will work, so there is a good chance it won't work. Let me know how it turns out...

skanky said...

For reference, eso-pic has a \ClearShipoutPicture command that removes the background image. Assuming you need it for specific sections, rather than page numbers, just inserting Add & Clear commands at the start and end of the required sections should do the trick (nb I haven't tried this as I haven't needed to do it).

Also, I know this is a bit late to the party, but I found this post via Google whilst trying to work out how to do what I wanted (see below) and thought it might be useful to others.

I wanted to use eso-pic to add an image to pages of letter documents. The image(s) are the company's headed paper, and I an using newlfm to create the letters.

newlfm has a background command to do this, but I just couldn't get it to position the background pdf correctly vertically. However, using eso-pic and AddToShipoutPicture I was able to do that.

However, the first page headed paper has the address, but subsequent pages have logos etc., but not the address, and thus need a second image.

After trying a few things (there were some hints above), I found that if I add the AddToShipoutPicture*{firstpdf} to the letterhead (see nwlfm) and then use AddToShipoutPicture{secondpdf} immediately after the \begin{newlfm} statement, the result is as required.

The \begin{newlfm} statement applies the header, but only for the first page, then the second AddToShipoutPicture command sets the background for all subsequent pages. Doing it this way, I don't need to know where the first page boundary is.

Gaspard said...

It may not be the ideal place to ask, but I'm not very familiar with low level LaTeX. Is it possible to choose a different picture/text for odd and even pages ?

Hari Sri said...

Thanks for the code. But can anyone suggest how to change the font? I mean either to timesroman or lucid , etc.,?

Bhupala!

AlfC said...

please, using
\usepackage[firstpage]{draftwatermark}
is so much simpler and it works in latex and pdflatex.

Shakes said...

I prefer using the watermark package.
\usepackage{watermark}

Eg. To add a figure to the current page only:

\thiswatermark{
%\centering
\put(0,-680){\includegraphics[width=0.98\textwidth]{figs/ribs}}
}

Unknown said...

thanks,
nice script .. .very useful.
the usepackage{draftcopy} does not work for me though .. maybe a missing file .. the script works well though, the placement needs to be adjusted sometimes .. thanks a ton!

Unknown said...

If i want to change the lightness of the image in the watermark how am i supposed to do that. Please help.

Nino Vessella said...

Hi, again!

Now I'm using watermarks, but this time the picture is a colored one. When I compile Latex->DVI, DVI->PS, PS->PDF (I'm using TexMaker, and I have to follow this method), in the final PDF all colors are lost! I'm using the package{color}, too.

Thanks for any help.

Unknown said...

Hi there,
I had the same problem as Amy. I need a watermark for 50 some pages in my appendix only. Therefor the watermark package did not work for me.

I therefore used the \begin{water}..\end{water} solution and it worked.
However the size of the writing is quite small and I would like to make it much bigger. But it is not possible to make it bigger. I tried many diferent values for the value of the fontsize, but that did not help.

Does anyone know a solution?
Thank you so much!

Unknown said...

I solved the problem by now. It is a problem with type=1cm
I deleted the line with the type=1cm package and instead used the following:

\RequirePackage{fix-cm}
\documentclass{article}

That solved the problem

Jonathan Aldrich said...

Since the blog is entitled "Daily rant" let me provide mine:

<rant>DON'T DO IT! I find a watermarks printed behind the text incredibly distracting and I am unable to focus on the text.</rant>

Maybe I'm the only person in the world with this issue, but please consider your readers before using watermarks ;-)

Unknown said...

Hi,

I found your post on watermarking in latex very helpful and I have a question. Suppose, I want to watermark on all pages but the first. How should I change the above code?

Thanks !!

Unknown said...

I love your codes!!! Hope to see more innovative codes in the future!!! Thank you!!!

BTW, is there any possibility you write some codes to produce the effect as \draftcopyVersion ?

Snigle said...

It is possible to start the watermark after a certain page using the draftcopy package. In the preamble, include the line:

\draftcopyFirstPage{3} % skips the draft mark until the 3rd page, eg.

Thanks for the detailed post!

Renee

Humility Consulting said...

When I do the above (in writelatex.com) I get:


This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian) (format=pdflatex 2014.5.23) 2 JUL 2014 18:12
entering extended mode
\write18 enabled.
file:line:error style messages enabled.
%&-line parsing enabled.
**main.tex
(./main.tex
LaTeX2e <2011/06/27>
.
.
.
.
.
.
(/usr/share/texlive/texmf-dist/tex/latex/type1cm/type1cm.sty)
(/usr/share/texlive/texmf-dist/tex/latex/eso-pic/eso-pic.sty

/usr/share/texlive/texmf-dist/tex/latex/eso-pic/eso-pic.sty:24: LaTeX Error: Mi
ssing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
...

l.24 \NeedsTeXFormat{LaTeX2e}[
1999/12/01]
Here is how much of TeX's memory you used:
1239 strings out of 493307
14895 string characters out of 6139905
80593 words of memory out of 5000000
4721 multiletter control sequences out of 15000+600000
3640 words of font info for 14 fonts, out of 8000000 for 9000
957 hyphenation exceptions out of 8191
23i,0n,19p,266b,101s stack positions out of 5000i,500n,10000p,200000b,80000s

/usr/share/texlive/texmf-dist/tex/latex/eso-pic/eso-pic.sty:24: ==> Fatal error occurred, no output PDF file produced!

Humility Consulting said...
This comment has been removed by the author.
BlackyDee said...

Hi Filox.

Your code works fine for me, but i does not "overwrite" pictures. Can you modify your code, so that the "DRAFT" is written even over pictures?

Thank you very much in advance.