Document Previewer
The document previewer renders a file inline in the browser — typically inside a modal panel — without triggering a download. Your handler generates or loads the file bytes and passes them directly in the /runevent response.
When your handler calls PreviewDocument(), the command is returned in the /runevent response. The SDK receives it and opens the viewer.
Method signature
cmd.PreviewDocument(string fileId, byte[]? data, string fieldName = "attached_files", object? context = null)
| Parameter | Description |
|---|---|
fileId | Filename shown to the user in the viewer (e.g. "contract.pdf") |
data | File content as byte[]. The bytes are Base64-encoded and embedded in the response. Pass null to have the SDK stream the file by path instead. |
fieldName | Optional. The widget field this file is associated with. Defaults to "attached_files". |
context | Optional. Record context object (pluginCode, formCode, guid, pGuid) — used when streaming large files. |
Generating and previewing a PDF
The most common pattern is to generate PDF bytes on demand and pass them directly:
public override async Task Form_onPrint()
{
byte[] pdfBytes = await _pdfService.RenderAsync("ContractTemplate", record.GetData());
cmd.PreviewDocument("contract.pdf", pdfBytes);
}
The bytes are Base64-encoded and embedded in the /runevent response — no separate file storage step is required.
Previewing a stored file
If the file is already in storage, load the bytes and pass them the same way:
public async Task Form_onClick(string widgetName)
{
if (widgetName == "previewattachmentbtn")
{
string? filePath = record.GetField("attachment")?.ToString();
if (string.IsNullOrEmpty(filePath))
{
ShowMessage("warning", "No attachment found.");
return;
}
byte[] fileBytes = await _fileStorage.GetFileBytesAsync(filePath);
string fileName = Path.GetFileName(filePath);
cmd.PreviewDocument(fileName, fileBytes);
}
}
Large files
For files larger than ~10 MB, pass null for data and provide a context object so the SDK can stream the file by path instead of embedding the bytes in the response:
object previewContext = new
{
pluginCode = Context.PluginCode,
formCode = Context.FormCode,
guid = Context.Guid,
pGuid = Context.PGuid
};
cmd.PreviewDocument(filePath, fileBytes?.Length < 10 * 1024 * 1024 ? fileBytes : null, "attached_files", previewContext);
When data is null, the SDK uses fileId as a path and streams the content via /getlinkedfilebychunks.
Supported file types
The built-in viewer supports PDF files, images (PNG, JPG, GIF, etc.), and Excel spreadsheets.