API 参考

PDF

本 API 页面目前提供英文正文。代码签名与标识符不随界面语言变化。
Packages@univerjs-pro/pdfs

Facade APIs for creating, reading, and editing Univer PDF units.

Import @univerjs-pro/pdfs/facade before using these APIs in plugin mode.

For setup and task-oriented examples, start with the Univer PDFs guide.

Before using the API

Register UniverPdfsPlugin and import @univerjs-pro/pdfs/facade after @univerjs/core/facade. Browser editing also needs UniverPdfsUIPlugin, its CSS, and the client license configuration. See installation.

PDF positions and sizes use PDF points, not CSS pixels. Page indexes and element indexes are zero-based. Use save() to persist accepted edits; getDocument() returns the source document and does not replace a complete snapshot. See the PDF data model.

Unit access

TypeScript
createPdf(data?: Partial<IPdfUnitData>, options?: ICreateUnitOptions): FPdfgetActivePdf(): FPdf | nullgetPdf(id: string): FPdf | nullgetPdfTableThemePresets(): ReadonlyArray<Readonly<IPdfTableThemePreset>>

createPdf() returns a new FPdf; omitting data creates a blank unit. Supply a complete snapshot to restore saved content. getActivePdf() and getPdf(id) return null if no matching PDF exists, so check the result before editing.

FPdf

CategoryMethods
Identity and datagetId, getName, save, getDocument, getModel
PagesinsertPage, getPages, getPageById, getPageByIndex
TypeScript
getId(): stringgetName(): stringsave(): IPdfUnitDatagetDocument(): IPdfDocumentgetModel(): PdfDocumentModelinsertPage(index?: number): FPdfPagegetPages(): FPdfPage[]getPageById(id: string): FPdfPage | nullgetPageByIndex(index: number): FPdfPage | null

Page access and persistence

MethodParametersResult and behavior
insertPage(index?)Optional integer from 0 through the current page countReturns FPdfPage; omission appends. Throws RangeError for an invalid index. The first page uses A4; later pages inherit adjacent page geometry.
getPageByIndex(index)Zero-based page indexReturns FPdfPage or null when absent.
getPageById(id)Stable page IDReturns FPdfPage or null when absent.
save()NoneReturns a detached IPdfUnitData snapshot with current edits.
getDocument()NoneReturns the source IPdfDocument baseline.

FPdfPage

FPdfPage resolves existing page elements and inserts editable text boxes, paragraphs, lists, tables, dividers, annotations, and images.

TypeScript
getId(): stringgetIndex(): numbergetData(): Readonly<IPdfPage>getElements(): FPdfPageElement[]getElementById(id: string): FPdfPageElement | nullgetTextBoxes(): FPdfTextBox[]getImages(): FPdfImage[]getParagraphs(): FPdfParagraph[]getLists(): FPdfList[]getTables(): FPdfTable[]getDividers(): FPdfDivider[]getAnnotations(): FPdfAnnotation[]getTextSpans(): FPdfTextSpan[]insertTextBox(options: IPdfTextBoxInsertOptions): FPdfTextBoxinsertParagraph(options: IPdfParagraphInsertOptions): FPdfParagraphinsertList(options: IPdfListInsertOptions): FPdfListinsertTable(options: IPdfTableInsertOptions): FPdfTableinsertDivider(options?: IPdfDividerInsertOptions): FPdfDividerinsertAnnotation(options: IPdfAnnotationInsertOptions): FPdfAnnotationnewImage(existing?: FPdfImage | string): FPdfImageBuilderinsertImage(info: IPdfImageBuilderInfo, index?: number): FPdfImageinsertImageAsync(source: string | IFBlobSource, options?: IPdfImageInsertOptions): Promise<FPdfImage>

Insertion parameters

Text boxes, paragraphs, lists, tables, dividers, annotations, and images share IPdfElementInsertOptions:

PropertyTypeMeaning
idstring?Stable element ID; generated if omitted.
left, topnumber?Position in PDF points; default 0.
width, heightnumber?Size in PDF points; defaults depend on the element type.
rotationnumber?Clockwise degrees; default 0.
indexnumber?Zero-based position in the editable element stack; omission places the element at the front.

Text boxes and paragraphs

insertTextBox(options) returns FPdfTextBox; insertParagraph(options) returns FPdfParagraph.

PropertyTypeMeaning
textstringRequired initial text; an empty string is valid.
fontFamilystring?Initial font family; renderer default if omitted.
fontSizenumber?Initial size in points; default 12.
fillPdfColor?Initial text color.

Tables

insertTable(options) returns FPdfTable.

PropertyTypeMeaning
rowCount, columnCountnumberRequired positive initial dimensions.
cellTextsstring[]?Initial values in row-major order; missing cells contain empty strings.
defaultCellStyleIPdfTableCellStyle?Style copied to each initial cell.
styleIdPdfStyleId?Table style ID; use a table theme preset or the default.
optionsIPdfTableStyleOptions?Conditional table-style regions.

Images

insertImageAsync(source, options?) accepts a URL string or IFBlobSource and returns Promise<FPdfImage>. Await completion before saving the snapshot. Image options add imageSourceType, crop, and opacity (0 through 1, default 1) to the shared placement fields. See images.

Errors and lifetime

Mutations throw when the action is rejected. Invalid page insertion indexes throw RangeError. Catch errors at the application interaction boundary and present the failed operation to the user. Do not continue using page or element wrappers after their owning unit is disposed. Dispose the owning univer instance when unmounting the editor.

Page elements

Every FPdfPageElement supports identity, transform, visibility, locking, z-order, and removal. Specialized wrappers add content editing:

WrapperMethods
FPdfTextBoxgetText, getTextRuns, setText, setTextStyle, getTextAnchor, setTextAnchor
FPdfParagraphgetBlocks, insertBlock, appendBlock, setBlockText, setBlockStyle, removeBlock
FPdfListgetItems, insertItem, setItemText, removeItem, changeItemLevel, setPreset, setStartNumber
FPdfTablegetRowCount, getColumnCount, getCell, resize, getTheme, setTheme
FPdfTableCellgetText, setText, getStyle, setStyle
FPdfDividergetStroke, setStroke
FPdfAnnotationgetAnnotationType, getMarkup, getInk, getStyle, setStyle
FPdfImagegetSource, getImageSourceType, setSource, getCrop, setCrop, getOpacity, setOpacity, toBuilder
FPdfTextSpangetId, getText, getTextRuns, getBounds, replaceText
TypeScript
// FPdfPageElementgetId(): stringgetType(): PdfObjectTypegetData(): Readonly<PdfObject>getTransform(): IPdfFacadeTransformsetTransform(transform: IPdfFacadeTransform): thissetPosition(left: number, top: number): thissetSize(width: number, height: number): thissetRotation(rotation: number): thissetVisible(visible: boolean): thissetLocked(locked: boolean): thisbringToFront(): thissendToBack(): thisbringForward(): thissendBackward(): thisremove(): void
TypeScript
import '@univerjs-pro/pdfs/facade'const pdf = univerAPI.createPdf({ name: 'Project brief' })const page = pdf.insertPage()page.insertTextBox({ text: 'Hello PDF', left: 36, top: 36 }).setTextStyle({ fontSize: 18, fill: '#274690' })page.insertTable({  left: 36,  top: 90,  rowCount: 2,  columnCount: 2,  cellTexts: ['Metric', 'Value', 'Users', '42'],})const snapshot = pdf.save()// Store snapshot in your application; pass it to createPdf(snapshot) when restoring.

PDF facade enums include PdfAnnotationType, PdfListKind, PdfListPresetId, PdfTextAnchor, and PdfTableCellVerticalAlign through univerAPI.Enum.

For browser printing, see @univerjs-pro/pdfs-print.

Source: @univerjs-pro/pdfs

你觉得这篇文档如何?

© 2026 DreamNum Co., Ltd.