如何在 Android 应用程序中向 MS Word 文档添加水印

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

此技术提示解释了如何在 Android 应用程序中 向 Microsoft Word 文档中的文档添加水印 。有时您需要在 Word 文档中插入水印,例如,如果您想要打印草稿文档或将其标记为机密。在 Microsoft Word 中,您可以使用“插入水印”命令快速插入水印。使用此命令的人很少意识到这种“水印”只是一种形状,文本插入到页眉或页脚中并位于页面中央。虽然 Aspose.Words 没有像 Microsoft Word 那样的单一插入水印命令,但可以很容易地将任何形状或图像插入页眉或页脚,从而创建任何可以想象的类型的水印。

下面的代码将水印插入到 Word 文档中。

[Java 代码示例]


 package AddWatermark;

import java.awt.Color; import java.io.File; import java.net.URI;

import com.aspose.words.Document; import com.aspose.words.Shape; import com.aspose.words.ShapeType; import com.aspose.words.RelativeHorizontalPosition; import com.aspose.words.RelativeVerticalPosition; import com.aspose.words.WrapType; import com.aspose.words.VerticalAlignment; import com.aspose.words.HorizontalAlignment; import com.aspose.words.Paragraph; import com.aspose.words.Section; import com.aspose.words.HeaderFooterType; import com.aspose.words.HeaderFooter;

public class Program { public static void main(String[] args) throws Exception { // Sample infrastructure. URI exeDir = Program.class.getResource("").toURI(); String dataDir = new File(exeDir.resolve("../../Data")) + File.separator;

    Document doc = new Document(dataDir + "TestFile.doc");
    insertWatermarkText(doc, "CONFIDENTIAL");
    doc.save(dataDir + "TestFile Out.doc");
}

/**
 * Inserts a watermark into a document.
 *
 * @param doc The input document.
 * @param watermarkText Text of the watermark.
 */
private static void insertWatermarkText(Document doc, String watermarkText) throws Exception
{
    // Create a watermark shape. This will be a WordArt shape.
    // You are free to try other shape types as watermarks.
    Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

    // Set up the text of the watermark.
    watermark.getTextPath().setText(watermarkText);
    watermark.getTextPath().setFontFamily("Arial");
    watermark.setWidth(500);
    watermark.setHeight(100);
    // Text will be directed from the bottom-left to the top-right corner.
    watermark.setRotation(-40);
    // Remove the following two lines if you need a solid black text.
    watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
    watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

    // Place the watermark in the page center.
    watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    watermark.setWrapType(WrapType.NONE);
    watermark.setVerticalAlignment(VerticalAlignment.CENTER);
    watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

    // Create a new paragraph and append the watermark to this paragraph.
    Paragraph watermarkPara = new Paragraph(doc);
    watermarkPara.appendChild(watermark);

    // Insert the watermark into all headers of each document section.
    for (Section sect : doc.getSections())
    {
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
    }
}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception
{
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.getDocument(), headerType);
        sect.getHeadersFooters().add(header);
    }

    // Insert a clone of the watermark into the header.

    header.appendChild(watermarkPara.deepClone(true));

}

}


相关文章