Skip to content

Writer

Creates, loads, and converts word processing documents in DOCX, PDF, HTML, RTF, TXT, and Word XML formats.

Creates an empty document. Add a section with SectionCollection.Add before adding content, or load content with Writer.LoadText or Writer.LoadFromBase64.

1
new Writer(): Writer

Remarks

A document is a tree of elements: Writer.Sections contain blocks (paragraphs and tables), and paragraphs contain inlines (text runs, fields, hyperlinks, and so on). Collections create their elements with Add... methods; every method takes an optional options object whose properties are applied to the new element, for example AddRun({ Text : 'Total' }).

Writer is a thin layer over the GemBox.Document library, so the public properties and methods of the underlying objects are also available even when this reference does not list them. Values are converted as follows:

  • Enumeration values are strings, such as 'Center'.
  • Colors are strings in #RRGGBB form. Options objects do not apply colors; set color properties after the element is added, for example run.CharacterFormat.FontColor = '#1F4E79'.
  • When a method has several overloads, the overload with the same number of arguments is called.

Use Writer.LoadText with HTML for documents with simple layout; build the element tree when you need tables, headers, footers, fields, or exact formatting.

Examples

Build a PDF document from paragraphs

1
2
3
4
5
6
7
8
9
var writer = new Writer();
var section = writer.Sections.Add();

$Xml.SelectAll('Lines/Line', function (line) {
    var run = section.Blocks.AddParagraph().Inlines.AddRun();
    run.Text = line.Evaluate('Product') + ': ' + line.Evaluate('Quantity');
});

var file = $Files.AddBase64(writer.SaveToBase64('.pdf'), 'Lines.pdf', 'application/pdf');

Create a table with a header row from form data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var writer = new Writer();
var section = writer.Sections.Add();

section.Blocks.AddParagraph({ ParagraphFormat : { Alignment : 'Center', SpaceAfter : 12 } })
    .Inlines.AddRun({ Text : 'Order ' + $Xml.Evaluate('OrderNumber'), CharacterFormat : { Bold : true, Size : 16 } });

var table = section.Blocks.AddTable();

function addRow(values, bold) {
    var row = table.Rows.Add();

    for (var i = 0; i < values.length; i++) {
        row.Cells.Add().Blocks.AddParagraph()
            .Inlines.AddRun({ Text : values[i], CharacterFormat : { Bold : bold } });
    }
}

addRow(['Product', 'Quantity', 'Price'], true);

$Xml.SelectAll('Lines/Line', function (line) {
    addRow([line.Evaluate('Product'), line.Evaluate('Quantity'), line.Evaluate('Price')], false);
});

$Files.AddBase64(writer.SaveToBase64('.pdf'), 'Order.pdf', 'application/pdf');
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var writer = new Writer();
var section = writer.Sections.Add({ PageSetup : { Orientation : 'Landscape' } });

section.Blocks.AddParagraph().Inlines.AddRun({ Text : $Xml.Evaluate('ReportText') });

var footer = section.HeadersFooters.Add('FooterDefault');
var paragraph = footer.Blocks.AddParagraph({ ParagraphFormat : { Alignment : 'Right' } });

paragraph.Inlines.AddRun({ Text : 'Page ' });
paragraph.Inlines.AddField('Page');

$Files.AddBase64(writer.SaveToBase64('.pdf'), 'Report.pdf', 'application/pdf');
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
var writer = new Writer();
var paragraph = writer.Sections.Add().Blocks.AddParagraph();

var status = paragraph.Inlines.AddRun({ Text : 'Approved', CharacterFormat : { Bold : true, Size : 14 } });
status.CharacterFormat.FontColor = '#2E7D32';

paragraph.Inlines.AddSpecialCharacter('LineBreak');
paragraph.Inlines.AddRun({ Text : 'Details: ' });
paragraph.Inlines.AddHyperlink({
    Address : 'https://portal.example.com/requests/' + $Xml.Evaluate('RequestId'),
    Text : 'Open the request'
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
var writer = new Writer();
var paragraph = writer.Sections.Add().Blocks.AddParagraph();

paragraph.Inlines.AddField('DisplayBarcode', {
    Type : 'QR',
    Value : 'https://portal.example.com/assets/' + $Xml.Evaluate('AssetNumber')
});

paragraph.Inlines.AddSpecialCharacter('LineBreak');

paragraph.Inlines.AddField('DisplayBarcode', {
    Type : 'Code128',
    Value : $Xml.Evaluate('AssetNumber'),
    ShowLabel : true
});

$Files.AddBase64(writer.SaveToBase64('.pdf'), 'AssetLabel.pdf', 'application/pdf');

Capabilities

Constructors