การทำงานกับลายน้ำ
หัวข้อนี้อธิบายวิธีการทำงานโดยทางโปรแกรมด้วยลายน้ำโดยใช้ Aspose.Words ลายน้ำคือภาพพื้นหลังที่แสดงอยู่ด้านหลังข้อความในเอกสาร ลายน้ำอาจมีข้อความหรือรูปภาพที่แสดงโดยคลาส Watermark
ลองออนไลน์
คุณสามารถลองใช้ฟังก์ชันนี้กับ ลายน้ำเอกสารออนไลน์ฟรี ของเราได้
วิธีเพิ่มลายน้ำให้กับเอกสาร
ใน Microsoft Word คุณสามารถแทรกลายน้ำในเอกสารได้อย่างง่ายดายโดยใช้คำสั่งแทรกลายน้ำ Aspose.Words จัดเตรียมคลาส Watermark เพื่อเพิ่มหรือลบลายน้ำในเอกสาร Aspose.Words จัดให้มีการแจงนับ WatermarkType ซึ่งกำหนดลายน้ำที่เป็นไปได้สามประเภท (TEXT, IMAGE และ NONE) ที่จะใช้งาน
เพิ่มลายน้ำข้อความ
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการแทรกลายน้ำข้อความในเอกสารโดยการกำหนด TextWatermarkOptions โดยใช้วิธี set_text
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
options = aw.TextWatermarkOptions() | |
options.font_family = "Arial" | |
options.font_size = 36 | |
options.color = drawing.Color.black | |
options.layout = aw.WatermarkLayout.HORIZONTAL | |
options.is_semitrasparent = False | |
doc.watermark.set_text("Test", options) | |
doc.save(docs_base.artifacts_dir + "WorkWithWatermark.add_text_watermark_with_specific_options.docx") |
เพิ่มลายน้ำรูปภาพ
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการแทรกลายน้ำรูปภาพในเอกสารโดยการกำหนด ImageWatermarkOptions โดยใช้วิธี set_image:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
options = aw.ImageWatermarkOptions() | |
options.scale = 5 | |
options.is_washout = False | |
doc.watermark.set_image(docs_base.images_dir + "Transparent background logo.png", options) | |
doc.save(docs_base.artifacts_dir + "WorkWithWatermark.add_image_watermark.docx") |
ลายน้ำสามารถแทรกได้โดยใช้คลาสรูปร่างเช่นกัน มันง่ายมากที่จะแทรกรูปร่างหรือรูปภาพใด ๆ ลงในส่วนหัวหรือส่วนท้ายและสร้างลายน้ำประเภทใดก็ได้เท่าที่จะจินตนาการได้
ตัวอย่างรหัสต่อไปนี้แทรกลายน้ำลงในเอกสาร Word:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
def test_add_and_remove_watermark(self) : | |
doc = aw.Document(docs_base.my_dir + "Document.docx") | |
self.insert_watermark_text(doc, "CONFIDENTIAL") | |
doc.save(docs_base.artifacts_dir + "TestFile.watermark.docx") | |
self.remove_watermark_text(doc) | |
doc.save(docs_base.artifacts_dir + "WorkWithWatermark.remove_watermark.docx") | |
# <summary> | |
# Inserts a watermark into a document. | |
# </summary> | |
# <param name="doc">The input document.</param> | |
# <param name="watermarkText">Text of the watermark.</param> | |
def insert_watermark_text(self, doc : aw.Document, watermarkText : str) : | |
# Create a watermark shape, this will be a WordArt shape. | |
watermark = aw.drawing.Shape(doc, aw.drawing.ShapeType.TEXT_PLAIN_TEXT) | |
watermark.name = "Watermark" | |
watermark.text_path.text = watermarkText | |
watermark.text_path.font_family = "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.fill_color = drawing.Color.gray | |
watermark.stroke_color = drawing.Color.gray | |
# Place the watermark in the page center. | |
watermark.relative_horizontal_position = aw.drawing.RelativeHorizontalPosition.PAGE | |
watermark.relative_vertical_position = aw.drawing.RelativeVerticalPosition.PAGE | |
watermark.wrap_type = aw.drawing.WrapType.NONE | |
watermark.vertical_alignment = aw.drawing.VerticalAlignment.CENTER | |
watermark.horizontal_alignment = aw.drawing.HorizontalAlignment.CENTER | |
# Create a new paragraph and append the watermark to this paragraph. | |
watermarkPara = aw.Paragraph(doc) | |
watermarkPara.append_child(watermark) | |
# Insert the watermark into all headers of each document section. | |
for sect in doc.sections : | |
sect = sect.as_section() | |
# 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. | |
self.insert_watermark_into_header(watermarkPara, sect, aw.HeaderFooterType.HEADER_PRIMARY) | |
self.insert_watermark_into_header(watermarkPara, sect, aw.HeaderFooterType.HEADER_FIRST) | |
self.insert_watermark_into_header(watermarkPara, sect, aw.HeaderFooterType.HEADER_EVEN) | |
def insert_watermark_into_header(self, watermarkPara : aw.Paragraph, sect : aw.Section, headerType : aw.HeaderFooterType) : | |
header = sect.headers_footers.get_by_header_footer_type(headerType) | |
if (header == None) : | |
# There is no header of the specified type in the current section, so we need to create it. | |
header = aw.HeaderFooter(sect.document, headerType) | |
sect.headers_footers.add(header) | |
# Insert a clone of the watermark into the header. | |
header.append_child(watermarkPara.clone(True)) | |
ลบลายน้ำออกจากเอกสาร
คลาส Watermark จัดเตรียมวิธีการลบเพื่อลบลายน้ำออกจากเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการลบลายน้ำออกจากเอกสาร:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
# Add a plain text watermark. | |
doc.watermark.set_text("Aspose Watermark") | |
# If we wish to edit the text formatting using it as a watermark, | |
# we can do so by passing a TextWatermarkOptions object when creating the watermark. | |
textWatermarkOptions = aw.TextWatermarkOptions() | |
textWatermarkOptions.font_family = "Arial" | |
textWatermarkOptions.font_size = 36 | |
textWatermarkOptions.color = drawing.Color.black | |
textWatermarkOptions.layout = aw.WatermarkLayout.DIAGONAL | |
textWatermarkOptions.is_semitrasparent = False | |
doc.watermark.set_text("Aspose Watermark", textWatermarkOptions) | |
doc.save(docs_base.artifacts_dir + "Document.text_watermark.docx") | |
# We can remove a watermark from a document like this. | |
if (doc.watermark.type == aw.WatermarkType.TEXT) : | |
doc.watermark.remove() | |
doc.save(docs_base.artifacts_dir + "WorkWithWatermark.remove_watermark_from_document.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-Python-via-.NET | |
def remove_watermark_text(self, doc : aw.Document) : | |
for hf in doc.get_child_nodes(aw.NodeType.HEADER_FOOTER, True) : | |
hf = hf.as_header_footer() | |
for shape in hf.get_child_nodes(aw.NodeType.SHAPE, True) : | |
shape = shape.as_shape() | |
if shape.name.find("WaterMark") >= 0 : | |
shape.remove() | |
เพิ่มลายน้ำในเซลล์ตาราง
บางครั้งคุณจำเป็นต้องแทรกลายน้ำ/รูปภาพลงในเซลล์ของตารางและแสดงไว้นอกตาราง คุณสามารถใช้คุณสมบัติ is_layout_in_cell ได้ คุณสมบัตินี้ได้รับหรือตั้งค่าสถานะที่ระบุว่ารูปร่างจะแสดงภายในตารางหรือภายนอก โปรดทราบว่าคุณสมบัตินี้ใช้งานได้เฉพาะเมื่อคุณปรับเอกสารให้เหมาะสมสำหรับ Microsoft Word 2010 โดยใช้วิธี optimize_for
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการใช้คุณสมบัตินี้:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.start_table() | |
builder.row_format.height = 100 | |
builder.row_format.height_rule = aw.HeightRule.EXACTLY | |
for i in range(0, 31) : | |
if (i != 0 and i % 7 == 0) : | |
builder.end_row() | |
builder.insert_cell() | |
builder.write("Cell contents") | |
builder.end_table() | |
watermark = aw.drawing.Shape(doc, aw.drawing.ShapeType.TEXT_PLAIN_TEXT) | |
watermark.relative_horizontal_position = aw.drawing.RelativeHorizontalPosition.PAGE | |
watermark.relative_vertical_position = aw.drawing.RelativeVerticalPosition.PAGE | |
watermark.is_layout_in_cell = True # Display the shape outside of the table cell if it will be placed into a cell. | |
watermark.width = 300 | |
watermark.height = 70 | |
watermark.horizontal_alignment = aw.drawing.HorizontalAlignment.CENTER | |
watermark.vertical_alignment = aw.drawing.VerticalAlignment.CENTER | |
watermark.rotation = -40 | |
watermark.fill_color = drawing.Color.gray | |
watermark.stroke_color = drawing.Color.gray | |
watermark.text_path.text = "watermarkText" | |
watermark.text_path.font_family = "Arial" | |
watermark.name = "WaterMark_" + str(uuid.uuid4()) | |
watermark.wrap_type = aw.drawing.WrapType.NONE | |
run = doc.get_child_nodes(aw.NodeType.RUN, True)[doc.get_child_nodes(aw.NodeType.RUN, True).count - 1].as_run() | |
builder.move_to(run) | |
builder.insert_node(watermark) | |
doc.compatibility_options.optimize_for(aw.settings.MsWordVersion.WORD2010) | |
doc.save(docs_base.artifacts_dir + "WorkingWithShapes.layout_in_cell.docx") |