Code Monkey home page Code Monkey logo

Comments (10)

AlexNosk avatar AlexNosk commented on June 7, 2024

@chengshuanghub 您能否在此处附上您的输入和输出文件以供我们参考。 我们将检查问题并为您提供更多信息。
顺便说一句,最新版本的 Aspose.Words 提供了一种在文档中添加水印的内置方法:
https://docs.aspose.com/words/java/working-with-watermark/

from aspose.words-for-java.

chengshuanghub avatar chengshuanghub commented on June 7, 2024

@chengshuanghub 您能否在此处附上您的输入和输出文件以供我们参考。 我们将检查问题并为您提供更多信息。 顺便说一句,最新版本的 Aspose.Words 提供了一种在文档中添加水印的内置方法: https://docs.aspose.com/words/java/working-with-watermark/

麻烦您帮忙看看。

输入文件:
页眉测试.docx
输出文件:
test4.docx

from aspose.words-for-java.

AlexNosk avatar AlexNosk commented on June 7, 2024

@chengshuanghub 出现此问题的原因是您插入带有段落的水印。 请看下面的截图。 为避免该问题,您应该在现有段落中插入水印形状。
image
或者您可以简单地使用内置方法插入水印:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getWatermark().setText("11111111111111111111111");
doc.save("C:\\Temp\\out.docx");

out.docx

from aspose.words-for-java.

chengshuanghub avatar chengshuanghub commented on June 7, 2024

@chengshuanghub 出现此问题的原因是您插入带有段落的水印。 请看下面的截图。 为避免该问题,您应该在现有段落中插入水印形状。 image 或者您可以简单地使用内置方法插入水印:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getWatermark().setText("11111111111111111111111");
doc.save("C:\\Temp\\out.docx");

out.docx

您好,这个带有段落的水印不是我代码里加的,是加完水印后页眉就变成了2行,我添加的水印是不带段落的。内置方法插入好像设置不了位置,我们需要插入多个水印。

from aspose.words-for-java.

AlexNosk avatar AlexNosk commented on June 7, 2024

@chengshuanghub 在您的代码中,您为水印形状创建一个段落并将其附加到每个部分的每个标题中。 请参阅以下代码片段:

Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
for (Section sect : doc.getSections()) {
    insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
}

正如我提到的,如果标题已经包含一个段落,您应该在这个现有段落中插入一个水印形状,而不是创建一个单独的段落。

from aspose.words-for-java.

chengshuanghub avatar chengshuanghub commented on June 7, 2024

@chengshuanghub 在您的代码中,您为水印形状创建一个段落并将其附加到每个部分的每个标题中。 请参阅以下代码片段:

Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
for (Section sect : doc.getSections()) {
    insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
}

正如我提到的,如果标题已经包含一个段落,您应该在这个现有段落中插入一个水印形状,而不是创建一个单独的段落。

请问您有获取现有段落并插入水印的例子吗?

官网也是我这样写的
image

from aspose.words-for-java.

AlexNosk avatar AlexNosk commented on June 7, 2024

@chengshuanghub 您可以使用如下代码:

HeaderFooter hf = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if(hf!=null)
{
    hf.getFirstParagraph().appendChild(watermarkShape.deepClone(true));
}

from aspose.words-for-java.

chengshuanghub avatar chengshuanghub commented on June 7, 2024

@chengshuanghub 您可以使用如下代码:

HeaderFooter hf = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
if(hf!=null)
{
    hf.getFirstParagraph().appendChild(watermarkShape.deepClone(true));
}

@AlexNosk 您好,我替换成上面的代码后,程序报错。您能帮忙看看吗?
image

报错信息:
java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.
at com.aspose.words.CompositeNode.zzWwL(Unknown Source)
at com.aspose.words.CompositeNode.insertAfter(Unknown Source)
at com.aspose.words.CompositeNode.appendChild(Unknown Source)
at FileUtils.insertWatermarkIntoHeader(FileUtils.java:220)
at FileUtils.insertWatermarkText(FileUtils.java:180)
at FileUtils.setWordWaterMarkss(FileUtils.java:143)
at FileUtils.main(FileUtils.java:301)

from aspose.words-for-java.

AlexNosk avatar AlexNosk commented on June 7, 2024

@chengshuanghub 在您的代码中,您试图将一个段落插入另一个段落。 您应该插入水印形状,而不是水印段落。你应该插入这个节点

Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

from aspose.words-for-java.

chengshuanghub avatar chengshuanghub commented on June 7, 2024
watermarkShape

@AlexNosk 插入水印形状后好了,谢谢您的帮助

from aspose.words-for-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.