Document¶
featherdoc::Document is the root handle for a .docx package. Use it to
open and save files, access body/header/footer template parts, inspect sections,
and call document-wide editing APIs.
Common Tasks¶
Start here when you already know the workflow you need:
Open and save a document: use
Document(path),open(),save(), andsave_as(path).Fill a body template: use
body_template()or the document-levelreplace_content_control_text_by_tag(...)shortcut.Work on headers or footers: use
header_template(...),footer_template(...),section_header_template(...), orsection_footer_template(...).Edit body content directly: use
paragraphs(),tables(),append_table(...), and the image append methods.Inspect structure before changing it: use
inspect_sections(),inspect_body_blocks(),list_bookmarks(),list_content_controls(), andlast_error().
Success And Failure Semantics¶
Return shape |
Success |
Failure or no-op |
|---|---|---|
|
Empty error code means the package operation completed. |
Non-empty error code means the operation failed; inspect
|
|
|
|
|
Non-zero value is the number of matched items changed or appended. |
|
|
Contains the requested inspection or settings value. |
Empty means the section, setting, or semantic comparison could not be resolved. |
Handle objects |
Returned handles such as |
Check handle-specific validity rules before assuming the target exists. |
Short C++ Example¶
featherdoc::Document doc{"template.docx"};
if (doc.open()) return 1;
doc.replace_content_control_text_by_tag("customer", "Ada");
return doc.save_as("filled.docx") ? 1 : 0;
Typed Signature Guide¶
Document is a package-level handle. Call open() for an existing file or
create_empty() for a new package before using editing methods. Most indexes
are zero-based. Path parameters are filesystem paths; text parameters are
written into the resolved WordprocessingML target.
Signature |
Parameters |
Return semantics |
|---|---|---|
|
|
Creates a handle; no package is loaded until |
|
None. |
Empty error code means a new package was initialized. |
|
None. |
Empty error code means the current path was loaded. |
|
|
Empty error code means the package was written to the new path. |
|
None. |
Body template-part handle; check handle validity before editing. |
|
|
Header template-part handle for that physical part. |
|
|
Template-part handle for the resolved section header. |
|
|
Matched, replaced, requested, and missing-bookmark counts. |
|
|
Number of matching controls replaced; |
|
|
New body table handle. |
|
|
|
|
|
Empty when the section or setup cannot be resolved. |
Lifecycle¶
Open, create, save, and inspect the current package state.
Method |
Parameters |
Returns |
Purpose |
|---|---|---|---|
|
None. |
|
Create an empty handle; call |
|
|
|
Create a handle bound to a file path. |
|
None. |
|
Initialize a new empty document package. |
|
|
|
Replace the current path and reset loaded package state. |
|
None. |
|
Return the path currently bound to the handle. |
|
None. |
|
Load the current |
|
|
|
Load with the requested policy; use |
|
None. |
|
Return package issues, severity, entry names, and repairability recorded during tolerant open. |
|
|
|
Repair package state transactionally; no partial mutation occurs when an unsafe or disabled issue is present. |
|
None. |
|
Save changes back to the current path. |
|
|
|
Save changes to a new path. |
|
None. |
|
Return whether |
|
None. |
|
Inspect the latest failure |
|
None. |
|
Add |
|
None. |
|
Remove the update-fields-on-open setting. |
|
None. |
|
Inspect the update-fields-on-open setting; empty means the setting could not be read. |
Open Validation And Resource Limits¶
The parameterless open() uses the default document_open_options. The
default is package_validation_mode::strict and requires valid
[Content_Types].xml, root relationships, word/document.xml, and a
w:document/w:body structure. Callers repairing known legacy damage must
explicitly select package_validation_mode::tolerant. Tolerant validation
does not disable archive resource limits and never silently repairs the input.
Inspect package_diagnostics(), call repair_package() explicitly, and
write the result to a new file with save_as().
Default archive_limits allow 10,000 entries, 64 MiB per XML part, 256 MiB
per binary part, 512 MiB total uncompressed data, and a compression ratio of
200. These limits are checked from ZIP metadata before extraction. Raise them
only when the application has a concrete need and a trusted input boundary.
Explicit Package Repair¶
The default repair policy handles only deterministic changes that preserve
unknown metadata: create w:body under a valid w:document, create missing
root or main-document relationships, and create missing content types or correct
the main-document MIME. Malformed XML, invalid roots or namespaces, duplicate
main-document declarations, and external main-document relationships return
package_repair_not_possible without applying other changes.
After a successful repair, save() and save_as() write a sibling temporary
archive and reopen it with strict validation before replacing the target. A
failed check returns package_repair_validation_failed and preserves the
original. The CLI exposes inspect-package <input.docx> --json and
repair-package <input.docx> --output <repaired.docx> --json.
Handle Invalidation¶
Paragraph, Run, Table, TableRow, TableCell, and
TemplatePart are tracked, non-owning handles into the current DOM. Each
handle records a package generation and a node epoch, so it neither extends
the Document lifetime nor dereferences a pugixml node after the package or
node has been retired.
set_path(...),open(...), andcreate_empty()reset the package and invalidate every XML-backed handle previously returned by thatDocument.A successful
repair_package(...)that changes the package replaces repaired DOM state, so previously retained XML-backed handles must be reacquired.Removing a node invalidates handles to that node and all descendants.
Rebuilding a table, paragraph, content control, or template-part structure invalidates handles into the replaced subtree.
Use valid() for Paragraph, Run, Table, TableRow, and
TableCell; use the explicit bool conversion for TemplatePart.
Reads through an invalid handle return an empty result, while mutations return
false or an empty handle. Unaffected sibling subtrees remain valid.
Reacquire affected handles from Document or an unaffected parent.
Breaking API migration¶
Older releases exposed two-pugi::xml_node constructors and public
set_parent / set_current methods for the XML-backed handle classes.
Those entry points could not carry lifetime metadata and have been removed.
Obtain handles only from Document, TemplatePart, or a parent handle;
application code no longer needs direct pugixml DOM access through the public
API.
Template Part Access¶
Use template parts when the same operation should work on body, headers, or footers.
Method |
Parameters |
Returns |
Purpose |
|---|---|---|---|
|
None. |
|
Access the document body through the template-part API. |
|
|
|
Access a physical header part by index. |
|
|
|
Access a physical footer part by index. |
|
|
|
Access the resolved header for a section/reference kind. |
|
|
|
Access the resolved footer for a section/reference kind. |
Sections And Inspection¶
Method |
Parameters |
Returns |
Purpose |
|---|---|---|---|
|
|
|
Add a section to the end of the document. |
|
|
|
Insert a section before an existing section. |
|
|
|
Remove a section. |
|
|
|
Reorder sections without recreating the whole document. |
|
None. |
|
Return the number of sections. |
|
None. |
|
Return physical header/footer part counts. |
|
None. |
|
Return section/header/footer inspection data. |
|
|
|
Inspect one section; empty means the section is not available. |
|
|
|
Read page size, margins, orientation, and related setup. |
|
|
|
Apply page setup to a section. |
|
|
|
Replace section header text through the resolved header part. |
|
|
|
Replace section footer text through the resolved footer part. |
|
|
|
Remove the section reference only; it does not delete the physical part. |
|
|
|
Remove the footer reference only; it does not delete the physical part. |
|
None. |
|
Inspect paragraph/table order in the body. |
|
|
|
Compare semantic document content. |
Template Filling Shortcuts¶
These methods operate from Document directly. Use them for common template
fills without first taking a TemplatePart.
Method |
Parameters |
Returns |
Purpose |
|---|---|---|---|
|
|
|
Replace matching bookmark text and return the replacement count. |
|
|
|
Fill multiple bookmarks and report requested, matched, replaced, and missing bookmarks. |
|
|
|
Fill a small bookmark set without constructing a separate container. |
|
|
|
Inspect available bookmark slots before filling. |
|
|
|
Replace matching content controls and return the replacement count. |
|
|
|
Fill controls addressed by alias/title. |
|
None. |
|
Inspect content-control tags, aliases, lock state, and data binding metadata before filling. |
|
|
|
Locate fill targets before mutating the document. |
|
|
|
Replace matching controls with generated paragraphs. |
|
|
|
Replace matching controls with rows in the surrounding table context. |
|
|
|
Replace matching controls with generated tables. |
|
|
|
Replace matching controls with images. |
|
|
|
Replace matching controls with sized images. |
Body, Tables, And Images¶
Method |
Parameters |
Returns |
Purpose |
|---|---|---|---|
|
None. |
|
Return the body paragraph iterator/editing entry. |
|
None. |
|
Return the body table iterator/editing entry. |
|
|
|
Append a table and return the created table handle. |
|
|
|
Append an inline image using natural dimensions. |
|
|
|
Append an inline image with an explicit size. |
|
|
|
Append a floating image. |
|
|
|
Append a sized floating image. |
Notes, Revisions, And Links¶
The detailed object pages cover these families, but the root Document API
also exposes document-wide entry points.
Method |
Parameters |
Returns |
Purpose |
|---|---|---|---|
|
|
|
Add a comment; returns |
|
|
|
Add a comment to a precise paragraph text range. |
|
Start/end paragraph indexes, text offsets, comment body, and optional metadata. |
|
Add a comment that spans a multi-paragraph text range. |
|
None. |
|
Inspect tracked revisions. |
|
|
|
Accept or reject one tracked revision. |
|
|
|
Append a footnote. |
|
None. |
|
Inspect hyperlink text and targets before editing. |
|
|
|
Append a hyperlink to the body. |
|
|
|
Replace an existing hyperlink. |
Examples¶
Open, fill content controls, and save:
featherdoc::Document doc{"template.docx"};
if (doc.open()) {
return 1;
}
doc.body_template().replace_content_control_text_by_tag("customer", "Ada");
return doc.save_as("filled.docx") ? 1 : 0;
Fill directly from Document and inspect failures:
featherdoc::Document doc{"template.docx"};
if (auto error = doc.open()) {
std::cerr << doc.last_error().detail << '\n';
return 1;
}
const auto replaced =
doc.replace_content_control_text_by_tag("customer", "Ada");
if (replaced == 0) {
return 1;
}
if (auto error = doc.save_as("filled.docx")) {
std::cerr << doc.last_error().detail << '\n';
return 1;
}
return 0;
Create a new document with a table and field-update setting:
featherdoc::Document doc;
if (doc.create_empty()) {
return 1;
}
doc.enable_update_fields_on_open();
auto table = doc.append_table(2, 3);
if (auto customer = table.find_cell(0, 0)) {
customer->set_text("Customer");
}
if (auto status = table.find_cell(0, 1)) {
status->set_text("Status");
}
return doc.save_as("report.docx") ? 1 : 0;