您好!
我使用的产品是Aspose.word for Java ,我需要在word文档正文中添加的页眉和水印,前8页是正常的,后面几页加不上页眉和水印,请问是怎么回事呢?
国家会议中心大酒店餐饮消防智能化管理系统服务合同(1).zip (95.4 KB)
您好!
我使用的产品是Aspose.word for Java ,我需要在word文档正文中添加的页眉和水印,前8页是正常的,后面几页加不上页眉和水印,请问是怎么回事呢?
国家会议中心大酒店餐饮消防智能化管理系统服务合同(1).zip (95.4 KB)
@SupportDhorde 您好!请分享您用于添加页眉和水印的代码。也请分享显示问题的Word文档。
以下是一些可能有帮助的文档链接:
您的 Word 文档有 3 个部分。 如果您设置 为第一部分设置 HeaderPrimary 页眉, 第 2 和第 3 部分 的页眉取自上一节。
请尝试此代码。
public class MainClass {
public static void main(String[] args) throws Exception {
License lic = new License();
lic.setLicense("Aspose.Words.Java.lic");
String contractSelfCode = "测试合[2022]00001号";
String fileLogo = "logo.png";
String fileWatermark = "watermark.png";
Document doc = new Document("测试.docx");
// remove all headers
// 删除所有页眉。
for (Section sect : doc.getSections()) {
HeaderFooter header;
header = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if (header != null)
header.remove();
header = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);
if (header != null)
header.remove();
header = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN);
if (header != null)
header.remove();
}
DocumentBuilder builder = new DocumentBuilder(doc);
// Set HeaderPrimary header for the first section.
// Sections on page 8 and page 11 do not have headers. In this case, headers are taken from the previous section.
// 为第一部分设置 HeaderPrimary 页眉。
// 第 8 页和第 11 页上的部分没有页眉。在这种情况下,页眉取自上一节。
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Paragraph paragraph = builder.insertParagraph();
builder.moveTo(paragraph);
// 插入logo
BufferedImage imageLogo = ImageIO.read(new File(fileLogo));
builder.insertImage(imageLogo, RelativeHorizontalPosition.MARGIN, 2, RelativeVerticalPosition.TOP_MARGIN,
44, 87, 27, WrapType.NONE);
// 插入合同编号
Run run = new Run(doc,"合同编号:" + contractSelfCode);
run.getFont().setBold(true);
run.getFont().setSize(11);
run.getFont().setName("宋体");
paragraph.appendChild(run);
paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
// 插入页眉分隔线
Border borderHeader = paragraph.getParagraphFormat().getBorders().getBottom();
borderHeader.setShadow(true);
borderHeader.setDistanceFromText(2);
borderHeader.setLineStyle(LineStyle.SINGLE);
// 插入水印
Shape shape = new Shape(doc, ShapeType.IMAGE);
BufferedImage imageWatermark = ImageIO.read(new File(fileWatermark));
shape.getImageData().setImage(imageWatermark);
// 设置倾斜度
shape.setRotation(0);
// 设置亮度
shape.getImageData().setBrightness(0.5);
// 设置对比度
shape.getImageData().setContrast(0.5);
shape.setWrapType(WrapType.NONE);
shape.setAnchorLocked(true);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setVerticalAlignment(VerticalAlignment.CENTER);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph watermarkPara = builder.insertParagraph();
watermarkPara.appendChild(shape);
watermarkPara.getParagraphBreakFont().setSize(1.0);
watermarkPara.getParagraphFormat().getBorders().clearFormatting();
doc.save("测试.headerswatermarks.docx");
}
测试.headerswatermarks.docx (110.8 KB)
插入页眉会默认增加一个换行符,导致页眉高度自动增高,这样就把正文内容挤压到了更下面,导致原本一页的内容变成两页,或者有的合同正文落款在底边,挤压后就把落款分割成两页了
Desktop.zip (59.1 KB)
您好@SupportDhorde。您可以移动,然后将合同编号插入页眉的第一段。
public static void main(String[] args) throws Exception {
License lic = new License();
lic.setLicense("Aspose.Words.Java.lic");
String contractSelfCode = "#123456789-0123456789";
Document doc = new Document("测试文档.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
//插入合同编号
builder.moveToParagraph(0, 0);
Run run = new Run(doc, contractSelfCode);
run.getFont().setBold(true);
run.getFont().setSize(11);
run.getFont().setName("宋体");
builder.getCurrentParagraph().appendChild(run);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
//插入页眉分隔线
Border borderHeader = builder.getParagraphFormat().getBorders().getBottom();
borderHeader.setShadow(true);
borderHeader.setDistanceFromText(2);
borderHeader.setLineStyle(LineStyle.SINGLE);
doc.save("测试文档.contractInHeader.docx");
}
测试文档.contractInHeader.docx (57.7 KB)
谢谢你之前分享的代码,经过测试,之前的文档这个问题解决了,但是当我测试下面这个文档的时候,又出现了同样的问题,插入页眉后,第一页的日期自动跳转到第二页了。
测试文档 (2).docx (29.7 KB)
微信图片_20220214134929.png (9.2 KB)
您好 @SupportDhorde,
我无法重现该问题。我正在使用相同的代码。请提供更多详细信息如何重现该问题。
public static void main(String[] args) throws Exception {
License lic = new License();
lic.setLicense("Aspose.Words.Java.lic");
String contractSelfCode = "合同编号:NZJT-ZF-22-MM-033";
Document doc = new Document("测试文档 (2).docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
//插入合同编号
builder.moveToParagraph(0, 0);
Run run = new Run(doc, contractSelfCode);
run.getFont().setBold(true);
run.getFont().setSize(11);
run.getFont().setName("宋体");
builder.getCurrentParagraph().appendChild(run);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
//插入页眉分隔线
Border borderHeader = builder.getParagraphFormat().getBorders().getBottom();
borderHeader.setShadow(true);
borderHeader.setDistanceFromText(2);
borderHeader.setLineStyle(LineStyle.SINGLE);
doc.save("测试文档 (2).contractInHeader.docx");
}
测试文档 (2).contractInHeader.docx (21.4 KB)