Document Loading Methods
These methods load documents into the editor from various sources.SetDocumentFromBytes
Loads a document into the editor from a byte array.The document content as a byte array.
The file name with extension (e.g., “report.docx”). The extension is used to determine document type.
- Generates a unique GUID-based file ID
- Saves the document to
~/App_Data/uploads/directory (creates if needed) - Sets DocumentName, DocumentKey, DocumentUrl, and CallbackUrl properties
- Does nothing if data is null/empty or fileName is null/whitespace
SetDocumentFromFile
Loads a document from a file path on the server.Physical or virtual path to the file on the server.
Optional display name for the document. If null, uses the file name from the path.
- Checks if file exists; returns silently if not found
- Reads file contents and delegates to
SetDocumentFromBytes - Uses file name from path if displayName not provided
SetDocumentFromUpload
Loads a document that was previously uploaded via the handler.The unique file identifier from the upload handler. Should correspond to a file in
~/App_Data/uploads/.Original file name from the upload. If null, uses fileId as the display name.
- Does not verify file existence (assumes handler validated)
- Constructs DocumentUrl and CallbackUrl using the fileId
- Generates DocumentKey from fileId
- Does nothing if fileId is null/whitespace
Document Retrieval Methods
GetEditedDocumentBytes
Retrieves the captured edited document as a byte array.byte[] - The edited document bytes, or null if no captured document exists or decoding fails.
Behavior:
- Reads the hidden field value containing Base64-encoded document
- Decodes from Base64 to byte array
- Returns null if hidden field is empty or decoding fails
This method retrieves client-side captured content, not real-time editor state. Content must be captured using
CaptureTriggerId or JavaScript API first.ClearEditedDocument
Clears the captured edited document from the hidden field.- Sets the hidden field value to empty string
- Call after successfully processing captured document to prevent duplicate saves
PDF Conversion Methods
These methods convert documents to PDF using OnlyOffice Document Server conversion service.All conversion methods use retry logic with configurable attempts and delays. They communicate with the Document Server’s ConvertService.ashx endpoint.
ConvertCurrentDocumentToPdfBytes
Converts the current document (original or edited if captured) to PDF and returns the bytes.Maximum number of conversion attempts. Conversion may require multiple attempts if the document is large.
Delay in milliseconds between conversion attempts.
byte[] - The PDF file content as a byte array.
Throws:
InvalidOperationException- If no document is loaded (HasDocumentis false)TimeoutException- If conversion doesn’t complete within maxAttemptsInvalidOperationException- If the Document Server returns an error
- If
HasEditedDocumentis true, converts the edited version - Otherwise, converts the original document
- Delegates to
ConvertCurrentDocumentToPdfUrlthen downloads the bytes
ConvertCurrentDocumentToPdfUrl
Converts the current document to PDF and returns the download URL.Maximum number of conversion attempts.
Delay in milliseconds between attempts.
string - Absolute URL to download the converted PDF from OnlyOffice Document Server.
Throws: Same as ConvertCurrentDocumentToPdfBytes
Behavior:
- If
HasEditedDocumentis true, converts the edited version - Otherwise, converts the original document
- Returns a temporary URL from the Document Server
ConvertEditedDocumentToPdfBytes
Converts the captured edited document to PDF and returns the bytes.Maximum number of conversion attempts.
Delay in milliseconds between attempts.
byte[] - The PDF file content.
Throws:
InvalidOperationException- If no base document is loadedInvalidOperationException- If no edited document has been capturedTimeoutException- If conversion doesn’t complete
ConvertEditedDocumentToPdfUrl
Converts the captured edited document to PDF and returns the download URL.Maximum number of conversion attempts.
Delay in milliseconds between attempts.
string - Absolute URL to download the converted PDF.
Throws: Same as ConvertEditedDocumentToPdfBytes
Behavior:
- Creates a temporary document source from the edited bytes
- Uploads to
~/App_Data/uploads/with_editedsuffix - Sends to Document Server for conversion
- Returns the PDF download URL from Document Server
Method Summary Table
| Method | Return Type | Purpose |
|---|---|---|
| SetDocumentFromBytes | void | Load document from byte array |
| SetDocumentFromFile | void | Load document from server file path |
| SetDocumentFromUpload | void | Load document using upload handler file ID |
| GetEditedDocumentBytes | byte[] | Retrieve captured edited document bytes |
| ClearEditedDocument | void | Clear captured edited document |
| ConvertCurrentDocumentToPdfBytes | byte[] | Convert current document to PDF bytes |
| ConvertCurrentDocumentToPdfUrl | string | Convert current document to PDF URL |
| ConvertEditedDocumentToPdfBytes | byte[] | Convert edited document to PDF bytes |
| ConvertEditedDocumentToPdfUrl | string | Convert edited document to PDF URL |
Conversion Service Details
The conversion methods use the OnlyOffice Document Server ConvertService.ashx endpoint. The URL is automatically resolved from The conversion service URL becomes:
OnlyOfficeApiUrl property.Example: If OnlyOfficeApiUrl is:Supported File Types
The conversion service supports: Word Processing: docx, doc, odt, rtf, txt, html, epub, mht Spreadsheets: xlsx, xls, ods, csv Presentations: pptx, ppt, odpRetry Logic
The conversion process may require multiple attempts because:- Document Server processes conversions asynchronously
- Large documents take time to convert
- The service returns a progress percentage until complete
- Construct request payload with document URL, type, and key
- Sign with JWT token
- POST to ConvertService.ashx
- Check response:
- If
endConvert: trueandfileUrlpresent → Success - If
errorpresent → Throw exception - Otherwise → Retry after delay
- If
- Repeat until maxAttempts reached