کار با واترمارک
این موضوع در مورد چگونگی کار برنامه نویسی با watermark با استفاده از Aspose.Words بحث می کند. علامت آبی یک تصویر پس زمینه است که در پشت متن در یک سند نمایش داده می شود. یک علامت آبی می تواند حاوی یک متن یا یک تصویر باشد که توسط کلاس Watermark نشان داده شده است.
یک علامت آبی به یک سند اضافه کنید
در Microsoft Word، یک علامت آبی را می توان به راحتی در یک سند با استفاده از دستور Insert Watermark وارد کرد. Aspose.Words کلاس watermark را برای اضافه کردن یا حذف علامت آبی در اسناد فراهم می کند. Aspose.Words شمارش WatermarkType را فراهم می کند که سه نوع ممکن از علامت های آبی (متن، تصویر و هیچ) را برای کار با آن تعریف می کند.
اضافه کردن علامت آبی متن
مثال کد زیر نشان می دهد که چگونه یک علامت آبی متن را در یک سند با تعریف TextWatermarkOptions با استفاده از روش SetText وارد کنید.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(inputDataDir + u"Document.doc"); | |
auto options = System::MakeObject<TextWatermarkOptions>(); | |
options->set_FontFamily(u"Arial"); | |
options->set_FontSize(36); | |
options->set_Color(System::Drawing::Color::get_Black()); | |
options->set_Layout(WatermarkLayout::Horizontal); | |
options->set_IsSemitrasparent(false); | |
doc->get_Watermark()->SetText(u"Test", options); | |
auto outputPath = outputDataDir + u"AddTextWatermark.docx"; | |
doc->Save(outputPath); |
علامت آب تصویر را اضافه کنید
مثال کد زیر نشان می دهد که چگونه یک علامت آبی تصویر را در یک سند با تعریف ImageWatermarkOptions با استفاده از روش SetImage وارد کنید.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(inputDataDir + u"Document.doc"); | |
auto options = System::MakeObject<ImageWatermarkOptions>(); | |
options->set_Scale(5); | |
options->set_IsWashout(false); | |
doc->get_Watermark()->SetImage(System::Drawing::Image::FromFile(inputDataDir + u"Watermark.png"), options); | |
auto outputPath = outputDataDir + u"AddImageWatermark.docx"; | |
doc->Save(outputPath); |
علامت آبی را می توان با استفاده از کلاس شکل نیز وارد کرد. بسیار آسان است که هر شکل یا تصویر را در یک سر یا پای صفحه قرار دهید و بنابراین یک علامت آبی از هر نوع قابل تصور ایجاد کنید. مثال کد زیر یک علامت آبی را در یک سند ورد قرار می دهد.
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
namespace | |
{ | |
void InsertWatermarkIntoHeader(const System::SharedPtr<Paragraph>& watermarkPara, const System::SharedPtr<Section>& sect, HeaderFooterType headerType) | |
{ | |
System::SharedPtr<HeaderFooter> header = sect->get_HeadersFooters()->idx_get(headerType); | |
if (header == nullptr) | |
{ | |
// There is no header of the specified type in the current section, create it. | |
header = System::MakeObject<HeaderFooter>(sect->get_Document(), headerType); | |
sect->get_HeadersFooters()->Add(header); | |
} | |
// Insert a clone of the watermark into the header. | |
header->AppendChild((System::StaticCast<Node>(watermarkPara))->Clone(true)); | |
} | |
void InsertWatermarkText(const System::SharedPtr<Document>& doc, const System::String& watermarkText) | |
{ | |
// Create a watermark shape. This will be a WordArt shape. | |
// You are free to try other shape types as watermarks. | |
System::SharedPtr<Shape> watermark = System::MakeObject<Shape>(doc, ShapeType::TextPlainText); | |
watermark->set_Name(u"WaterMark"); | |
// Set up the text of the watermark. | |
watermark->get_TextPath()->set_Text(watermarkText); | |
watermark->get_TextPath()->set_FontFamily(u"Arial"); | |
watermark->set_Width(500); | |
watermark->set_Height(100); | |
// Text will be directed from the bottom-left to the top-right corner. | |
watermark->set_Rotation(-40); | |
// Remove the following two lines if you need a solid black text. | |
watermark->get_Fill()->set_Color(System::Drawing::Color::get_Gray()); | |
// Try LightGray to get more Word-style watermark | |
watermark->set_StrokeColor(System::Drawing::Color::get_Gray()); | |
// Try LightGray to get more Word-style watermark | |
// Place the watermark in the page center. | |
watermark->set_RelativeHorizontalPosition(RelativeHorizontalPosition::Page); | |
watermark->set_RelativeVerticalPosition(RelativeVerticalPosition::Page); | |
watermark->set_WrapType(WrapType::None); | |
watermark->set_VerticalAlignment(VerticalAlignment::Center); | |
watermark->set_HorizontalAlignment(HorizontalAlignment::Center); | |
// Create a new paragraph and append the watermark to this paragraph. | |
System::SharedPtr<Paragraph> watermarkPara = System::MakeObject<Paragraph>(doc); | |
watermarkPara->AppendChild(watermark); | |
// Insert the watermark into all headers of each document section. | |
for (System::SharedPtr<Node> sectionNode : System::IterateOver(doc->get_Sections())) | |
{ | |
System::SharedPtr<Section> sect = System::DynamicCast<Section>(sectionNode); | |
// There could be up to three different headers in each section, since we want | |
// The watermark to appear on all pages, insert into all headers. | |
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType::HeaderPrimary); | |
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType::HeaderFirst); | |
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType::HeaderEven); | |
} | |
} | |
} | |
void AddWatermark() | |
{ | |
std::cout << "AddWatermark example started." << std::endl; | |
// The path to the documents directories. | |
System::String inputDataDir = GetInputDataDir_WorkingWithImages(); | |
System::String outputDataDir = GetOutputDataDir_WorkingWithImages(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.Watermark.doc"); | |
InsertWatermarkText(doc, u"CONFIDENTIAL"); | |
System::String outputPath = outputDataDir + u"AddWatermark.doc"; | |
doc->Save(outputPath); | |
std::cout << "Added watermark to the document successfully." << std::endl << "File saved at " << outputPath.ToUtf8String() << std::endl; | |
std::cout << "AddWatermark example finished." << std::endl << std::endl; | |
} |
علامت آب را از یک سند حذف کنید
کلاس Watermark روش حذف را برای حذف علامت آبی از یک سند فراهم می کند.
مثال کد زیر نشان می دهد که چگونه یک علامت آبی را از اسناد حذف کنیم:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
auto doc = System::MakeObject<Document>(inputDataDir + u"TextWatermark.docx"); | |
if (doc->get_Watermark()->get_Type() == WatermarkType::Text) | |
{ | |
doc->get_Watermark()->Remove(); | |
} | |
auto outputPath = outputDataDir + u"RemoveWatermark.docx"; | |
doc->Save(outputPath); |
اگر علامت های آبی با استفاده از شیء کلاس Shape اضافه شوند، برای حذف علامت آبی از یک سند باید فقط نام شکل علامت آبی را در هنگام قرار دادن تنظیم کنید و سپس شکل علامت آبی را با یک نام اختصاص داده شده حذف کنید.
مثال کد زیر به شما نشان می دهد که چگونه نام شکل علامت آبی را تنظیم کنید و آن را از سند حذف کنید:
// Set name to be able to remove it afterwards
watermark->set_Name(u"WaterMark");
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
namespace | |
{ | |
void RemoveWatermarkText(const System::SharedPtr<Document>& doc) | |
{ | |
System::SharedPtr<NodeCollection> headerFooterNodes = doc->GetChildNodes(NodeType::HeaderFooter, true); | |
for (System::SharedPtr<HeaderFooter> hf : System::IterateOver<System::SharedPtr<HeaderFooter>>(headerFooterNodes)) | |
{ | |
System::SharedPtr<NodeCollection> shapeNodes = hf->GetChildNodes(NodeType::Shape, true); | |
for (System::SharedPtr<Shape> shape: System::IterateOver<System::SharedPtr<Shape>>(shapeNodes)) | |
{ | |
if (shape->get_Name().Contains(u"WaterMark")) | |
{ | |
shape->Remove(); | |
} | |
} | |
} | |
} | |
} | |
void RemoveWatermark() | |
{ | |
std::cout << "RemoveWatermark example started." << std::endl; | |
// The path to the documents directories. | |
System::String inputDataDir = GetInputDataDir_WorkingWithImages(); | |
System::String outputDataDir = GetOutputDataDir_WorkingWithImages(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"RemoveWatermark.docx"); | |
RemoveWatermarkText(doc); | |
System::String outputPath = outputDataDir + u"RemoveWatermark.docx"; | |
doc->Save(outputPath); | |
std:: cout << "File saved at " << outputPath.ToUtf8String() << std::endl; | |
std::cout << "RemoveWatermark example finished." << std::endl << std::endl; | |
} |
یک علامت آبی در سلول جدول اضافه کنید
گاهی اوقات شما باید یک علامت/تصویر را در سلول جدول قرار دهید و آن را در خارج از جدول نمایش دهید، می توانید از ویژگی IsLayoutInCell استفاده کنید. این ویژگی یک پرچم را دریافت یا تنظیم می کند که نشان می دهد آیا شکل در داخل یک جدول یا خارج از آن نمایش داده می شود. توجه داشته باشید که این ویژگی تنها زمانی کار می کند که شما سند را برای Microsoft Word 2010 با استفاده از روش OptimizeFor بهینه سازی کنید.
مثال کد زیر نشان می دهد که چگونه از این ویژگی استفاده کنید:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"LayoutInCell.docx"); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
System::SharedPtr<Shape> watermark = System::MakeObject<Shape>(doc, ShapeType::TextPlainText); | |
watermark->set_RelativeHorizontalPosition(RelativeHorizontalPosition::Page); | |
watermark->set_RelativeVerticalPosition(RelativeVerticalPosition::Page); | |
watermark->set_IsLayoutInCell(true); | |
// Display the shape outside of table cell if it will be placed into a cell. | |
watermark->set_Width(300); | |
watermark->set_Height(70); | |
watermark->set_HorizontalAlignment(HorizontalAlignment::Center); | |
watermark->set_VerticalAlignment(VerticalAlignment::Center); | |
watermark->set_Rotation(-40); | |
watermark->get_Fill()->set_Color(System::Drawing::Color::get_Gray()); | |
watermark->set_StrokeColor(System::Drawing::Color::get_Gray()); | |
watermark->get_TextPath()->set_Text(u"watermarkText"); | |
watermark->get_TextPath()->set_FontFamily(u"Arial"); | |
watermark->set_Name(System::String::Format(u"WaterMark_{0}",System::Guid::NewGuid())); | |
watermark->set_WrapType(WrapType::None); | |
System::SharedPtr<Run> run = System::DynamicCast_noexcept<Run>(doc->GetChildNodes(NodeType::Run, true)->idx_get(doc->GetChildNodes(NodeType::Run, true)->get_Count() - 1)); | |
builder->MoveTo(run); | |
builder->InsertNode(watermark); | |
doc->get_CompatibilityOptions()->OptimizeFor(MsWordVersion::Word2010); | |
System::String outputPath = outputDataDir + u"WorkingWithShapes.SetShapeLayoutInCell.docx"; | |
// Save the document to disk. | |
doc->Save(outputPath); |