您好,
感谢您的咨询。
引起第一页没有成功添加页眉的原因:您的docx文档的页眉页脚设置了“不同的第一个页面”。这个设置将使第一页和其他页面的页眉页脚区分开来,需要单独为其设置。
页眉设置.png
针对您的文档,将所有页面设置相同的页眉页脚,解决方案如下:
先判断是否section页眉页脚设置了“不同的第一个页面”: section.getPageSetup().getDifferentFirstPageHeaderFooter()
如果返回true,说明设置了,就需要将其改为false: section.getPageSetup().setDifferentFirstPageHeaderFooter(false);
- Code: Select all
for (int ii = 0; ii < document.getSections().getCount(); ii++) {
Section section = document.getSections().get(ii);
if (section.getPageSetup().getDifferentFirstPageHeaderFooter()) {
section.getPageSetup().setDifferentFirstPageHeaderFooter(false);
HeaderFooter header = section.getHeadersFooters().getHeader();
DocPicture headerPicture = null;
headerPicture = header.addParagraph().appendPicture(image);
headerPicture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
headerPicture.setWidth(50f);
headerPicture.setHeight(50f);
Float width = section.getPageSetup().getClientWidth();
Float height = section.getPageSetup().getHeaderDistance();
headerPicture.setHorizontalPosition(-40f);
headerPicture.setVerticalPosition(28f);
headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);
headerPicture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
}else{
HeaderFooter header = section.getHeadersFooters().getHeader();
DocPicture headerPicture = null;
headerPicture = header.addParagraph().appendPicture(image);
headerPicture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
headerPicture.setWidth(50f);
headerPicture.setHeight(50f);
Float width = section.getPageSetup().getClientWidth();
Float height = section.getPageSetup().getHeaderDistance();
headerPicture.setHorizontalPosition(-40f);
headerPicture.setVerticalPosition(28f);
headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);
headerPicture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);
}}
Sincerely,
Amy
E-iceblue support team
Login to view the files attached to this post.