Open Source

Flutter package

docs_gee

Pure Dart DOCX and PDF generation from a single document model — no native dependencies, runs everywhere Dart runs.

v1.4.222.8k / month31 likesMITAll platforms incl. WASMDOCX + PDF

DocxReader & API

Read → modify → write round-trips with DocxReader, the core class reference and output compatibility.

source: README.md

API

DocxReader — read, modify, write

Added in 1.3.0, DocxReader parses an existing DOCX file into a DocxDocument — enabling a full round-trip: read, modify with the normal mutation API, then write back out. It reads files created by Microsoft Word and other DOCX-compatible applications.

dart
class DocxReader {
  const DocxReader();
  DocxDocument read(Uint8List bytes)
}

Read a document and walk its content in order:

dart
final reader = DocxReader();
final document = reader.read(docxBytes);

for (final item in document.content) {
  if (item is DocxParagraph) {
    print(item.plainText);
  }
}

The test suite exercises the round-trip with two one-liners — generate bytes, then read them straight back:

dart
Uint8List generate(DocxDocument doc) => generator.generate(doc);
DocxDocument roundTrip(DocxDocument doc) => reader.read(generate(doc));

Put together, a read → modify → write cycle looks like this (composed from the documented read, addParagraph and generate calls):

dart
// Read an existing DOCX into a document model
final document = DocxReader().read(docxBytes);

// Modify it with the normal mutation API
document.addParagraph(Paragraph.heading('Appendix', level: 1));
document.addParagraph(Paragraph.text('Added after reading.'));

// Write it back out
final updated = DocxGenerator().generate(document);

What the reader preserves

  • All paragraph styles: headings 1-4, subtitle, caption, quote, code block, footnote
  • Text formatting: bold, italic, underline, strikethrough, color, background color
  • Alignment
  • Lists (bullet, dash, numbered, alpha, roman) with indent levels
  • Tables with borders, colspan, rowspan and cell properties
  • External hyperlinks and internal bookmark references
  • Page breaks and line breaks
  • Mixed content order
  • Document metadata (title, author) from docProps/core.xml

Parsing failures throw a sealed DocxReaderException, so you can switch on the concrete cause:

dart
sealed class DocxReaderException implements Exception

class InvalidDocxArchiveException extends DocxReaderException  // not a valid ZIP archive
class MissingDocxPartException extends DocxReaderException     // Missing required DOCX part: $partName
class InvalidDocxXmlException extends DocxReaderException      // Malformed XML in DOCX

Heads up

The reader degrades gracefully: a missing styles.xml or numbering.xml falls back to default mappings, and unknown styles fall back to normal. Some content is skipped, however:

  • Images (<w:drawing>) are skipped
  • Charts and embedded objects are skipped
  • Complex fields (TOC) are skipped
  • Column widths are not preserved

Document

DocxDocument (alias Document) is the root container. Add content with addParagraph, addParagraphs and addTable; read it back via the content getter, which returns paragraphs and tables in insertion order.

ParameterTypeDefault
paragraphsList<DocxParagraph>?
titleString?
authorString?
includeTableOfContentsboolfalse
tocTitleString'Table of Contents'
tocMaxLevelint3

Paragraph & TextRun

DocxParagraph (alias Paragraph) holds a list of runs plus block-level properties. The named factories — .text, .heading, .subtitle, .caption, .quote, .codeBlock, .footnote, .bulletItem, .dashItem, .numberedItem, .alphaItem, .romanItem and .empty — wrap this constructor. plainText joins the run texts.

ParameterTypeDefault
runsList<DocxRun>required
styleDocxParagraphStylenormal
alignmentDocxAlignmentleft
pageBreakBeforeboolfalse
indentLevelint0
bookmarkNameString?

DocxRun (alias TextRun) is a span of text with inline formatting. TextRun.lineBreak() is a convenience for a soft return; getters isLink and hasFormatting and a copyWith method round it out.

ParameterTypeDefault
textStringrequired
boldboolfalse
italicboolfalse
underlineboolfalse
strikethroughboolfalse
colorString?
backgroundColorString?
hyperlinkString?
bookmarkRefString?
isLineBreakboolfalse

Table, TableRow & TableCell

DocxTable takes rows and table-level borders; columnCount and rowCount getters account for spans. Factories DocxTable.simple and DocxTable.fromHeaders build tables from plain data.

ParameterTypeDefault
rowsList<DocxTableRow>required
bordersDocxTableBordersDocxTableBorders.all()
columnWidthsList<double>?

DocxTableRow is a thin wrapper around its cells:

ParameterTypeDefault
cellsList<DocxTableCell>required

DocxTableCell carries paragraphs, alignment, backgrounds, per-cell borders and merge spans. DocxTableCell.text is a shorthand for a single-string cell, and DocxTableCell.merged() is the continuation placeholder for a rowSpan merge.

ParameterTypeDefault
paragraphsList<DocxParagraph>const []
alignmentDocxAlignmentleft
verticalAlignmentDocxVerticalAlignmenttop
backgroundColorString?
bordersDocxCellBorders?
colSpanint1
rowSpanint1
isMergedContinuationboolfalse

Borders share the DocxBorder model (width in eighths of a point), and the alignment, vertical-alignment and style enums are:

dart
const DocxBorder({
  this.color = '000000',
  this.size = 4,
  this.style = DocxBorderStyle.single,
});
dart
enum DocxAlignment { left, center, right, justify }        // justify -> OOXML 'both'
enum DocxVerticalAlignment { top, center, bottom }
enum DocxBorderStyle { single, double, dashed, dotted }
enum DocxParagraphStyle {
  normal, heading1, heading2, heading3, heading4,
  subtitle, caption, quote, codeBlock, footnote,
  listBullet, listDash, listNumber, listNumberAlpha, listNumberRoman,
}

Generators & PDF options

Both generators implement DocumentGenerator and return Uint8List bytes.

dart
abstract class DocumentGenerator {
  Uint8List generate(DocxDocument document);
}

DocxGenerator defaults to Times New Roman at 24 half-points (12pt). DOCX font sizes are half-points:

dart
DocxGenerator({
  this.fontName = 'Times New Roman',
  this.fontSize = 24,   // half-points (24 = 12pt, 28 = 14pt)
})
ValuePoint sizeTypical use
2010ptFootnotes
2211ptCompact body
2412ptStandard body
2814ptSubheadings
3216ptSection headings
4824ptTitles

PdfGenerator works in points and exposes page size and margins. Its font must be one of the PDF base-14 fonts (Helvetica, Times-Roman, Courier).

ParameterTypeDefault
fontNameString'Helvetica'
fontSizeint12
pageWidthint612
pageHeightint792
marginTopint72
marginBottomint72
marginLeftint72
marginRightint72

Source vs doc/api.md

Where the published doc/api.md disagrees with the source, the source above is authoritative: fontSize is an int (not a double) and PdfGenerator carries the page and margin parameters; Table defaults to DocxTableBorders.all() (not none()); and DocxBorderStyle has no thick value.

Type aliases

The Docx-prefixed classes are exported under format-agnostic aliases, which is why the examples use Document, Paragraph and TextRun rather than their full names. Both spellings refer to the same class.

AliasOriginal class
DocumentDocxDocument
ParagraphDocxParagraph
TextRunDocxRun
AlignmentDocxAlignment
ParagraphStyleDocxParagraphStyle
TableDocxTable
TableRowDocxTableRow
TableCellDocxTableCell
TableBordersDocxTableBorders
BorderDocxBorder
BorderStyleDocxBorderStyle
CellBordersDocxCellBorders
VerticalAlignmentDocxVerticalAlignment
DocumentReaderDocxReader

Let's make something together.

If you have any questions about a new project or other inquiries, feel free to contact us. We will get back to you as soon as possible.

Codigee
We are using cookies. Learn more