การทำงานกับรูปภาพ

Aspose.Words ช่วยให้ผู้ใช้สามารถทำงานกับรูปภาพได้อย่างยืดหยุ่น ในบทความนี้ คุณสามารถสำรวจความเป็นไปได้บางประการในการทำงานกับรูปภาพเท่านั้น

การแทรกรูปภาพ

DocumentBuilder มีวิธีการ insert_image มากเกินไปซึ่งช่วยให้คุณสามารถแทรกรูปภาพแบบอินไลน์หรือแบบลอยได้ หากรูปภาพเป็นเมตาไฟล์ EMF หรือ WMF รูปภาพนั้นจะถูกแทรกลงในเอกสารในรูปแบบเมตาไฟล์ รูปภาพอื่นๆ ทั้งหมดจะถูกจัดเก็บในรูปแบบ PNG วิธี แทรก_รูปภาพ สามารถใช้รูปภาพจากแหล่งต่างๆ ได้:

  • จากไฟล์หรือ URL โดยส่งพารามิเตอร์สตริง
  • จากสตรีมโดยส่งพารามิเตอร์ Stream
  • จากอาร์เรย์ไบต์โดยการส่งผ่านพารามิเตอร์อาร์เรย์ไบต์

สำหรับแต่ละวิธีของ แทรก_รูปภาพ มีการโอเวอร์โหลดเพิ่มเติมซึ่งทำให้คุณสามารถแทรกรูปภาพด้วยตัวเลือกต่อไปนี้:

  • อินไลน์หรือลอยอยู่ในตำแหน่งเฉพาะ เช่น แทรก_รูปภาพ
  • ระดับเปอร์เซ็นต์หรือขนาดที่กำหนดเอง นอกจากนี้ วิธีการ DocumentBuilder.insert_image ยังส่งคืนออบเจ็กต์ Shape ที่เพิ่งสร้างและแทรก เพื่อให้คุณสามารถแก้ไขคุณสมบัติของ Shape เพิ่มเติมได้

การแทรกรูปภาพอินไลน์

ส่งสตริงเดียวที่แสดงไฟล์ที่มีรูปภาพไปที่ insert_image เพื่อแทรกรูปภาพลงในเอกสารเป็นกราฟิกอินไลน์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกรูปภาพอินไลน์ที่ตำแหน่งเคอร์เซอร์ลงในเอกสาร:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

builder.insert_image(docs_base.images_dir + "Logo.jpg")

doc.save(docs_base.artifacts_dir+"WorkingWithImages.document_builder_insert_inline_image.doc")

การแทรกรูปภาพแบบลอย (ตำแหน่งที่แน่นอน)

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกรูปภาพแบบลอยจากไฟล์หรือ URL ในตำแหน่งและขนาดที่ระบุ:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

builder.insert_image(docs_base.images_dir + "Logo.jpg",
    aw.drawing.RelativeHorizontalPosition.MARGIN,
    100,
    aw.drawing.RelativeVerticalPosition.MARGIN,
    100,
    200,
    100,
    aw.drawing.WrapType.SQUARE)

doc.save(docs_base.artifacts_dir+"WorkingWithImages.document_builder_insert_floating_image.doc")

วิธีแยกรูปภาพออกจากเอกสาร

รูปภาพทั้งหมดจะถูกจัดเก็บไว้ในโหนด Shape ในรูปแบบ Document หากต้องการแยกรูปภาพทั้งหมดหรือรูปภาพที่มีประเภทเฉพาะออกจากเอกสาร ให้ทำตามขั้นตอนเหล่านี้:

  • ใช้วิธี Document.get_child_nodes เพื่อเลือกโหนด Shape ทั้งหมด
  • วนซ้ำผ่านการรวบรวมโหนดที่เกิดขึ้น
  • ตรวจสอบคุณสมบัติบูลีน Shape.has_image
  • แยกข้อมูลภาพโดยใช้คุณสมบัติ Shape.image_data
  • บันทึกข้อมูลภาพเป็นไฟล์

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแยกรูปภาพจากเอกสารและบันทึกเป็นไฟล์:

คุณสามารถดาวน์โหลดไฟล์เทมเพลตของตัวอย่างนี้ได้จาก ที่นี่

# 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 + "Images.docx")
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True)
imageIndex = 0
for shape in shapes :
shape = shape.as_shape()
if (shape.has_image) :
imageFileName = f"Image.ExportImages.{imageIndex}_{aw.FileFormatUtil.image_type_to_extension(shape.image_data.image_type)}"
shape.image_data.save(docs_base.artifacts_dir + imageFileName)
imageIndex += 1

วิธีแทรกบาร์โค้ดในแต่ละหน้าของเอกสาร

ตัวอย่างนี้สาธิตให้คุณเพิ่มบาร์โค้ดที่เหมือนกันหรือต่างกันในหน้าทั้งหมดหรือเฉพาะเจาะจงของเอกสาร Word ไม่มีวิธีโดยตรงในการเพิ่มบาร์โค้ดในทุกหน้าของเอกสาร แต่คุณสามารถใช้วิธี move_to_section, move_to_header_footer และ insert_image เพื่อย้ายไปยังส่วนหรือส่วนหัว/ส่วนท้าย และแทรกรูปภาพบาร์โค้ดตามที่คุณเห็นในโค้ดต่อไปนี้

ตัวอย่างโค้ดต่อไปนี้ แทรกภาพบาร์โค้ดในแต่ละหน้าของเอกสาร

# 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)
# The number of pages the document should have
numPages = 4
# The document starts with one section, insert the barcode into this existing section
self.insert_barcode_into_footer(builder, doc.first_section, aw.HeaderFooterType.FOOTER_PRIMARY)
for i in range(1, numPages) :
# Clone the first section and add it into the end of the document
cloneSection = doc.first_section.clone(False).as_section()
cloneSection.page_setup.section_start = aw.SectionStart.NEW_PAGE
doc.append_child(cloneSection)
# Insert the barcode and other information into the footer of the section
self.insert_barcode_into_footer(builder, cloneSection, aw.HeaderFooterType.FOOTER_PRIMARY)
# Save the document as a PDF to disk
# You can also save this directly to a stream
doc.save(docs_base.artifacts_dir + "InsertBarcodeImage.docx")
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def insert_barcode_into_footer(builder : aw.DocumentBuilder, section : aw.Section, footerType : aw.HeaderFooterType) :
# Move to the footer type in the specific section.
builder.move_to_section(section.document.index_of(section))
builder.move_to_header_footer(footerType)
# Insert the barcode, then move to the next line and insert the ID along with the page number.
# Use pageId if you need to insert a different barcode on each page. 0 = First page, 1 = Second page etc.
builder.insert_image(docs_base.images_dir + "Barcode.png")
builder.writeln()
builder.write("1234567890")
builder.insert_field("PAGE")
# Create a right-aligned tab at the right margin.
tabPos = section.page_setup.page_width - section.page_setup.right_margin - section.page_setup.left_margin
builder.current_paragraph.paragraph_format.tab_stops.add(aw.TabStop(tabPos, aw.TabAlignment.RIGHT, aw.TabLeader.NONE))
# Move to the right-hand side of the page and insert the page and page total.
builder.write(aw.ControlChar.TAB)
builder.insert_field("PAGE")
builder.write(" of ")
builder.insert_field("NUMPAGES")

ล็อคอัตราส่วนภาพของภาพ

อัตราส่วนภาพของรูปทรงเรขาคณิตคืออัตราส่วนของขนาดในมิติต่างๆ คุณสามารถล็อคอัตราส่วนภาพของภาพโดยใช้ aspect_ratio_locked ค่าเริ่มต้นของอัตราส่วนภาพของรูปร่างจะขึ้นอยู่กับ ShapeType เป็น True สำหรับ ShapeType.IMAGE และ False สำหรับรูปร่างประเภทอื่นๆ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับอัตราส่วนภาพ:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

shape = builder.insert_image(docs_base.images_dir + "Logo.jpg")
shape.aspect_ratio_locked = False

doc.save(docs_base.artifacts_dir+"WorkingWithImages.set_aspect_ratio_locked.doc")

วิธีรับขอบเขตของรูปร่างตามจริงเป็นคะแนน

หากคุณต้องการให้กล่องขอบเขตจริงของรูปร่างตามที่แสดงผลบนเพจ คุณสามารถทำได้โดยใช้คุณสมบัติ bounds_in_points

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการใช้คุณสมบัตินี้:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

shape = builder.insert_image(docs_base.images_dir + "Logo.jpg")
shape.aspect_ratio_locked = False

print("\nGets the actual bounds of the shape in points.")
rect = shape.get_shape_renderer().bounds_in_points
print(f"{rect.x}, {rect.y}, {rect.width}, {rect.height}")

ครอบตัดรูปภาพ

การครอบตัดรูปภาพมักหมายถึงการลบส่วนนอกที่ไม่ต้องการของรูปภาพออกเพื่อช่วยปรับปรุงการจัดเฟรม นอกจากนี้ยังใช้สำหรับการลบบางส่วนของภาพเพื่อเพิ่มโฟกัสไปที่พื้นที่เฉพาะ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการบรรลุเป้าหมายนี้โดยใช้ Aspose.Words API:

# The path to the documents directory.
inputPath = docs_base.images_dir + "Logo.jpg"
outputPath = docs_base.artifacts_dir + "cropped_logo.jpg"

self.crop_image(inputPath,outputPath, 100, 90, 200, 200)
@staticmethod
def crop_image(inPath : str, outPath : str, left : int, top : int, width : int, height : int) :
    
    doc = aw.Document();
    builder = aw.DocumentBuilder(doc)
    
    croppedImage = builder.insert_image(inPath)
    
    src_width_points = croppedImage.width
    src_height_points = croppedImage.height
    
    croppedImage.width = aw.ConvertUtil.pixel_to_point(width)
    croppedImage.height = aw.ConvertUtil.pixel_to_point(height)
    
    widthRatio = croppedImage.width / src_width_points
    heightRatio = croppedImage.height / src_height_points
    
    if (widthRatio< 1) :
        croppedImage.image_data.crop_right = 1 - widthRatio
    
    if (heightRatio< 1) :
        croppedImage.image_data.crop_bottom = 1 - heightRatio
    
    leftToWidth = aw.ConvertUtil.pixel_to_point(left) / src_width_points
    topToHeight = aw.ConvertUtil.pixel_to_point(top) / src_height_points
    
    croppedImage.image_data.crop_left = leftToWidth
    croppedImage.image_data.crop_right = croppedImage.image_data.crop_right - leftToWidth
    
    croppedImage.image_data.crop_top = topToHeight
    croppedImage.image_data.crop_bottom = croppedImage.image_data.crop_bottom - topToHeight
    
    croppedImage.get_shape_renderer().save(outPath, aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG))

การบันทึกภาพเป็น WMF

Aspose.Words มีฟังก์ชันในการบันทึกรูปภาพที่มีอยู่ในเอกสารเป็นรูปแบบ WMF ในขณะที่แปลง DOCX เป็น RTF

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการบันทึกรูปภาพเป็น WMF ด้วยตัวเลือกการบันทึก RTF:

# 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")
saveOptions = aw.saving.RtfSaveOptions()
saveOptions.save_images_as_wmf = True
doc.save(docs_base.artifacts_dir + "WorkingWithRtfSaveOptions.saving_images_as_wmf.rtf", saveOptions)