Images¶
FeatherDoc does not expose a single featherdoc::Image object. Image APIs
are available on featherdoc::Document and featherdoc::TemplatePart.
Use them to append inline or floating images, inspect packaged image metadata,
and extract, replace, or remove existing image parts.
Common Tasks¶
Use these entry points for image workflows:
Append an inline image with detected source dimensions: call
Document::append_image(image_path).Append an inline image with explicit display size: call
Document::append_image(image_path, width_px, height_px).Append an image to a header, footer, or section part: call
TemplatePart::append_image(...).Place a floating image: configure
floating_image_optionsand callappend_floating_image(...).Inspect packaged images before editing: call
drawing_images()orinline_images().Preserve references while changing media: use
replace_drawing_image(...)orreplace_inline_image(...).Export or delete media: use the matching
extract_*orremove_*method with an index from the corresponding list method.
Success And Failure Semantics¶
Return shape |
Success |
Failure or no-op |
|---|---|---|
|
Contains image metadata in the order used by image-index APIs. |
Empty means no matching image placement exists in the document. |
|
|
|
|
|
|
|
|
|
|
|
|
Short Example¶
// PNG/JPEG/GIF/BMP dimensions are detected through the stb_image path.
doc.append_image("logo.png", 240, 80);
auto images = doc.inline_images();
if (!images.empty()) {
doc.replace_inline_image(images.front().index, "logo-new.png");
}
Dimension Detection¶
When an append or replace method needs source dimensions, FeatherDoc uses
stb_image as the authoritative byte probe for PNG, JPEG, GIF,
and BMP files. SVG, WebP, and TIFF keep FeatherDoc-specific
dimension readers because they are outside the supported stb_image decode
set used by this project.
Explicit width_px and height_px values bypass source-size inference for
layout size, but the image file still has to be readable and recognized so the
package media part and content type can be written correctly.
The package content type and media part extension are selected from the source
file extension. Keep the extension aligned with the actual image bytes; for
example, do not store JPEG bytes in a .png file when the generated DOCX
should advertise image/png.
Typed Signature Guide¶
Image APIs use zero-based image_index values from drawing_images() or
inline_images(). width_px and height_px are explicit pixel sizes.
Methods returning bool report whether the package image part and its
relationships were updated. Methods that infer size may return false when
dimension detection cannot identify a supported image format.
Signature |
Parameters |
Return semantics |
|---|---|---|
|
None. |
All drawing images, including anchored floating images. |
|
None. |
All inline images with package metadata and optional body location. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Image Info Types¶
Type |
Key fields |
Purpose |
|---|---|---|
|
|
Describes one inline image and where it appears in body content. |
|
|
Describes either an inline drawing object or an anchored floating image. |
|
references, offsets, wrap distances, |
Configures floating image placement, wrapping, layering, and optional crop. |
|
|
Stores crop percentages in per-mille units. |
Append Images¶
Method |
Returns |
Purpose |
|---|---|---|
|
|
Append an inline image to the document body using detected source dimensions. |
|
|
Append an inline image to the body with explicit pixel dimensions. |
|
|
Append a floating image to the body with placement metadata. |
|
|
Append a floating image to the body with explicit size. |
|
|
Append an inline image in a body, header, footer, or section part using detected source dimensions. |
|
|
Append a floating image in the selected template part. |
Inspect And Mutate Images¶
Method |
Returns |
Purpose |
|---|---|---|
|
|
Enumerate drawing images, including anchored floating images. |
|
|
Enumerate inline images. |
|
|
Copy one drawing image part to a filesystem path. |
|
|
Replace one drawing image while preserving document references. |
|
|
Remove one drawing image and its related package part. |
|
|
Copy one inline image part to a filesystem path. |
|
|
Replace one inline image while preserving document references. |
|
|
Remove one inline image and its related package part. |
Example¶
featherdoc::Document doc{"template.docx"};
doc.open();
// Uses detected PNG dimensions when no explicit size is supplied.
doc.append_image("logo.png");
// Uses explicit layout dimensions.
doc.append_image("logo.png", 240, 80);
featherdoc::floating_image_options options;
options.horizontal_offset_px = 32;
options.vertical_offset_px = 12;
options.wrap_mode = featherdoc::floating_image_wrap_mode::square;
doc.body_template().append_floating_image("badge.png", options);
auto inline_images = doc.inline_images();
if (!inline_images.empty()) {
doc.replace_inline_image(0, "logo-updated.png");
}