Lucrul cu filigran
Acest subiect discută despre cum să lucrezi programatic cu filigran folosind Aspose.Words. O filă de apă este o imagine de fundal care se afișează în spatele textului într-un document. Un filigran poate conține un text sau o imagine reprezentat de clasa Watermark.
Încercaţi online
Puteți încerca această funcționalitate cu Free online document watermark nostru.
Adaugă un filigran la un document
În Microsoft Word, un filigran poate fi inserat cu ușurință într-un document folosind comanda Insert Watermark. Aspose.Words oferă clasa watermark pentru a adăuga sau elimina filigran în documente. Aspose.Words oferă enumerarea WatermarkType care definește trei tipuri posibile de filigrane (Text, imagine și niciuna) pentru a lucra cu
Adaugă filigran de text
Exemplul de cod următor demonstrează cum să se inserieze un filigran text într-un document prin definirea TextWatermarkOptions utilizând metoda 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"); |
Adaugă filigran imagine
Exemplul următor de cod demonstrează cum se inserează un filigran cu imagine într-un document prin definirea ImageWatermarkOptions folosind metoda 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"); |
Poză de apă poate fi, de asemenea, inserată folosind clasa de formă ca bine. Este foarte uşor să inserţi orice formă sau imagine într-un antet sau subsol şi astfel creezi o filigrană de orice tip imaginabil.
Exemplul următor de cod introduce o filigrană într-un document 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)); | |
} |
Îndepărtați filigranul de pe un document
Clasa Watermark oferă metoda “remove” pentru îndepărtarea filigranului de pe un document.
Exemplul următor de cod arată cum să îndepărtaţi un filigran din documente:
// 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"); |
Dacă marca de apă este adăugată folosind obiectul Shape de clasă atunci pentru a elimina marca de apă dintr-un document trebuie să setați doar numele formei de marcă de apă în timpul inserării și apoi eliminați forma de marcă de apă cu un nume atribuit.
Exemplul de cod următor vă arată cum să stabiliți numele formei de filigran și să o eliminați din document:
// 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(); | |
} | |
} | |
} | |
} | |
} |
Adăugaţi o filigran într-o celulă de tabelă
Uneori trebuie să inserezi o apă de marcă/o imagine într-o celulă de tabel și afișați-l în afara tabelului, puteți utiliza proprietatea IsLayoutInCell. Această proprietate obține sau setează o steagă care indică dacă forma este afișată într-o tabelă sau în afara acestuia. Notă că această proprietate funcționează doar atunci când optimizați documentul pentru Microsoft Word 2010 folosind metoda OptimizeFor.
Exemplul de cod următor arată cum se foloseşte această proprietate:
// 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); |