워터마크 작업
이 항목에서는 Aspose.Words를 사용하여 워터마크를 프로그래밍 방식으로 작업하는 방법에 대해 설명합니다. 워터마크는 문서의 텍스트 뒤에 표시되는 배경 이미지입니다. 워터마크에는 Watermark 클래스로 표시되는 텍스트나 이미지가 포함될 수 있습니다.
온라인으로 사용해 보세요
무료 온라인 문서 워터마크를 통해 이 기능을 사용해 볼 수 있습니다.
문서에 워터마크 추가
Microsoft Word에서는 워터마크 삽입 명령을 사용하여 문서에 워터마크를 쉽게 삽입할 수 있습니다. Aspose.Words는 문서에 워터마크를 추가하거나 제거하기 위한 watermark 클래스를 제공합니다. Aspose.Words는 작업할 수 있는 세 가지 유형의 워터마크(텍스트, 이미지 및 없음)를 정의하는 워터마크 유형enumeration을 제공합니다
텍스트 워터마크 추가
다음 코드 예제에서는 SetText 메서드를 사용하여 TextWatermarkOptions를 정의하여 문서에 텍스트 워터마크를 삽입하는 방법을 보여줍니다
// 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"); |
이미지 워터마크 추가
다음 코드 예제에서는 SetImage 메서드를 사용하여 ImageWatermarkOptions를 정의하여 문서에 이미지 워터마크를 삽입하는 방법을 보여줍니다
// 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 속성을 사용할 수 있습니다. 이 속성은 도형이 테이블 내부에 표시되는지 아니면 테이블 외부에 표시되는지를 나타내는 플래그를 가져오거나 설정합니다. 이 속성은 OptimizeFor 메서드를 사용하여 Microsoft Word 2010에 맞게 문서를 최적화하는 경우에만 작동합니다.
다음 코드 예제에서는 이 속성을 사용하는 방법을 보여줍니다
// 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); |