Get Started
Documentation

ApiDocument

new ApiDocument()

Class representing a document.

Extends

Name Description
ApiDocumentContent Class representing a container for paragraphs and tables.

Methods

Name Description
AddElement Add paragraph or table using its position in the document.
CreateNumbering Create an abstract multilevel numbering with a specified type.
CreateSection Create a new document section which ends at the specified paragraph. Allows to set local parameters for the current section - page size, footer, header, columns, etc.
CreateStyle Create a new style with the specified type and name. If there is a style with the same name it will be replaced with a new one.
GetClassType Get this class type.
GetCommentsReport Get a report about all the comments added to the document.
GetDefaultParaPr Get a set of default paragraph properties in the current document.
GetDefaultStyle Get the default style parameters for the specified document element.
GetDefaultTextPr Get a set of default properties for the text run in the current document.
GetElement Get the element by its position in the document.
GetElementsCount Get the number of elements in the current document.
GetFinalSection Get the document final section.
GetReviewReport Get a report about every change which was made to the document in the review mode.
GetStyle Get a style by the style name.
Push Push a paragraph or a table to actually add it to the document.
RemoveAllElements Remove all elements from the current document or from the current document element.
RemoveElement Remove element using the position specified.
SetEvenAndOddHdrFtr Specify whether sections in this document will have different headers and footers for even and odd pages (one header/footer for odd pages and another header/footer for even pages).

Example

Copy code
builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph, oParaPr, oTextPr, oRun;
var oNoSpacingStyle = oDocument.GetStyle("No Spacing");
var oSection = oDocument.GetFinalSection();
var oHeader = oSection.GetHeader("default", true);
oParagraph = oHeader.GetElement(0);
oParagraph.AddText("This is the text in the default header");
oTextPr = oDocument.GetDefaultTextPr();
oTextPr.SetFontSize(22);
oTextPr.SetLanguage("en-US");
oTextPr.SetFontFamily("Calibri");
oParaPr = oDocument.GetDefaultParaPr();
oParaPr.SetSpacingLine(276, "auto");
oParaPr.SetSpacingAfter(200);
var oNormalStyle = oDocument.GetDefaultStyle("paragraph");
oParaPr = oNormalStyle.GetParaPr();
oParaPr.SetSpacingLine(240, "auto");
oParaPr.SetJc("both");
oTextPr = oNormalStyle.GetTextPr();
oTextPr.SetColor(0x26, 0x26, 0x26, false);
var oHeading1Style = oDocument.CreateStyle("Heading 1", "paragraph");
oParaPr = oHeading1Style.GetParaPr();
oParaPr.SetKeepNext(true);
oParaPr.SetKeepLines(true);
oParaPr.SetSpacingAfter(240);
oTextPr = oHeading1Style.GetTextPr();
oTextPr.SetColor(0xff, 0x68, 0x00, false);
oTextPr.SetFontSize(40);
oTextPr.SetFontFamily("Calibri Light");
oParagraph = oDocument.GetElement(0);
oParagraph.SetStyle(oHeading1Style);
oParagraph.AddText("This is a heading");
oParagraph = Api.CreateParagraph();
oRun = Api.CreateRun();
oRun.AddText("Number of paragraph elements at this point: ");
oRun.AddTabStop();
oRun.AddText("" + oParagraph.GetElementsCount());
oRun.AddLineBreak();
oParagraph.AddElement(oRun);
oRun.AddText("Number of paragraph elements after we added a text run: ");
oRun.AddTabStop();
oRun.AddText("" + oParagraph.GetElementsCount());
oDocument.Push(oParagraph);
var oNumbering = oDocument.CreateNumbering("bullet");
var oNumLvl;
for (var nLvl = 0; nLvl < 8; ++nLvl)
{
  oNumLvl = oNumbering.GetLevel(nLvl);
  oParagraph = Api.CreateParagraph();
  oParagraph.AddText("Default bullet lvl " + (nLvl + 1));
  oParagraph.SetNumbering(oNumLvl);
  oParagraph.SetContextualSpacing(true);
  oDocument.Push(oParagraph);
}
builder.SaveFile("docx", "ApiDocument.docx");
builder.CloseFile();

Resulting document