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
- Create Word .docx files
- Document property settings (title, author, subject, company)
- Paragraph operations (add, align, style)
- Heading levels 1-9
- Font formatting (bold, italic, underline, strikethrough, superscript/subscript, font name, color, highlight, font size)
- Paragraph formatting (indentation, spacing, line spacing, page break control)
- Tab stops (left/right/center/decimal alignment, 5 leader types)
- Table operations (create, style, alignment, column width, cells)
- Hyperlinks
- Page breaks, line breaks, column breaks
- Page setup (paper size, margins, orientation)
- Unit conversion (EMU/twips/points/cm/inches)
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.
| Parameter | Type | Description |
| *doc | PbDocument | Document object pointer |
| filePath | String | Output 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.
| Parameter | Type | Description |
| *doc | PbDocument | Document object pointer |
| title | String | Document 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.
| Parameter | Type | Description |
| *doc | PbDocument | Document object pointer |
| text | String | Paragraph text (can be an empty string) |
Returns: Paragraph object pointer (PbDocParagraph).
PbDoc_Document_AddParagraph(*doc, "This is paragraph text")
*para = PbDoc_Document_AddParagraph(*doc, "")
PbDoc_Paragraph_SetAlignment(*para.PbDocParagraph, alignment.l)
Set paragraph alignment.
| Parameter | Type | Description |
| alignment | Integer | Alignment 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.
| Parameter | Type | Description |
| text | String | Heading text |
| level | Integer | Heading 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.
| Parameter | Type | Description |
| breakType | Integer | #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 Value | Description |
| #PbDoc_UNDERLINE_SINGLE | Single underline |
| #PbDoc_UNDERLINE_DOUBLE | Double underline |
| #PbDoc_UNDERLINE_DOTTED | Dotted underline |
| #PbDoc_UNDERLINE_DASH | Dashed underline |
| #PbDoc_UNDERLINE_WAVE | Wavy underline |
| #PbDoc_UNDERLINE_THICK | Thick 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")
PbDoc_Font_SetColorHex(*run, "0000FF")
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)
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)
PbDoc_Font_SetSize(*run, 24)
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.
| Parameter | Type | Description |
| rows | Integer | Number of rows |
| cols | Integer | Number 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.
*table = PbDoc_Document_AddTable(*doc, 3, 4)
PbDoc_Table_SetStyle(*table, "TableGrid")
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).
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).
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
Example Code
Example 1: Create a Basic Word Document
XIncludeFile "../PbDoc.pb"
PbDoc_Init()
*doc.PbDocument = PbDocument_Create()
PbDocument_SetTitle(*doc, "My Document")
PbDocument_SetAuthor(*doc, "PbDoc User")
PbDoc_Document_AddHeading(*doc, "Chapter 1 Introduction", 1)
PbDoc_Document_AddParagraph(*doc, "This is the first paragraph.")
PbDocument_Save(*doc, "output.docx")
PbDocument_Free(*doc)
PbDoc_Cleanup()
Example 2: Formatted Text
*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
*table = PbDoc_Document_AddTable(*doc, 3, 4)
PbDoc_Table_SetStyle(*table, "TableGrid")
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")
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")