处理图像

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")

如何从文档中提取图像

所有图像都存储在 Document 中的 Shape 节点内。要从文档中提取所有图像或具有特定类型的图像,请按照下列步骤操作:

以下代码示例演示如何从文档中提取图像并将其另存为文件:

您可以从这里下载本示例的模板文件。

# 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_sectionmove_to_header_footerinsert_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。对于 ShapeType.IMAGE,它是 True;对于其他形状类型,它是 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 的功能。

以下代码示例演示如何使用 RTF 保存选项将图像保存为 WMF:

# 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)