Java 如何在生成pdf时添加不同的页脚(flyingsaucer/itext)

Java 如何在生成pdf时添加不同的页脚(flyingsaucer/itext),java,css,itext,flying-saucer,Java,Css,Itext,Flying Saucer,我有一个HTML文件,我想转换成PDF格式。 我使用freemarker模板来描述HTML+CSS+自定义字体。 生成PDF后,PDF文件包含10页(页数是动态的) 我需要有不同的页脚为第一页,第四页,第八页 对于第一页,我按以下方式设置页脚: @page: first { @bottom-center { content: element(footer); } } 它起作用了 但是如何在第4页和第8页设置不同的页脚呢?这里还有一个困难,不同页脚的页数可以改变 我尝试

我有一个HTML文件,我想转换成PDF格式。 我使用freemarker模板来描述HTML+CSS+自定义字体。 生成PDF后,PDF文件包含10页(页数是动态的) 我需要有不同的页脚为第一页,第四页,第八页

对于第一页,我按以下方式设置页脚:

@page: first {
   @bottom-center {
       content: element(footer);
   }
}
它起作用了 但是如何在第4页和第8页设置不同的页脚呢?这里还有一个困难,不同页脚的页数可以改变

我尝试使用命名页面,如:

@page different_section: first {
   @bottom-center {
       content: element(footer);
   }
}
#different_section {page:different_section}
但这不起作用

也许有一种方法可以通过代码实现这一点?

这里有一个解决方案

对于包含具有特定类或id的元素的页面,页脚已更改

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        .newpage{page-break-before:always}

        @page {@bottom-center {content: element(footer)}}
        #footer {position: running(footer);}

        @page pagewithspecialfooter {@bottom-center {content: element(specialfooter)}}
        #specialfooter{position: running(specialfooter);}
        .page4and8  {page:pagewithspecialfooter;}
    </style>
</head>
<body>
<div id="footer">Default footer</div>
<div id="specialfooter">A different footer that appear on page 4 and 8</div>

<div>Page 1 content</div>
<div class="newpage">Page 2 content</div>
<div class="newpage">Page 3 content</div>
<div class="newpage page4and8">Page 4 content</div>
<div class="newpage">Page 5 content</div>
<div class="newpage">Page 6 content</div>
<div class="newpage">Page 7 content</div>
<div class="newpage page4and8">Page 8 content</div>
<div class="newpage">Page 9 content</div>
</body>
</html>

.newpage{之前的分页符:始终}
@页面{@底部中间{内容:元素(页脚)}
#页脚{位置:运行(页脚);}
@页面页面带有特殊页脚{@bottom center{content:element(specialfooter)}
#specialfooter{位置:运行(specialfooter);}
.page4和8{page:pagewithspecialfooter;}
默认页脚
出现在第4页和第8页上的不同页脚
第1页内容
第2页内容
第3页内容
第4页内容
第5页内容
第6页内容
第7页内容
第8页内容
第9页内容
这里有一个解决方案

对于包含具有特定类或id的元素的页面,页脚已更改

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        .newpage{page-break-before:always}

        @page {@bottom-center {content: element(footer)}}
        #footer {position: running(footer);}

        @page pagewithspecialfooter {@bottom-center {content: element(specialfooter)}}
        #specialfooter{position: running(specialfooter);}
        .page4and8  {page:pagewithspecialfooter;}
    </style>
</head>
<body>
<div id="footer">Default footer</div>
<div id="specialfooter">A different footer that appear on page 4 and 8</div>

<div>Page 1 content</div>
<div class="newpage">Page 2 content</div>
<div class="newpage">Page 3 content</div>
<div class="newpage page4and8">Page 4 content</div>
<div class="newpage">Page 5 content</div>
<div class="newpage">Page 6 content</div>
<div class="newpage">Page 7 content</div>
<div class="newpage page4and8">Page 8 content</div>
<div class="newpage">Page 9 content</div>
</body>
</html>

.newpage{之前的分页符:始终}
@页面{@底部中间{内容:元素(页脚)}
#页脚{位置:运行(页脚);}
@页面页面带有特殊页脚{@bottom center{content:element(specialfooter)}
#specialfooter{位置:运行(specialfooter);}
.page4和8{page:pagewithspecialfooter;}
默认页脚
出现在第4页和第8页上的不同页脚
第1页内容
第2页内容
第3页内容
第4页内容
第5页内容
第6页内容
第7页内容
第8页内容
第9页内容

但在这种情况下,它将为第4页和第7页创建相同的页脚,但我只需要在第4页创建页脚,第5、6、7页不应包含该页脚。特定的页脚将仅显示在存在其他类的地方。我已经更新了示例,使解决方案更加清晰。但在本例中,它将为第4页和第7页创建相同的页脚,但我只需要在第4页上创建,第5、6、7页不应包含该页脚。特定的页脚将仅显示在存在其他类的地方。我已经更新了示例,使解决方案更加清晰。