当前位置:网站首页>C#/VB.NET to add more lines more columns image watermark into the Word document

C#/VB.NET to add more lines more columns image watermark into the Word document

2022-08-02 10:40:00 51CTO

在WordWhen adding a watermark inside,A common situation is to only support adding a watermark effect to the entire page;For the effect that needs to cover the entire page with the watermark, you can refer to the method in this article.This article introduces a more flexible way to add multiple rows and multiple columns(平铺)图片水印到Word文档.下面是详细方法及步骤.

环境配置

引入Word API-Free Spire.Doc for .NET( Spire.Doc.dll)

1. 通过 ​NuGet​引入dll(2种方法)的方法

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”.等待程序安装完成.

(2)将以下内容复制到PM控制台安装:

      
      
Install-PackageFreeSpire.Doc -Version 10.2
  • 1.

2. 手动添加dll引用的方法

可通过手动​ ​下载包​​到本地,然后解压,找到BIN文件夹下的Spire.Doc.dll.然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序.

Add multi-line and multi-column image watermark

Realize multi-row and multi-column image watermark effect,That is, add a picture to the header,And simulate the effect of spreading the watermark all over the page by duplicating the picture multiple times,Depending on the number of different pictures copied horizontally or vertically,The distribution effect of the watermark image on the page can be adjusted arbitrarily.以下,This is the main code step for implementing image watermarking:

  • 创建Document类的对象,并通过Document.LoadFromFile(string fileName)方法加载Word文档.
  • forLoop through all in the documentSection,并通过Document.Sections[int Index]Property gets the section in the page.
  • 使用HeadersFooters.Header属性获取页眉,并通过HeaderFooter.AddParagraph()方法添加段落到页眉.
  • 创建DocPicture类的对象,并调用DocPicture.LoadImage(string imgFile)method to load the watermarked image,以及通过TextWrappingStyleThe enumeration value sets the image wrapping method.
  • 通过for循环以DocPicture.Clone()The method duplicates the image multiple times,并通过DocPicture.VerticalPositionDocPicture.HorizontalPositionThe property sets the picture position arrangement.
  • 调用Paragraph.ChildObjects.Add(IDocumentObject entity)method to add an image to the header paragraph.
  • 最后,通过Document.SaveToFile(string fileName, FileFormat fileFormat)The methods to save the document all specify the path.

C#

      
      
using Spire. Doc;
using Spire. Doc. Documents;
using Spire. Doc. Fields;

namespace MultiLinePictureWatermark
{
class Program
{
static void Main( string[] args)
{
//加载Word文档
Document doc = new Document();
doc. LoadFromFile( "test.docx");

//遍历所有section
for ( int i = 0; i < doc. Sections. Count; i ++)
{
Section section = doc. Sections[ i];

//获取section的页眉
HeaderFooter header = section. HeadersFooters. Header;

//添加段落到页眉
Paragraph paragraph1 = header. AddParagraph();

//加载水印图片
DocPicture picture = new DocPicture( doc);
picture. LoadImage( "panda.png");
picture. TextWrappingStyle = TextWrappingStyle. Behind;

//复制图片
for ( int p = 0; p < 5; p ++)
{
for ( int q = 0; q < 3; q ++)
{
picture = ( DocPicture) picture. Clone();
picture. VerticalPosition = 50 + 150 * p;
picture. HorizontalPosition = 10 + 140 * q;
paragraph1. ChildObjects. Add( picture);
}
}
}

//保存文档
doc. SaveToFile( "MultiLinePictureWatermatk.docx", FileFormat. Docx2013);
}
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.

VB.NET

      
      
Imports Spire .Doc
Imports Spire .Doc .Documents
Imports Spire .Doc .Fields

Namespace MultiLinePictureWatermark
Class Program
Private Shared Sub Main( args As String())
'加载Word文档
Dim doc As New Document()
doc .LoadFromFile( "test.docx")

'遍历所有section
For i As Integer = 0 To doc .Sections .Count - 1
Dim section As Section = doc .Sections( i)

'获取section的页眉
Dim header As HeaderFooter = section .HeadersFooters .Header

'添加段落到页眉
Dim paragraph1 As Paragraph = header .AddParagraph()

'加载水印图片
Dim picture As New DocPicture( doc)
picture .LoadImage( "panda.png")
picture .TextWrappingStyle = TextWrappingStyle .Behind

'复制图片
For p As Integer = 0 To 4
For q As Integer = 0 To 2
picture = DirectCast( picture .Clone(), DocPicture)
picture .VerticalPosition = 50 + 150 * p
picture .HorizontalPosition = 10 + 140 * q
paragraph1 .ChildObjects .Add( picture)
Next
Next
Next

'保存文档
doc .SaveToFile( "MultiLinePictureWatermatk.docx", FileFormat .Docx2013)
End Sub
End Class
End Namespace
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

C#/VB.NET Add multi-line and multi-column image watermark toWord文档_.NET


—END—


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208021023420167.html

随机推荐