Skip to content

Allow clients to specify image width, flow, and alt text for images#56

Merged
shinjukunian merged 27 commits intoshinjukunian:masterfrom
oneeightyg:image-width-and-flow-pr
Apr 14, 2026
Merged

Allow clients to specify image width, flow, and alt text for images#56
shinjukunian merged 27 commits intoshinjukunian:masterfrom
oneeightyg:image-width-and-flow-pr

Conversation

@andalman
Copy link
Copy Markdown
Contributor

@andalman andalman commented Apr 8, 2026

Added a new class, DocXImageAttachment, which is a subclass of NSTextAttachment. This new class contains the following additional properties:

  • imageWidthFraction: the image width as a fraction of the text column width
  • imageFlow: specifies whether the image should be aligned left or right, with text flowing around it (when not specified, the image is centered and text does not flow around it)
  • imageDescription: the alt text for the image

This is obviously a subset of the image layout feature set that Word offers, but it adds some much-needed flexibility with how this package deals with images.

andalman and others added 25 commits October 7, 2022 10:17
This will properly escape reserved characters that appear in links (e.g. the ampersand)

shinjukunian#24
This required adding a “settings.xml” file that contains the following important line:
<w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="15"/>

Setting “compatibilityMode” to 15 indicates that this is a “modern” docx file. With that set, Word will not open the docx in “Compatibility Mode” and you will be able to use recent Word features.

In order for the settings.xml file to be found, “document.xml.rels” now contains a reference to “settings.xml.”

Lastly, “[Content_Types].xml” was edited so that it is more human-readable.
# Conflicts:
#	DocX/blank/word/_rels/document.xml.rels
Made `color` optional when creating an underline element. In addition, if the `underlineColor` attribute is present, it will be preferred over `foregroundColor` (if any). If neither is specified, the underline color will not be output explicitly.
# Conflicts:
#	DocX/AttributeElements.swift
#	DocX/DocXPageDefinition.swift
#	DocX/DocXWriting.swift
#	DocXTests/DocXTests.swift
To annotate a paragraph does that it becomes a real list item in Word, you can use the following new attributes:
  .listNumberingId
  .listNumberingLevel
  .listStyle

The “listNumberingId” is the identifier for the definition in the generated numbering.xml file. The “listNumberingLevel” specifies the indent level (0-based). And “listStyle” determines what kind of decoration is used for the list item (e.g. decimal, or bullet).
Added four new attributes:
  .footnoteReferenceId
  .footnoteBodyId
  .endnoteReferenceId
  .endnoteBodyId

To indicate a footnote cue, add the “.footnoteReferenceId” attribute with an integer value that serves as an identifier for the footnote. Then, for the paragraph(s) of the footnote body, add the “.footnoteBodyId” attribute with the same integer identifier. Anything tagges with the “.footnoteBodyId” will not be exported into the main document content.

For endnotes, just use the other attribute pair.
Added a new class, DocXImageAttachment, which is a subclass of NSTextAttachment. This new class contains the following additional properties:
- imageWidthFraction: the image width as a fraction of the text column width
- imageFlow: specifies whether the image should be aligned left or right, with text flowing around it (when not specified, the image is centered and text does not flow around it)
- imageDescription: the alt text for the image

This is obviously a subset of the image layout feature set that Word offers, but it adds some much-needed flexibility with how this package deals with images.
Added a new class, DocXImageAttachment, which is a subclass of NSTextAttachment. This new class contains the following additional properties:
- imageWidthFraction: the image width as a fraction of the text column width
- imageFlow: specifies whether the image should be aligned left or right, with text flowing around it (when not specified, the image is centered and text does not flow around it)
- imageDescription: the alt text for the image

This is obviously a subset of the image layout feature set that Word offers, but it adds some much-needed flexibility with how this package deals with images.
This test was bad: due to a copy/paste error, we were reusing footnote IDs. This is a problem, as they are intended to be globally unique.

To prevent this in the future, we now check to make sure that footnote/endnote reference IDs are globally unique. If not, DocX will throw an error during writing. I added another test, testWriteSections_duplicateFootnoteIdThrows, that tests this.

Lastly, while looking at the OOXML, I discovered that we weren’t adhering to OOXML standards (though this didn’t result in an issue). Nevertheless, we now ensure that <w​:p​Pr> is the first child of <w:p> when adding section breaks.
@shinjukunian shinjukunian self-requested a review April 13, 2026 10:04
@shinjukunian shinjukunian self-assigned this Apr 13, 2026
Copy link
Copy Markdown
Owner

@shinjukunian shinjukunian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @andalman ,

This is great. Could you rebase this PR onto the master branch?

Cheers and thanks for your contribution

@andalman
Copy link
Copy Markdown
Contributor Author

andalman commented Apr 14, 2026 via email

@shinjukunian shinjukunian self-requested a review April 14, 2026 22:10
@shinjukunian shinjukunian merged commit 7b65899 into shinjukunian:master Apr 14, 2026
1 check passed
@shinjukunian
Copy link
Copy Markdown
Owner

thank you again for your contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants