javahtmlitextitext7pdf-conversion

iText7 - Html to PDF - Footer with page counter - How change color


Using iText7 for Java, I try to convert a HTML into PDF. I try to change the style of my footer, without success.

My HTML :

<html>
    <head>
        <style>
            #header {
                position: running(header);
                text-align: left;
                margin-top: 50pt;
                margin-left: 320pt;
                font-family: Garamond;
            }
            
            @page {
                margin-top: 200pt;
                margin-right: 30pt;
                margin-bottom: 50pt;
                margin-left: 30pt;
                
                @top-right {
                    content: element(header);
                }
               
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                }

            }
        </style>
    </head>
    <body>
        <div id="header">
            Monsieur Jay LAPOISSE<br>
            13 avenue de la Chance<br>
            35911 Rennes
        </div>
        
        <div style="page-break-after: always;">First page</div>
        <div style="page-break-after: always;">Second page</div>
        <div>Last page</div>
    </body>
</html>

My Java

try {
    HtmlConverter.convertToPdf(new FileInputStream(new File(SRC)), new FileOutputStream(new File(DEST)));
} catch(Exception e) {
    e.printStackTrace(System.err);
}

My goal:

the goal

If I try to do as for the header, I don't arrive to have the page counter.

And if I do as my code above, I don't arrive to affect a style.


Solution

  • CSS has 16 page margin areas in total where you can put your content. Your use case can be achieved by using those areas easily with the following CSS code:

    @bottom-right {
        color: red;
        content: "Page " counter(page) " of " counter(pages);
    }
    @bottom-left {
        color: red;
        content: "[document title]";
    }
    

    Full HTML:

    <html>
    <head>
      <style>
        #header {
          position: running(header);
          text-align: left;
          margin-top: 50pt;
          margin-left: 320pt;
          font-family: Garamond;
        }
    
        @page {
          margin-top: 200pt;
          margin-right: 30pt;
          margin-bottom: 50pt;
          margin-left: 30pt;
    
          @top-right {
            content: element(header);
          }
    
          @bottom-right {
            color: red;
            content: "Page " counter(page) " of " counter(pages);
          }
          @bottom-left {
            color: red;
            content: "[document title]";
          }
    
        }
      </style>
    </head>
    <body>
    <div id="header">
      Monsieur Jay LAPOISSE<br>
      13 avenue de la Chance<br>
      35911 Rennes
    </div>
    
    <div style="page-break-after: always;">First page</div>
    <div style="page-break-after: always;">Second page</div>
    <div>Last page</div>
    </body>
    </html>
    

    Visual result after converting to PDF with pdfHTML 3.0.1:

    result