with one click
open-xml-docx
// Guide and examples for using OpenXML Docx features in the open_xml package.
// Guide and examples for using OpenXML Docx features in the open_xml package.
| name | open-xml-docx |
| description | Guide and examples for using OpenXML Docx features in the open_xml package. |
The open_xml package allows for generating, editing, and parsing .docx documents.
To create a new Word Document:
import 'package:open_xml/open_xml.dart';
import 'package:file/local.dart';
Future<void> main() async {
const fs = LocalFileSystem();
final doc = await WordDocument.create(fs);
// Save the document
await doc.save(fs.file('example.docx'));
}
A WordDocument contains a sequence of elements, most commonly Paragraphs. A Paragraph contains a sequence of Runs.
// Add a simple paragraph
doc.addParagraph(
Paragraph()
..addRun(Run(text: 'Hello, '))
..addRun(Run(
text: 'World!',
bold: true,
color: 'FF0000',
fontSize: 24,
)),
);
bold: Boolean (true/false)italic: Boolean (true/false)underline: Boolean (true/false)color: Hex color string (e.g. '00FF00')fontSize: Integer representing half-points.fontFamily: String representing font face.Hyperlinks can be added to paragraphs easily:
doc.addParagraph(
Paragraph()
..addHyperlink(
'Visit Google',
'https://google.com',
),
);
You can insert comments on a specific text range:
doc.addParagraph(
Paragraph()
..addComment(
author: 'Author Name',
text: 'This is a comment on the text',
initials: 'AN',
run: Run(text: 'commented text'),
),
);
You can generate a .docx document directly from Markdown, or extract Markdown from an existing .docx.
final markdown = '''
# Heading 1
This is a paragraph with **bold** and *italic* text.
''';
await doc.addMarkdown(markdown);
final existingDoc = await WordDocument.open(fs.file('my_doc.docx'));
final markdownText = existingDoc.toMarkdown();
print(markdownText);
You can extract text nodes or navigate through paragraphs in existing documents:
final existingDoc = await WordDocument.open(fs.file('my_doc.docx'));
// Iterate over text runs
for (final text in existingDoc.parseText()) {
print(text);
}
Guide and examples for using OpenXML Package features in the open_xml package.
Guide and examples for using OpenXML Pptx features in the open_xml package.
Guide and examples for using OpenXML Xlsx features in the open_xml package.