การทำงานกับลายน้ำ
หัวข้อนี้อธิบายวิธีการทำงานโดยทางโปรแกรมด้วยลายน้ำโดยใช้ Aspose.Words ลายน้ำคือภาพพื้นหลังที่แสดงอยู่ด้านหลังข้อความในเอกสาร ลายน้ำอาจมีข้อความหรือรูปภาพที่แสดงโดยคลาส Watermark
ลองออนไลน์
คุณสามารถลองใช้ฟังก์ชันนี้กับ ลายน้ำเอกสารออนไลน์ฟรี ของเราได้
เพิ่มลายน้ำให้กับเอกสาร
ใน Microsoft Word คุณสามารถแทรกลายน้ำในเอกสารได้อย่างง่ายดายโดยใช้คำสั่งแทรกลายน้ำ Aspose.Words จัดเตรียมคลาส watermark เพื่อเพิ่มหรือลบลายน้ำในเอกสาร Aspose.Words จัดให้มี ลายน้ำประเภทenumeration ซึ่งกำหนดประเภทลายน้ำที่เป็นไปได้สามประเภท (ข้อความ รูปภาพ และไม่มี) ที่จะใช้งานได้
เพิ่มลายน้ำข้อความ
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการแทรกลายน้ำข้อความในเอกสารโดยการกำหนด TextWatermarkOptions โดยใช้วิธี SetText:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Document.doc"); | |
TextWatermarkOptions options = new TextWatermarkOptions() | |
{ | |
FontFamily = "Arial", | |
FontSize = 36, | |
Color = Color.Black, | |
Layout = WatermarkLayout.Horizontal, | |
IsSemitrasparent = false | |
}; | |
doc.Watermark.SetText("Test", options); | |
doc.Save(dataDir + "AddTextWatermark_out.docx"); |
เพิ่มลายน้ำรูปภาพ
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการแทรกลายน้ำรูปภาพในเอกสารโดยการกำหนด ImageWatermarkOptions โดยใช้วิธี SetImage:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "Document.doc"); | |
ImageWatermarkOptions options = new ImageWatermarkOptions() | |
{ | |
Scale = 5, | |
IsWashout = false | |
}; | |
doc.Watermark.SetImage(Image.FromFile(dataDir + "Watermark.png"), options); | |
doc.Save(dataDir + "AddImageWatermark_out.docx"); |
ลายน้ำสามารถแทรกได้โดยใช้คลาสรูปร่างเช่นกัน มันง่ายมากที่จะแทรกรูปร่างหรือรูปภาพใด ๆ ลงในส่วนหัวหรือส่วนท้ายและสร้างลายน้ำประเภทใดก็ได้เท่าที่จะจินตนาการได้
ตัวอย่างรหัสต่อไปนี้แทรกลายน้ำลงในเอกสาร Word:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
[Test] | |
public void AddWatermark() | |
{ | |
Document doc = new Document(MyDir + "Document.docx"); | |
InsertWatermarkText(doc, "CONFIDENTIAL"); | |
doc.Save(ArtifactsDir + "WorkWithWatermark.AddWatermark.docx"); | |
} | |
/// <summary> | |
/// Inserts a watermark into a document. | |
/// </summary> | |
/// <param name="doc">The input document.</param> | |
/// <param name="watermarkText">Text of the watermark.</param> | |
private void InsertWatermarkText(Document doc, string watermarkText) | |
{ | |
// Create a watermark shape, this will be a WordArt shape. | |
Shape watermark = new Shape(doc, ShapeType.TextPlainText) { Name = "Watermark" }; | |
watermark.TextPath.Text = watermarkText; | |
watermark.TextPath.FontFamily = "Arial"; | |
watermark.Width = 500; | |
watermark.Height = 100; | |
// Text will be directed from the bottom-left to the top-right corner. | |
watermark.Rotation = -40; | |
// Remove the following two lines if you need a solid black text. | |
watermark.FillColor = Color.Gray; | |
watermark.StrokeColor = Color.Gray; | |
// Place the watermark in the page center. | |
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page; | |
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page; | |
watermark.WrapType = WrapType.None; | |
watermark.VerticalAlignment = VerticalAlignment.Center; | |
watermark.HorizontalAlignment = 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. | |
foreach (Section sect in doc.Sections) | |
{ | |
// There could be up to three different headers in each section. | |
// Since we want the watermark to appear on all pages, insert it into all headers. | |
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary); | |
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst); | |
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven); | |
} | |
} | |
private void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, | |
HeaderFooterType headerType) | |
{ | |
HeaderFooter header = sect.HeadersFooters[headerType]; | |
if (header == null) | |
{ | |
// There is no header of the specified type in the current section, so we need to create it. | |
header = new HeaderFooter(sect.Document, headerType); | |
sect.HeadersFooters.Add(header); | |
} | |
// Insert a clone of the watermark into the header. | |
header.AppendChild(watermarkPara.Clone(true)); | |
} |
ลบลายน้ำออกจากเอกสาร
คลาส Watermark จัดเตรียมวิธีการลบเพื่อลบลายน้ำออกจากเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบลายน้ำออกจากเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "AddTextWatermark_out.docx"); | |
if (doc.Watermark.Type == WatermarkType.Text) | |
{ | |
doc.Watermark.Remove(); | |
} | |
doc.Save(dataDir + "RemoveWatermark_out.docx"); |
หากเพิ่มลายน้ำโดยใช้วัตถุคลาส Shape หากต้องการลบลายน้ำออกจากเอกสาร คุณต้องตั้งชื่อรูปร่างลายน้ำเท่านั้นในระหว่างการแทรก จากนั้นจึงลบรูปร่างลายน้ำตามชื่อที่กำหนด
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งชื่อรูปร่างลายน้ำและลบออกจากเอกสาร:
// Set name to be able to remove it afterwards
watermark.Name("WaterMark");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithImages(); | |
string fileName = "RemoveWatermark.docx"; | |
Document doc = new Document(dataDir + fileName); | |
RemoveWatermarkText(doc); | |
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName); | |
doc.Save(dataDir); | |
} | |
private static void RemoveWatermarkText(Document doc) | |
{ | |
foreach (HeaderFooter hf in doc.GetChildNodes(NodeType.HeaderFooter, true)) | |
{ | |
foreach (Shape shape in hf.GetChildNodes(NodeType.Shape, true)) | |
{ | |
if (shape.Name.Contains("WaterMark")) | |
{ | |
shape.Remove(); | |
} | |
} | |
} | |
} | |
} |
เพิ่มลายน้ำลงในเซลล์ตาราง
บางครั้งคุณจำเป็นต้องแทรกลายน้ำ/รูปภาพลงในเซลล์ของตารางและแสดงไว้นอกตาราง คุณสามารถใช้คุณสมบัติ IsLayoutInCell ได้ คุณสมบัตินี้ได้รับหรือตั้งค่าสถานะที่ระบุว่ารูปร่างจะแสดงภายในตารางหรือภายนอก โปรดทราบว่าคุณสมบัตินี้ใช้งานได้เฉพาะเมื่อคุณปรับเอกสารให้เหมาะสมสำหรับ Microsoft Word 2010 โดยใช้วิธี OptimizeFor
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการใช้คุณสมบัตินี้:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + @"LayoutInCell.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape watermark = new Shape(doc, ShapeType.TextPlainText); | |
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page; | |
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page; | |
watermark.IsLayoutInCell = true; // Display the shape outside of table cell if it will be placed into a cell. | |
watermark.Width = 300; | |
watermark.Height = 70; | |
watermark.HorizontalAlignment = HorizontalAlignment.Center; | |
watermark.VerticalAlignment = VerticalAlignment.Center; | |
watermark.Rotation = -40; | |
watermark.Fill.Color = Color.Gray; | |
watermark.StrokeColor = Color.Gray; | |
watermark.TextPath.Text = "watermarkText"; | |
watermark.TextPath.FontFamily = "Arial"; | |
watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid()); | |
watermark.WrapType = WrapType.None; | |
Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run; | |
builder.MoveTo(run); | |
builder.InsertNode(watermark); | |
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010); | |
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx"; | |
// Save the document to disk. | |
doc.Save(dataDir); |