[DevExpress]透過 .NET Document Automation 來將Word檔轉成PDF,並加入浮水印

[DevExpress]透過 .NET Document Automation 來將Word檔轉成PDF,並加入浮水印

環境:Visual Studio 2013, DevExpress v14.2

 

DevExpress除了很多好用的控制項,現在又有「.NET Document Automation」可以讓我們針對 Word 及 Excel 做一些操作。

以下介紹如何透過 .NET Document Automation 讀入 Word 檔,加入浮水印,再轉成PDF檔案。

首先建立 Console 應用程式,並加入以的下參考

System.Drawing
System.Windows.Forms
DevExpress.Data
DevExpress.Docs
DevExpress.Office.Core
DevExpress.RichEDit.Core

 

如果要將 Word 檔,轉出 PDF 檔案,如下,

//建立 RichEditDocumentServer
RichEditDocumentServer srv = new RichEditDocumentServer();
//讀進 word 檔案
srv.LoadDocument(@"d:\w.docx");
//建立要轉出的 pdf 檔案 
using(Stream stream = File.Create(@"d:\w.pdf")){
	srv.ExportToPdf(stream);
}

image

以上幾行Code就可以將Word檔轉成PDF檔。

 

再來加入圖片的浮水印,如下,

1.增加處理加入圖片浮水印的Method

/// <summary>
/// 加入圖片浮水印
/// </summary>
/// <param name="sessions"></param>
/// <param name="image"></param>
public static void SetImageWatermark(SectionCollection sessions, Image image)
{
	//旋轉角度
	const int RotationAngle = -45;
	Section section = sessions[0];
	SubDocument subDocument = section.BeginUpdateHeader();
	subDocument.Delete(subDocument.Range);
	Shape shape = subDocument.InsertPicture(subDocument.Range.Start, image);
	shape.RotationAngle = RotationAngle;
	shape.Offset = new PointF(section.Page.Width / 2 - shape.Size.Width / 2, section.Page.Height / 2 - shape.Size.Height / 2);
	section.EndUpdateHeader(subDocument);
}

 

2.準備圖片,呼叫 SetImageWatermark Method

//建立 RichEditDocumentServer
RichEditDocumentServer srv = new RichEditDocumentServer();
//讀進 word 檔案
srv.LoadDocument(@"d:\w.docx");
//讀入 Image
Image img = Image.FromFile(@"d:\preview.png");
// 呼叫 SetImageWatermark Method ,來產生 圖片浮水印
SetImageWatermark(srv.Document.Sections, img);
//建立要轉出的 pdf 檔案 
using(Stream stream = File.Create(@"d:\w.pdf")){
	srv.ExportToPdf(stream);
}

image

 

 

如果加入的浮水印要使用文字也是可以的哦!!!

1.增加處理加入文字浮水印的Method

/// <summary>
/// 加入文字浮水印
/// </summary>
/// <param name="sessions"></param>
/// <param name="text"></param>
public static void SetTextWatermark(SectionCollection sessions, string text)
{
	const int dpi = 120;
	//浮水印文字Size
	const int fontSize = 28;
	//旋轉角度
	const int RotationAngle = -45;
	Section section = sessions[0];
	SubDocument subDocument = section.BeginUpdateHeader();
	subDocument.Delete(subDocument.Range);
	Shape shape = subDocument.Shapes.InsertTextBox(subDocument.Range.Start);
	shape.TextBox.Document.AppendText(text);
	
	CharacterProperties cp = shape.TextBox.Document.BeginUpdateCharacters(shape.TextBox.Document.Range);
	cp.FontSize = fontSize;
	cp.ForeColor = Color.Red;
	Font measureFont = new Font(cp.FontName, cp.FontSize.Value);
	shape.TextBox.Document.EndUpdateCharacters(cp);

	shape.RotationAngle = RotationAngle;
	Size sizeInPixels = TextRenderer.MeasureText(text, measureFont) ;
	shape.Size = new SizeF(Units.PixelsToDocumentsF(sizeInPixels.Width, dpi), Units.PixelsToDocumentsF(sizeInPixels.Height, dpi));
	shape.Offset = new PointF(section.Page.Width / 2 - shape.Size.Width / 2, section.Page.Height / 2 - shape.Size.Height / 2);
	section.EndUpdateHeader(subDocument);
}

 

2.準備浮水印文字,呼叫 SetTextWatermark Method

//建立 RichEditDocumentServer
RichEditDocumentServer srv = new RichEditDocumentServer();
//讀進 word 檔案
srv.LoadDocument(@"d:\w.docx");
//準備浮水印文字
string watermarkString
    = string.Format("{0}@{1}", "亂馬客", DateTime.Now.ToString("yyyy/MM/dd H:mm:ss"));
// 呼叫 SetTextWatermark Method ,來產生 文字浮水印
SetTextWatermark(srv.Document.Sections, watermarkString);
//建立要轉出的 pdf 檔案 
using(Stream stream = File.Create(@"d:\w.pdf")){
	srv.ExportToPdf(stream);
}

image

 

 

 

參考資料

.NET Document Server Demo

.NET Document Server Demo 影片

How to add a watermark with a text or image to the document

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^