Java 給 Word 文檔每一頁(yè)添加不同圖片水印

?Word中設(shè)置水印時(shí),可加載圖片設(shè)置為水印效果,但通常添加水印效果時(shí),會(huì)對(duì)所有頁(yè)面都設(shè)置成統(tǒng)一效果,如果需要對(duì)每一頁(yè)或者某個(gè)頁(yè)面設(shè)置不同的水印效果,則可以參考本文中的方法。下面,將以Java代碼為例,對(duì)Word每一頁(yè)設(shè)置不同的圖片水印效果作詳細(xì)介紹。

方法思路

在給Word每一頁(yè)添加水印前,首先需要在Word文檔每一頁(yè)正文的最后一個(gè)字符后面插入“連續(xù)”分節(jié)符,然后在每一節(jié)的頁(yè)眉段落里添加水印圖片,并設(shè)置圖片的坐標(biāo)位置、對(duì)齊方式、襯與文字下方等。最后保存文檔。

Jar引入

在程序中引入?Free Spire.Doc for Java?中的Spire.Doc.jar文件(該文件在lib文件夾下);如果需要通過(guò)?Maven下載導(dǎo)入,

配置pom.xml:

<repositories>

? ? ? ? <repository>

? ? ? ? ? ? <id>com.e-iceblue</id>

? ? ? ? ? ? <url>https://repo.e-iceblue.cn/repository/maven-public/</url>

? ? ? ? </repository>

? ? </repositories>

<dependencies>

? ? <dependency>

? ? ? ? <groupId>e-iceblue</groupId>

? ? ? ? <artifactId>spire.doc.free</artifactId>

? ? ? ? <version>5.1.0</version>

? ? </dependency>

</dependencies>

Java代碼

給每頁(yè)添加圖片水印時(shí),可參考如下步驟:

創(chuàng)建Document類的對(duì)象,并通過(guò)Document.loadFromFile(String fileName)方法加載Word文檔。

通過(guò)Document.getSections().get(int index)方法獲取指定節(jié)。

通過(guò)Section.getHeadersFooters().getHeader()方法獲取頁(yè)眉,HeaderFooter.addParagraph()方法添加段落到頁(yè)眉。

通過(guò)Paragraph.appendPicture(String filePath)方法添加圖片到段落,DocPicture.setVerticalPosition(float value)方法設(shè)置水印圖片位置,DocPicture.setHorizontalAlignment(ShapeHorizontalAlignment value)方法設(shè)置圖片對(duì)齊方式。

最后,通過(guò)Document.saveToFile(String fileName, FileFormat fileFormat)方法保存文檔。

不同頁(yè)面中設(shè)置不一樣的圖片水印效果,只需要獲取該頁(yè)面對(duì)應(yīng)的節(jié),然后參考上述用到的方法來(lái)添加即可。

下面是完整的Java代碼示例:

import com.spire.doc.*;

import com.spire.doc.documents.Paragraph;

import com.spire.doc.documents.TextWrappingStyle;

import com.spire.doc.fields.DocPicture;

/**

* 說(shuō)明:word 添加水印

* 作者:FH Admin

* from fhadmin.cn

*/

public class DifferentImageWatermark {

? ? public static void main(String[] args) {

? ? ? ? //加載Word測(cè)試文檔

? ? ? ? Document doc = new Document();

? ? ? ? doc.loadFromFile("test.docx");

? ? ? ? //獲取文檔第一節(jié)

? ? ? ? Section section1 = doc.getSections().get(0);

? ? ? ? //定義水印圖片的縱向坐標(biāo)位置

? ? ? ? float y = (float) (section1.getPageSetup().getPageSize().getHeight()/3);

? ? ? ? //添加圖片水印1

? ? ? ? HeaderFooter header1 = section1.getHeadersFooters().getHeader();//獲取頁(yè)眉

? ? ? ? header1.getParagraphs().clear();//刪除原有頁(yè)眉格式的段落

? ? ? ? Paragraph para1= header1.addParagraph();//重新添加段落

? ? ? ? DocPicture pic1 = para1.appendPicture("logo1.png");//加載圖片

? ? ? ? pic1.setTextWrappingStyle(TextWrappingStyle.Behind);//圖片置于文字下方

? ? ? ? pic1.setVerticalPosition(y);

? ? ? ? pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);//設(shè)置圖片對(duì)齊方式

? ? ? ? //同理設(shè)置第二節(jié)頁(yè)眉中的圖片水印2

? ? ? ? Section section2 = doc.getSections().get(1);

? ? ? ? HeaderFooter header2 = section2.getHeadersFooters().getHeader();

? ? ? ? header2.getParagraphs().clear();

? ? ? ? Paragraph para2= header2.addParagraph();

? ? ? ? DocPicture pic2 = para2.appendPicture("logo2.png");

? ? ? ? pic2.setTextWrappingStyle(TextWrappingStyle.Behind);

? ? ? ? pic2.setVerticalPosition(y);

? ? ? ? pic2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);

? ? ? ? //同理設(shè)置第三節(jié)中的頁(yè)眉中的圖片水印3

? ? ? ? Section section3 = doc.getSections().get(2);

? ? ? ? HeaderFooter header3 = section3.getHeadersFooters().getHeader();

? ? ? ? header3.getParagraphs().clear();

? ? ? ? Paragraph para3= header3.addParagraph();

? ? ? ? DocPicture pic3 = para3.appendPicture("logo3.png");

? ? ? ? pic3.setTextWrappingStyle(TextWrappingStyle.Behind);

? ? ? ? pic3.setVerticalPosition(y);

? ? ? ? pic3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);

? ? ? ? //保存文檔

? ? ? ? doc.saveToFile("DifferentImageWatermark.docx",FileFormat.Docx_2013);

? ? ? ? doc.dispose();

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在日常生活中,我們經(jīng)常能看到許多廣告文件的字體后面印著公司的名稱或logo,也能在一些保密文件的正文后看到“機(jī)密”...
    Tina_Tang閱讀 1,612評(píng)論 0 1
  • 在Word文檔中,頁(yè)眉位于文檔中每個(gè)頁(yè)面的頂部區(qū)域,而頁(yè)腳則位于文檔中每個(gè)頁(yè)面的底部區(qū)域。它們都常用于顯示文檔的附...
    Tina_Tang閱讀 2,128評(píng)論 0 1
  • 在日常辦公中,PDF文件的使用越來(lái)越普遍。為了防止自己撰寫的文件內(nèi)容被抄襲或者被他人盜用,給PDF文件添加上水印是...
    Tina_Tang閱讀 1,012評(píng)論 0 0
  • 前言 在操作Word文檔時(shí),可以通過(guò)添加頁(yè)碼來(lái)使其條理清晰,以便于后期查看整理。通常來(lái)說(shuō),一個(gè)Word文檔包含了多...
    Tina_Tang閱讀 1,535評(píng)論 0 0
  • 在日常工作中有時(shí)為了防止文檔被盜用,我們經(jīng)常會(huì)給Word文檔添加各種不同的水印來(lái)宣示主權(quán),那么用極速寫作如何添加水...
    Hiten2018閱讀 616評(píng)論 0 1

友情鏈接更多精彩內(nèi)容