PbDoc Library

Version 1.3
Author: lcode.cn | License: Apache 2.0

Overview

PbDoc is a Word document processing library implemented entirely in PureBasic. It can create .docx files conforming to the Office Open XML standard without requiring Microsoft Office or any third-party dependencies.

This library is written with reference to the Python python-docx project, using PureBasic's built-in Packer (ZIP compression) library.

Note: Before using the PbDoc library, you need to add XIncludeFile "PbDoc.pb" at the beginning of your code to include the library file, and call PbDoc_Init() for initialization.

Key Features

Function Index

Initialization and Cleanup

PbDoc_Init()
Initialize the PbDoc library. This function must be called before using any PbDoc features.
Returns: #True on success, #False on failure.
; Initialize PbDoc library If PbDoc_Init() = #False Debug "Initialization failed" End EndIf
PbDoc_Cleanup()
Clean up all resources used by the PbDoc library. This function should be called before the program exits.

Document Operations

PbDocument_Create()
Create a new blank document. Returns a document object pointer, which is used for all subsequent operations.
Returns: Document object pointer (PbDocument). Returns 0 on failure.
*doc.PbDocument = PbDocument_Create() If *doc = 0 Debug "Failed to create document" EndIf
PbDocument_Save(*doc.PbDocument, filePath.s)
Save the document as a .docx file. This function generates a ZIP archive conforming to the Office Open XML standard.
ParameterTypeDescription
*docPbDocumentDocument object pointer
filePathStringOutput file path (.docx extension)
Returns: #True on success, #False on failure.
If PbDocument_Save(*doc, "output.docx") Debug "Save successful" Else Debug "Save failed: " + PbDoc_GetLastError() EndIf
PbDocument_Free(*doc.PbDocument)
Release all memory resources used by the document. This should be called after saving.
PbDocument_SetTitle(*doc.PbDocument, title.s)
Set the document title. This property is written to docProps/core.xml.
ParameterTypeDescription
*docPbDocumentDocument object pointer
titleStringDocument title
PbDocument_SetAuthor(*doc.PbDocument, author.s)
Set the document author.
PbDocument_SetSubject(*doc.PbDocument, subject.s)
Set the document subject.
PbDocument_SetCompany(*doc.PbDocument, company.s)
Set the company name. This property is written to docProps/app.xml.

Paragraph Operations

PbDoc_Document_AddParagraph(*doc.PbDocument, text.s)
Add a paragraph. If text is provided, a Run containing that text is automatically created.
ParameterTypeDescription
*docPbDocumentDocument object pointer
textStringParagraph text (can be an empty string)
Returns: Paragraph object pointer (PbDocParagraph).
; Add a normal paragraph PbDoc_Document_AddParagraph(*doc, "This is paragraph text") ; Add an empty paragraph, then manually add Runs *para = PbDoc_Document_AddParagraph(*doc, "")
PbDoc_Paragraph_SetAlignment(*para.PbDocParagraph, alignment.l)
Set paragraph alignment.
ParameterTypeDescription
alignmentIntegerAlignment enum: #PbDoc_ALIGN_LEFT, #PbDoc_ALIGN_CENTER, #PbDoc_ALIGN_RIGHT, #PbDoc_ALIGN_JUSTIFY, #PbDoc_ALIGN_DISTRIBUTE
*para = PbDoc_Document_AddParagraph(*doc, "Centered text") PbDoc_Paragraph_SetAlignment(*para, #PbDoc_ALIGN_CENTER)
PbDoc_Document_AddHeading(*doc.PbDocument, text.s, level.l)
Add a heading paragraph. Headings automatically apply built-in Heading styles.
ParameterTypeDescription
textStringHeading text
levelIntegerHeading level (1-9), 1 is the highest level
Returns: Paragraph object pointer.
PbDoc_Document_AddHeading(*doc, "Chapter 1 Introduction", 1) PbDoc_Document_AddHeading(*doc, "1.1 Background", 2)

Run Operations

A Run is a segment of text within a paragraph that has the same formatting. A paragraph can contain multiple Runs, each with different formatting.

PbDoc_Paragraph_AddRun(*para.PbDocParagraph, text.s)
Add a text run (Run) to a paragraph.
Returns: Run object pointer (PbDocRun).
*para = PbDoc_Document_AddParagraph(*doc, "") *run1 = PbDoc_Paragraph_AddRun(*para, "Normal text ") *run2 = PbDoc_Paragraph_AddRun(*para, "Bold text") PbDoc_Font_SetBold(*run2, #True)
PbDoc_Run_AddTab(*run.PbDocRun)
Add a Tab character to a Run.
PbDoc_Run_AddBreak(*run.PbDocRun, breakType.l)
Add a break to a Run.
ParameterTypeDescription
breakTypeInteger#PbDoc_BREAK_LINE (line break), #PbDoc_BREAK_PAGE (page break), #PbDoc_BREAK_COLUMN (column break)

Font Formatting

PbDoc_Font_SetBold(*run.PbDocRun, bold.l)
Set bold.
PbDoc_Font_SetItalic(*run.PbDocRun, italic.l)
Set italic.
PbDoc_Font_SetUnderline(*run.PbDocRun, underline.l)
Set underline type.
Enum ValueDescription
#PbDoc_UNDERLINE_SINGLESingle underline
#PbDoc_UNDERLINE_DOUBLEDouble underline
#PbDoc_UNDERLINE_DOTTEDDotted underline
#PbDoc_UNDERLINE_DASHDashed underline
#PbDoc_UNDERLINE_WAVEWavy underline
#PbDoc_UNDERLINE_THICKThick underline
PbDoc_Font_SetStrike(*run.PbDocRun, strike.l)
Set strikethrough.
PbDoc_Font_SetDoubleStrike(*run.PbDocRun, doubleStrike.l)
Set double strikethrough.
PbDoc_Font_SetSuperscript(*run.PbDocRun, superscript.l)
Set superscript.
PbDoc_Font_SetSubscript(*run.PbDocRun, subscript.l)
Set subscript.
PbDoc_Font_SetName(*run.PbDocRun, fontName.s)
Set font name, e.g. "SimSun", "Arial".
PbDoc_Font_SetColorHex(*run.PbDocRun, colorHex.s)
Set font color (hexadecimal format).
PbDoc_Font_SetColorHex(*run, "FF0000") ; Red PbDoc_Font_SetColorHex(*run, "0000FF") ; Blue
PbDoc_Font_SetColorRGB(*run.PbDocRun, r.a, g.a, b.a)
Set font color (RGB format, 0-255).
PbDoc_Font_SetColorRGB(*run, 255, 165, 0) ; Orange
PbDoc_Font_SetHighlight(*run.PbDocRun, highlight.s)
Set highlight color. Common values: "yellow", "green", "cyan", "red", "blue".
PbDoc_Font_SetSize(*run.PbDocRun, size.l)
Set font size (points).
PbDoc_Font_SetSize(*run, 12) ; 12pt PbDoc_Font_SetSize(*run, 24) ; 24pt

Paragraph Formatting

PbDoc_ParaFormat_SetFirstLineIndent(*para.PbDocParagraph, emu.q)
Set first line indent. Parameter is in EMU units.
; First line indent of 2 characters (approx. 0.74cm) PbDoc_ParaFormat_SetFirstLineIndent(*para, PbDoc_CmToEmu(0.74))
PbDoc_ParaFormat_SetLeftIndent(*para.PbDocParagraph, emu.q)
Set left indent.
PbDoc_ParaFormat_SetRightIndent(*para.PbDocParagraph, emu.q)
Set right indent.
PbDoc_ParaFormat_SetHangingIndent(*para.PbDocParagraph, emu.q)
Set hanging indent.
PbDoc_ParaFormat_SetSpaceBefore(*para.PbDocParagraph, emu.q)
Set space before paragraph.
; Space before paragraph: 12pt PbDoc_ParaFormat_SetSpaceBefore(*para, PbDoc_PtToEmu(12))
PbDoc_ParaFormat_SetSpaceAfter(*para.PbDocParagraph, emu.q)
Set space after paragraph.
PbDoc_ParaFormat_SetLineSpacing(*para.PbDocParagraph, spacing.q, rule.l)
Set line spacing.
ParameterTypeDescription
spacingQuadLine spacing value. Auto spacing: 240=single, 360=1.5x, 480=double; Exact/AtLeast spacing: twips value
ruleInteger#PbDoc_LINE_SPACING_AUTO (auto), #PbDoc_LINE_SPACING_EXACT (exact), #PbDoc_LINE_SPACING_AT_LEAST (at least)
; 1.5x line spacing PbDoc_ParaFormat_SetLineSpacing(*para, 360, #PbDoc_LINE_SPACING_AUTO) ; Fixed 20pt line spacing PbDoc_ParaFormat_SetLineSpacing(*para, PbDoc_PtToTwips(20), #PbDoc_LINE_SPACING_EXACT)
PbDoc_ParaFormat_SetKeepNext(*para.PbDocParagraph, keepNext.l)
Set keep with next, preventing a page break between this paragraph and the next.
PbDoc_ParaFormat_SetKeepLines(*para.PbDocParagraph, keepLines.l)
Set keep lines together, preventing page breaks within the paragraph.
PbDoc_ParaFormat_SetWidowControl(*para.PbDocParagraph, widowControl.l)
Set widow/orphan control, preventing the first or last line of a paragraph from appearing alone at the top or bottom of a page.
PbDoc_ParaFormat_AddTabStop(*para.PbDocParagraph, position.q, alignment.l, leader.l)
Add a tab stop.
ParameterTypeDescription
positionQuadPosition (EMU units)
alignmentIntegerAlignment: #PbDoc_TAB_LEFT, #PbDoc_TAB_CENTER, #PbDoc_TAB_RIGHT, #PbDoc_TAB_DECIMAL
leaderIntegerLeader: #PbDoc_TAB_LEADER_NONE, #PbDoc_TAB_LEADER_DOTS, #PbDoc_TAB_LEADER_DASHES, #PbDoc_TAB_LEADER_LINES
; Right-aligned tab with dot leader (table of contents style) PbDoc_ParaFormat_AddTabStop(*para, PbDoc_CmToEmu(14), #PbDoc_TAB_RIGHT, #PbDoc_TAB_LEADER_DOTS) *run1 = PbDoc_Paragraph_AddRun(*para, "Chapter 1 Introduction") PbDoc_Run_AddTab(*run1) *run2 = PbDoc_Paragraph_AddRun(*para, "1")

Page Breaks and Line Breaks

PbDoc_Document_AddPageBreak(*doc.PbDocument)
Insert a page break. Subsequent content starts on a new page.

Table Operations

PbDoc_Document_AddTable(*doc.PbDocument, rows.l, cols.l)
Create a table.
ParameterTypeDescription
rowsIntegerNumber of rows
colsIntegerNumber of columns
Returns: Table object pointer (PbDocTable).
PbDoc_Table_SetStyle(*tbl.PbDocTable, style.s)
Set table style. Commonly use "TableGrid" for a bordered table.
PbDoc_Table_SetAlignment(*tbl.PbDocTable, alignment.l)
Set table alignment.
PbDoc_Table_SetColumnWidth(*tbl.PbDocTable, colIndex.l, widthEmu.q)
Set column width (EMU units).
PbDoc_Table_SetColumnWidth(*table, 0, PbDoc_CmToEmu(5))
PbDoc_Table_GetCell(*tbl.PbDocTable, row.l, col.l)
Get a cell reference. Row and column indices start from 0.
Returns: Cell object pointer (PbDocCell).
PbDoc_Cell_SetText(*cell.PbDocCell, text.s)
Set cell text.
; Create a 3-row, 4-column table *table = PbDoc_Document_AddTable(*doc, 3, 4) PbDoc_Table_SetStyle(*table, "TableGrid") ; Fill header row PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 0, 0), "Name") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 0, 1), "Age")

Page Setup

PbDoc_Section_SetPageSize(*doc.PbDocument, widthTwips.q, heightTwips.q)
Set page size (twips units).
; A4 paper: 21cm x 29.7cm PbDoc_Section_SetPageSize(*doc, PbDoc_CmToTwips(21), PbDoc_CmToTwips(29.7))
PbDoc_Section_SetMargins(*doc.PbDocument, top.q, bottom.q, left.q, right.q)
Set page margins (twips units).
; Word default margins: top/bottom 2.54cm, left/right 3.17cm PbDoc_Section_SetMargins(*doc, PbDoc_CmToTwips(2.54), PbDoc_CmToTwips(2.54), PbDoc_CmToTwips(3.17), PbDoc_CmToTwips(3.17))
PbDoc_Section_SetOrientation(*doc.PbDocument, orientation.l)
Set page orientation. #PbDoc_ORIENT_PORTRAIT (portrait) or #PbDoc_ORIENT_LANDSCAPE (landscape).

Unit Conversion

PbDoc uses two internal units: EMU (English Metric Units) and twips. EMU is used for paragraph formatting parameters, and twips are used for page setup parameters.

PbDoc_InchesToEmu(inches.d) / PbDoc_CmToEmu(cm.d) / PbDoc_PtToEmu(pt.d)
Convert inches/centimeters/points to EMU. Used for paragraph formatting parameters.
PbDoc_CmToTwips(cm.d) / PbDoc_InchesToTwips(inches.d) / PbDoc_PtToTwips(pt.d)
Convert centimeters/inches/points to twips. Used for page setup parameters.
Common conversions:
1 inch = 914400 EMU = 1440 twips
1 cm = 360000 EMU = 567 twips
1 point = 12700 EMU = 20 twips

Enumeration Constants

Paragraph Alignment

ConstantValueDescription
#PbDoc_ALIGN_LEFT1Left align
#PbDoc_ALIGN_CENTER2Center align
#PbDoc_ALIGN_RIGHT3Right align
#PbDoc_ALIGN_JUSTIFY4Justify
#PbDoc_ALIGN_DISTRIBUTE5Distribute

Tab Alignment

ConstantValueDescription
#PbDoc_TAB_LEFT1Left tab
#PbDoc_TAB_CENTER2Center tab
#PbDoc_TAB_RIGHT3Right tab
#PbDoc_TAB_DECIMAL4Decimal tab

Tab Leaders

ConstantValueDescription
#PbDoc_TAB_LEADER_NONE1No leader
#PbDoc_TAB_LEADER_DOTS2Dots (......)
#PbDoc_TAB_LEADER_HEAVY3Heavy dots
#PbDoc_TAB_LEADER_DASHES4Dashes (------)
#PbDoc_TAB_LEADER_LINES5Underscore (______)

Underline Types

ConstantValueDescription
#PbDoc_UNDERLINE_NONE0No underline
#PbDoc_UNDERLINE_SINGLE1Single underline
#PbDoc_UNDERLINE_DOUBLE2Double underline
#PbDoc_UNDERLINE_DOTTED3Dotted underline
#PbDoc_UNDERLINE_DASH4Dashed underline
#PbDoc_UNDERLINE_WAVE5Wavy underline
#PbDoc_UNDERLINE_THICK6Thick underline

Break Types

ConstantValueDescription
#PbDoc_BREAK_LINE1Line break (soft return)
#PbDoc_BREAK_PAGE2Page break
#PbDoc_BREAK_COLUMN3Column break

Line Spacing Rules

ConstantValueDescription
#PbDoc_LINE_SPACING_AUTO1Auto line spacing (multiple)
#PbDoc_LINE_SPACING_EXACT2Exact line spacing (fixed)
#PbDoc_LINE_SPACING_AT_LEAST3At least line spacing (minimum)

Page Orientation

ConstantValueDescription
#PbDoc_ORIENT_PORTRAIT1Portrait
#PbDoc_ORIENT_LANDSCAPE2Landscape

Example Code

Example 1: Create a Basic Word Document

; PbDoc - Create a basic Word document example XIncludeFile "../PbDoc.pb" PbDoc_Init() *doc.PbDocument = PbDocument_Create() ; Set document properties PbDocument_SetTitle(*doc, "My Document") PbDocument_SetAuthor(*doc, "PbDoc User") ; Add heading and paragraph PbDoc_Document_AddHeading(*doc, "Chapter 1 Introduction", 1) PbDoc_Document_AddParagraph(*doc, "This is the first paragraph.") ; Save the file PbDocument_Save(*doc, "output.docx") PbDocument_Free(*doc) PbDoc_Cleanup()

Example 2: Formatted Text

; Add a multi-Run paragraph (different formatting) *para = PbDoc_Document_AddParagraph(*doc, "") *run1 = PbDoc_Paragraph_AddRun(*para, "Bold text ") PbDoc_Font_SetBold(*run1, #True) *run2 = PbDoc_Paragraph_AddRun(*para, "Red text ") PbDoc_Font_SetColorHex(*run2, "FF0000") *run3 = PbDoc_Paragraph_AddRun(*para, "18pt text") PbDoc_Font_SetSize(*run3, 18)

Example 3: Create a Table

; Create a 3-row, 4-column table *table = PbDoc_Document_AddTable(*doc, 3, 4) PbDoc_Table_SetStyle(*table, "TableGrid") ; Fill header row PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 0, 0), "Name") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 0, 1), "Age") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 0, 2), "City") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 0, 3), "Occupation") ; Fill data row PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 1, 0), "Zhang San") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 1, 1), "28") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 1, 2), "Beijing") PbDoc_Cell_SetText(PbDoc_Table_GetCell(*table, 1, 3), "Engineer")

Version History

v1.3 (2026-04-23)

v1.2 (2026-04-23)

v1.1 (2026-04-22)

v1.0 (2026-04-21)