We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.word for Java 在word文档中 插入页眉多了换行符

Aspose.word for Java 在word文档中 插入页眉会自动增加一个 换行符,怎么删除这个换行符?

c7fdd2f9cd3284cc51c4db4867c62d0.png (13.4 KB)

@SupportDhorde

您可以通过使用属性 HeaderFooter.LastChild 获取它并使用 Node.Remove 方法将其删除来删除最后一个空段落或换行符。 希望这对你有帮助。

如果您仍然遇到问题,请压缩并附上您的输入和输出文件。 然后,我们将为您提供有关它的更多信息。

附件是示例文档 和JAVA测试代码
test.zip (120.3 KB)

请提供一下能删除 页眉中 换行符 的 示例JAVA代码

@SupportDhorde

请检查修改后的操作字头方法中的以下代码行以实现您的要求。

if(builder.getCurrentStory().getLastParagraph() !=null)
  builder.getCurrentStory().getLastParagraph().remove();
public static Document operateWordHeader(Document doc, String contractSelfCode){ 
	String fileLogo = "logo.png";
	String fileWatermark = "watermark.png";
	try {
    	DocumentBuilder builder = new DocumentBuilder(doc);
        builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
        
    	//插入logo
    	BufferedImage imageLogo = ImageIO.read(new File(fileLogo));
    	builder.insertImage(imageLogo, RelativeHorizontalPosition.MARGIN, 2, RelativeVerticalPosition.TOP_MARGIN, 44, 87, 27, WrapType.NONE);
    	
    	//插入合同编号
    	Paragraph paragraph = builder.insertParagraph();
    	Run run = new Run(doc,"合同编号:"+contractSelfCode);
    	run.getFont().setBold(true);
    	run.getFont().setSize(11);
    	run.getFont().setName("宋体");
    	paragraph.appendChild(run);
    	builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
    	
    	
    	//插入页眉分隔线
    	Border borderHeader = builder.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 = new Paragraph(doc);
        watermarkPara.appendChild(shape);
        for (Section sect : doc.getSections()) {
        	insertWatermarkImgIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        }
    	
		if(builder.getCurrentStory().getLastParagraph() !=null)
		  builder.getCurrentStory().getLastParagraph().remove();
	} catch (Exception e) {
		e.printStackTrace();
	}
    return doc;
}

你好 ,谢谢您的回复。

根据您提供的方法,我们做了测试,用了这方法

if(builder.getCurrentStory().getLastParagraph() !=null)
builder.getCurrentStory().getLastParagraph().remove();

word文档中页眉下面的换行符已经删除了,但是这个操作同时把合同中正文里面的水印也一起删除了,请问这块该如何处理呢?

@SupportDhorde

在这种情况下,您可以使用以下代码片段来避免共享问题。

if(builder.getCurrentStory().getLastParagraph() !=null 
		&& builder.getCurrentStory().getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0
		&& !builder.getCurrentStory().getLastParagraph().hasChildNodes())
  builder.getCurrentStory().getLastParagraph().remove();

正文水印出来后,但是 页眉又有了 换行符
f3dd00fc340f61c5e04cba3380fc7d3.png (61.5 KB)
2c9213d89c5d48ad977432b4b7cf7d3.png (10.8 KB)

@SupportDhorde

您正面临 Aspose.Words 的预期行为。 在您的场景中,您正在为文本创建新段落,为水印创建新段落。 水印段落是您要删除的最后一个段落。

在您的情况下,我们建议您将 watmark 段落的大小减小到 1.0,如下所示。 希望这对您有所帮助。

watermarkPara.getParagraphBreakFont().setSize(1.0);
public static Document operateWordHeader(Document doc, String contractSelfCode) {
	String fileLogo = "C:\\Users\\temp\\logo.png";
	String fileWatermark = "C:\\Users\\temp\\watermark.png";
	try {
		DocumentBuilder builder = new DocumentBuilder(doc);
		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);
		builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);

		// 插入页眉分隔线
		Border borderHeader = builder.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 = new Paragraph(doc);
		watermarkPara.appendChild(shape);
		watermarkPara.getParagraphBreakFont().setSize(1.0);
		for (Section sect : doc.getSections()) {
			insertWatermarkImgIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
		}

	} catch (Exception e) {
		e.printStackTrace();
	}
	return doc;
}