[Tex/LaTex] Xwatermark prints watermark only on the first page

draftwatermarkwatermarkxwatermark

According to the documentation, the following should produce watermarks on every page, but it only appears on the first page:

\newwatermark*[allpages,angle=45]{watermark}

It seems xwatermark treated the doc as a single page documentation, oddpages produced the same effect as allpages and evenpages the watermark will never show up. I used \newpage in my doc and disabled page number.

And if I move text mark to background, it also doesn't appear at all:

\newwatermark[allpages,angle=45]{watermark}

However draftwatermark successfully printed watermark on all pages. But I need some features in xwatermark so need to make it work.


Edit: It's a rather large file, base configurations for this tex file as following. I think it should included all samples that might concern the question.

\documentclass[10pt]{article}

% encoding
\usepackage[T1]{fontenc}

% watermark
\usepackage{xwatermark}
\usepackage{textcomp} 

\usepackage{afterpage}


% line spacing
\usepackage{setspace}

% use color in latex
\usepackage{xcolor}

% character encoding
\usepackage[utf8]{inputenc}
\usepackage{CJKutf8}

% adjust the page margins
\usepackage[scale=0.75,a4paper,bindingoffset=0.2in,left=0.5in,right=1in,top=1in,bottom=1in,footskip=.25in]{geometry}

% define page header and footer
\usepackage{fancyhdr}
\pagestyle{fancy}

\fancyhf{}
\renewcommand{\headrulewidth}{\iffloatpage{0pt}{0pt}}
\renewcommand{\footrulewidth}{\iffloatpage{0pt}{0pt}}

% define page style
\fancypagestyle{summary}{
  \fancyhf{} % Clear all headers/footers
  \renewcommand{\headrulewidth}{4mm}
  \fancyhead[L]{% header left-aligned
    \fontsize{12}{14} \selectfont
    \textcolor{grey}{}\vspace{1em}
  }
  \renewcommand{\headrule}{\hbox to\headwidth{%
    \color{green}\leaders\hrule height \headrulewidth\hfill}}
  \renewcommand{\footrulewidth}{0pt}% No footer rule
}

% set watermark
\newwatermark*[allpages,angle=45]{watermark}

\begin{document}
\begin{CJK*}{UTF8}{gbsn}

page 1 ...
\newpage

page 2 ...
\newpage

\clearpage\end{CJK*}
\end{document}

Edit: @steve in comments compiled a file with watermarks on both pages, but I didn't. The sencond page has no watermark. I'm working on Ubuntu.

I found a historical problem some years ago here: xwatermark allpages does not work on Ubuntu but comments saying it shouldn't matter with operating system. While it demonstrated to be.

Best Answer

It seems to be caused by the broken way of getting the lastpage number in xwatermark package.

The internal macro \lastdocpage is used to mark the last page number. And if you print the macro you will find it always be 1.

So here is a work around by using additional packages to find the last page.

\documentclass{article}

\usepackage[2020-02-02]{latexrelease} % <- for avoidance of incompatibility
% more at: https://tex.stackexchange.com/questions/566088

\usepackage{xcolor}
\usepackage[printwatermark]{xwatermark}
\newwatermark[allpages,textmark={All pages},angle=0,scale=2,ypos=0]{}
\newwatermark[oddpages,textmark={Odd pages},angle=0,scale=2,ypos=50]{}
\newwatermark[evenpages,textmark={Even pages},angle=0,scale=2,ypos=50]{}

% fix \lastdocpage
\usepackage{lastpage}
\usepackage{refcount}
\renewcommand{\lastdocpage}{\getpagerefnumber{LastPage}}

\begin{document}
\setcounter{page}{100}
\null\newpage
\null\newpage
\null\newpage
\null
\end{document}