FlexGrid
PDF Export
The sample demonstrates how to export FlexGrid content to a PDF file.
Features
You can use the FlexGridPdfConverter, a PDFKit-based JavaScript library, to export FlexGrid to PDF (Portable Document Format) without using any server-side code.
To export a FlexGrid, you need to use the FlexGridPdfConverter.export function that takes the following arguments:
- A FlexGrid instance.
- Name of the file to export.
- Export settings.
In this sample, you can change the following export settings using the menus above:
- scaleMode: Determines how the FlexGrid content should be scaled in order to fit the page.
- orientation: Determines the orientation of pages.
- exportMode: Determines which part of the FlexGrid should be exported (all of the data or only the current selection).
You can also specify some security settings, such as user password, owner password and permissions, to protect the exported PDF content.
Grouping support
This sample shows how the row grouping feature of the FlexGrid is supported in the FlexGridPdfConverter.
Merging support
This sample shows how the cell merging feature of the FlexGrid is supported in the FlexGridPdfConverter.
Tree view support and font embedding
This sample shows how the tree view feature of the FlexGrid is supported in the FlexGridPdfConverter.
In this sample, FlexGrid uses font Fira and two typefaces, FiraSans-Regular.ttf and FiraSans-Bold.ttf, are embeded into the document. The FiraSans-Bold.ttf (boldface) typeface is used to display the header cells, and the FiraSans-Regular.ttf typeface is used for rest of the content.
To achieve this, the following export settings are used:
- embeddedFonts: Provides information to the export library, about various custom fonts to be embedded, such as URL, name, style, weight.
- styles: It is used to set up the style for the grid elements and link them with the embedded fonts.
using MvcExplorer.Models; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; namespace MvcExplorer.Controllers { public partial class FlexGridController : Controller { private static IList<ITreeItem> treeViewData = null; private static IEnumerable<Sale> normalData = Sale.GetData(25); public ActionResult PDFExport() { if (treeViewData == null) { treeViewData = Folder.Create(Startup.Environment.WebRootPath).Children; } ViewBag.GroupedFlexGridData = normalData; ViewBag.MergedFlexGridData = normalData; ViewBag.TreeViewData = treeViewData; return View(); } } }
@using C1.Web.Mvc.Grid @{ IEnumerable<Sale> groupedFlexGridData = ViewBag.GroupedFlexGridData; IEnumerable<Sale> mergedFlexGridData = ViewBag.MergedFlexGridData; IList<ITreeItem> treeViewData = ViewBag.TreeViewData; ViewBag.DemoDescription = false; } @section Styles{ <style> .fixed-top { z-index: 999; position: fixed; top: 0px; } .reduce { padding-bottom: 0px; } </style> } @section Summary{ <p>@Html.Raw(FlexGridRes.PDFExport_Summary)</p> } @section Scripts{ <script> var hasAddFixedStyle, groupedGrid, mergedGrid, treeViewGrid, exportSettings, settingsDiv, settingsDivDfScroll, securitySettingsDiv; c1.documentReady(function () { hasAddFixedStyle = false; groupedGrid = wijmo.Control.getControl("#groupingFlexGrid"); mergedGrid = wijmo.Control.getControl("#mergingFlexGrid"); treeViewGrid = wijmo.Control.getControl("#treeFlexGrid"); settingsDiv = document.getElementById("exportSettingsDiv"); defaultScrollTop = getScrollingTop(settingsDiv); securitySettingsDiv = document.getElementById("securitySettingsDiv"); exportSettings = { exportMode: wijmo.grid.pdf.ExportMode.All, orientation: wijmo.pdf.PdfPageOrientation.Portrait, scaleMode: wijmo.grid.pdf.ScaleMode.ActualSize, version: "v1_3", }; window.onscroll = function () { var documentScrollTop = document.documentElement.scrollTop || document.body.scrollTop; if (documentScrollTop <= defaultScrollTop && hasAddFixedStyle) { showDiv(securitySettingsDiv); clearFixedStyles(settingsDiv); hasAddFixedStyle = false; } else if (documentScrollTop > defaultScrollTop && !hasAddFixedStyle) { hideDiv(securitySettingsDiv); addFixedStyles(settingsDiv); hasAddFixedStyle = true; } }; window.onresize = function () { if (hasAddFixedStyle) { clearFixedStyles(settingsDiv); addFixedStyles(settingsDiv); } }; updateHeaders(); }); function updateHeaders() { var flex = wijmo.Control.getControl("#mergingFlexGrid"); if (flex) { // insert new row if not yet if (flex.columnHeaders.rows.length === 1) { flex.columnHeaders.rows.insert(0, new wijmo.grid.Row()); } flex.columnHeaders.rows[0].allowMerging = true; // set headings so the cells merge for (var i = 0; i < flex.columns.length; i++) { var hdr = 'String'; switch (flex.columns[i].binding) { case 'ID': case 'Amount': case 'Discount': hdr = 'Number'; break; case 'Active': hdr = 'Boolean'; break; } flex.columnHeaders.setCellData(0, i, hdr); } } } function clearFixedStyles(obj) { obj.style.height = ""; obj.style.width = ""; var classes = obj.className.split(/\s+/g); classes.splice(classes.indexOf("fixed-top"), 1); obj.className = classes.join(" "); if (settingsDiv.offsetParent != null) { let parentObj = settingsDiv.offsetParent; parentObj.style.paddingTop = ''; } } function addFixedStyles(obj) { if (settingsDiv.offsetParent != null) { let parentObj = settingsDiv.offsetParent; parentObj.style.paddingTop = settingsDiv.offsetHeight + "px"; } obj.style.height = settingsDiv.offsetHeight + "px"; obj.style.width = settingsDiv.offsetWidth + "px"; obj.className = obj.className + " fixed-top"; } function showDiv(obj) { if (obj.style.display == "none") obj.style.display = ''; } function hideDiv(obj) { obj.style.display = "none"; } function getScrollingTop(obj) { var scrollTop = obj.offsetTop; if (obj.offsetParent != null) { scrollTop += getScrollingTop(obj.offsetParent); } return scrollTop; } function onAllowMergingChanged(menu) { var mergingHeader = menu.selectedItem.Header; menu.header = "@Html.Raw(FlexGridRes.PDFExport_AllowMerging) <b>" + mergingHeader + "</b>"; mergedGrid.allowMerging = menu.selectedItem.CommandParameter; } function exportGroupedGrid() { var fontFile = { source: 'https://demo.grapecity.com/wijmo/sample/fonts/ipaexg.ttf', name: 'ipaexg' }, font = new wijmo.pdf.PdfFont('ipaexg'); wijmo.grid.pdf.FlexGridPdfConverter.export(groupedGrid, 'FlexGrid.pdf', { maxPages: 10, exportMode: exportSettings.exportMode, scaleMode: exportSettings.scaleMode, documentOptions: { userPassword: getPassword('User'), ownerPassword: getPassword('Owner'), version: exportSettings.version, permissions: { annotating: getPermission('Annotating'), contentAccessibility: getPermission('ContentAccessibility'), copying: getPermission('Copying'), documentAssembly: getPermission('DocumentAssembly'), fillingForms: getPermission('FillingForms'), modifying: getPermission('Modifying'), printing: getPermission('Printing') }, pageSettings: { layout: exportSettings.orientation }, header: { declarative: { text: '&[Page]\\&[Pages]\theader\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '&[Page]\\&[Pages]\tfooter\t&[Page]\\&[Pages]' } }, info: { author: 'C1', title: 'PdfDocument sample', keywords: 'PDF, C1, sample', subject: 'PdfDocument' } }, embeddedFonts: [fontFile], styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6', font: font }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }); } function exportMergedGrid() { var fontFile = { source: 'https://demo.grapecity.com/wijmo/sample/fonts/ipaexg.ttf', name: 'ipaexg' }, font = new wijmo.pdf.PdfFont('ipaexg'); wijmo.grid.pdf.FlexGridPdfConverter.export(mergedGrid, 'FlexGrid.pdf', { maxPages: 10, exportMode: exportSettings.exportMode, scaleMode: exportSettings.scaleMode, documentOptions: { userPassword: getPassword('User'), ownerPassword: getPassword('Owner'), version: exportSettings.version, permissions: { annotating: getPermission('Annotating'), contentAccessibility: getPermission('ContentAccessibility'), copying: getPermission('Copying'), documentAssembly: getPermission('DocumentAssembly'), fillingForms: getPermission('FillingForms'), modifying: getPermission('Modifying'), printing: getPermission('Printing') }, pageSettings: { layout: exportSettings.orientation }, header: { declarative: { text: '&[Page]\\&[Pages]\theader\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '&[Page]\\&[Pages]\tfooter\t&[Page]\\&[Pages]' } }, info: { author: 'C1', title: 'PdfDocument sample', keywords: 'PDF, C1, sample', subject: 'PdfDocument' } }, embeddedFonts: [fontFile], styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6', font: font }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }); } function exportTreeViewGrid() { wijmo.grid.pdf.FlexGridPdfConverter.export(treeViewGrid, 'FlexGrid.pdf', { maxPages: 10, exportMode: exportSettings.exportMode, scaleMode: exportSettings.scaleMode, embeddedFonts: [{ source: '@(Url.Content("~/Content/fonts/fira/FiraSans-Regular.ttf"))', name: 'fira', style: 'normal', weight: 'normal', sansSerif: true }, { source: '@(Url.Content("~/Content/fonts/fira/FiraSans-Bold.ttf"))', name: 'fira', style: 'normal', weight: 'bold', sansSerif: true }], documentOptions: { userPassword: getPassword('User'), ownerPassword: getPassword('Owner'), version: exportSettings.version, permissions: { annotating: getPermission('Annotating'), contentAccessibility: getPermission('ContentAccessibility'), copying: getPermission('Copying'), documentAssembly: getPermission('DocumentAssembly'), fillingForms: getPermission('FillingForms'), modifying: getPermission('Modifying'), printing: getPermission('Printing') }, pageSettings: { layout: exportSettings.orientation }, header: { declarative: { text: '&[Page]\\&[Pages]\theader\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '&[Page]\\&[Pages]\tfooter\t&[Page]\\&[Pages]' } }, info: { author: 'C1', title: 'PdfDocument sample', keywords: 'PDF, C1, sample', subject: 'PdfDocument' } }, styles: { cellStyle: { backgroundColor: '#ffffff', borderColor: '#c6c6c6', font: { family: 'fira' } }, altCellStyle: { backgroundColor: '#f9f9f9' }, groupCellStyle: { backgroundColor: '#dddddd' }, headerCellStyle: { backgroundColor: '#eaeaea' } } }); } function setScaleMode(menu) { menu.header = "@(FlexGridRes.PDFExport_ScaleMode) <b>" + menu.selectedItem.Header + "</b>"; exportSettings.scaleMode = wijmo.grid.pdf.ScaleMode[menu.selectedItem.Header]; } function setOrientation(menu) { menu.header = "@(FlexGridRes.PDFExport_Orientation) <b>" + menu.selectedItem.Header + "</b>"; exportSettings.orientation = wijmo.pdf.PdfPageOrientation[menu.selectedItem.Header]; } function setExportMode(menu) { menu.header = "@(FlexGridRes.PDFExport_ExportMode) <b>" + menu.selectedItem.Header + "</b>"; exportSettings.exportMode = wijmo.grid.pdf.ExportMode[menu.selectedItem.Header]; } function setPermissionPrinting(menu) { menu.header = "@FlexGridRes.PDFExport_Printing: <b>" + menu.selectedItem.Header + "</b>"; exportSettings.permissionPrinting = menu.selectedItem.Header; } function setVersion(menu) { menu.header = "PDF version: <b>" + menu.selectedItem.Header + "</b>"; switch (menu.selectedItem.Header) { case "1.3": exportSettings.version = "v1_3"; break; case "1.4": exportSettings.version = "v1_4"; break; case "1.5": exportSettings.version = "v1_5"; break; case "1.6": exportSettings.version = "v1_6"; break; case "1.7": exportSettings.version = "v1_7"; break; case "1.7 ExtensionLevel 3": exportSettings.version = "v1_7Ext3"; break; } } function getPassword(user) { return document.querySelector('#tb' + user + 'Password').value; } function getPermission(permissionName) { if (permissionName == 'Printing') { return exportSettings.permissionPrinting || 'NotAllowed'; } else { return document.querySelector('#cb' + permissionName).checked; } } </script> } <div class="copy well"> <p>@Html.Raw(FlexGridRes.PDFExport_Text0)</p> <p>@Html.Raw(FlexGridRes.PDFExport_Text1)</p> <ul> <li>@Html.Raw(FlexGridRes.PDFExport_Li1)</li> <li>@Html.Raw(FlexGridRes.PDFExport_Li2)</li> <li>@Html.Raw(FlexGridRes.PDFExport_Li3)</li> </ul> <p>@Html.Raw(FlexGridRes.PDFExport_Text2)</p> <ul> <li>@Html.Raw(FlexGridRes.PDFExport_Li4)</li> <li>@Html.Raw(FlexGridRes.PDFExport_Li5)</li> <li>@Html.Raw(FlexGridRes.PDFExport_Li6)</li> </ul> <p>@Html.Raw(FlexGridRes.PDFExport_Text8)</p> </div> <div class="copy well" id="exportSettingsDiv"> <b>@Html.Raw(FlexGridRes.PDFExport_ExportSettings)</b> <c1-menu header="@(FlexGridRes.PDFExport_ScaleMode + " <b>ActualSize</b>")" item-clicked="setScaleMode"> <c1-menu-item header="ActualSize"></c1-menu-item> <c1-menu-item header="PageWidth"></c1-menu-item> <c1-menu-item header="SinglePage"></c1-menu-item> </c1-menu> <c1-menu header="@(FlexGridRes.PDFExport_Orientation + " <b>Portrait</b>")" item-clicked="setOrientation"> <c1-menu-item header="Portrait"></c1-menu-item> <c1-menu-item header="Landscape"></c1-menu-item> </c1-menu> <c1-menu header="@(FlexGridRes.PDFExport_ExportMode + " <b>All</b>")" item-clicked="setExportMode"> <c1-menu-item header="All"></c1-menu-item> <c1-menu-item header="Selection"></c1-menu-item> </c1-menu> <div id="securitySettingsDiv"> <p></p> <b>@Html.Raw(FlexGridRes.PDFExport_SecuritySettings)</b> <div class="panel-body reduce"> <div class="row"> <div class="col-sm-3"> <input type="text" id="tbUserPassword" class="form-control" placeholder="@FlexGridRes.PDFExport_UserPassword" /> </div> <div class="col-sm-3"> <input type="text" id="tbOwnerPassword" class="form-control" placeholder="@FlexGridRes.PDFExport_OwnerPassword" /> </div> </div> <hr /> <div class="row"> <div class="col-sm-3"> <c1-menu header="PDF version: <b>1.3</b>" item-clicked="setVersion"> <c1-menu-item header="1.3"></c1-menu-item> <c1-menu-item header="1.4"></c1-menu-item> <c1-menu-item header="1.5"></c1-menu-item> <c1-menu-item header="1.6"></c1-menu-item> <c1-menu-item header="1.7"></c1-menu-item> <c1-menu-item header="1.7 ExtensionLevel 3"></c1-menu-item> </c1-menu> </div> </div> <div class="panel-body reduce"> <div class="row"> <div class="col"> <b>@FlexGridRes.PDFExport_Permissions</b> (<i>@FlexGridRes.PDFExport_PermissionsNote</i>) </div> </div> <div class="row"> <div class="col-sm-3"> <div class="checkbox"> <label><input type="checkbox" id="cbAnnotating" />@FlexGridRes.PDFExport_Annotating</label> </div> <div class="checkbox"> <label><input type="checkbox" id="cbContentAccessibility" />@FlexGridRes.PDFExport_ContentAccessibility</label> </div> <div class="checkbox"> <label><input type="checkbox" id="cbCopying" />@FlexGridRes.PDFExport_Copying</label> </div> <div class="checkbox"> <label><input type="checkbox" id="cbDocumentAssembly" />@FlexGridRes.PDFExport_DocumentAssembly</label> </div> <div class="checkbox"> <label><input type="checkbox" id="cbFillingForms" />@FlexGridRes.PDFExport_FillingForms</label> </div> <div class="checkbox"> <label><input type="checkbox" id="cbModifying" />@FlexGridRes.PDFExport_Modifying</label> </div> </div> </div> <c1-menu header="@(FlexGridRes.PDFExport_Printing + " <b>NotAllowed</b>")" item-clicked="setPermissionPrinting"> <c1-menu-item header="NotAllowed"></c1-menu-item> <c1-menu-item header="AllowLowResolution"></c1-menu-item> <c1-menu-item header="AllowHighResolution"></c1-menu-item> </c1-menu> </div> </div> </div> </div> <h3>@Html.Raw(FlexGridRes.PDFExport_GroupingSupport)</h3> <p>@Html.Raw(FlexGridRes.PDFExport_Text3)</p> <button class="btn btn-default" onclick="exportGroupedGrid()">@Html.Raw(FlexGridRes.PDFExport_Export)</button> <br /><br /> <c1-flex-grid id="groupingFlexGrid" selection-mode="ListBox" headers-visibility="All" show-groups="true" group-by="Product,Country,Amount" class="grid" allow-merging="All" auto-generate-columns="false"> <c1-flex-grid-column binding="ID"></c1-flex-grid-column> <c1-flex-grid-column binding="Start" header="Start Date" format="d"></c1-flex-grid-column> <c1-flex-grid-column binding="End" header="End Date" format="d"></c1-flex-grid-column> <c1-flex-grid-column binding="Country"></c1-flex-grid-column> <c1-flex-grid-column binding="Product"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount" header="Amount22" format="c" aggregate="Sum"></c1-flex-grid-column> <c1-flex-grid-column binding="Color"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount2" header="Pending" format="c2"></c1-flex-grid-column> <c1-flex-grid-column binding="Discount" format="p1"></c1-flex-grid-column> <c1-flex-grid-column binding="Active"></c1-flex-grid-column> <c1-items-source source-collection="groupedFlexGridData"></c1-items-source> </c1-flex-grid> <br /> <br /> <div> <h3>@Html.Raw(FlexGridRes.PDFExport_MergingSupport)</h3> <p>@Html.Raw(FlexGridRes.PDFExport_Text4)</p> <c1-menu header="@(FlexGridRes.PDFExport_AllowMerging + " <b>All</b>")" display-member-path="Header" command-parameter-path="CommandParameter" item-clicked="onAllowMergingChanged"> <c1-menu-item header="All" command-parameter="AllowMerging.All"></c1-menu-item> <c1-menu-item header="None" command-parameter="AllowMerging.None"></c1-menu-item> <c1-menu-item header="AllHeaders" command-parameter="AllowMerging.AllHeaders"></c1-menu-item> <c1-menu-item header="Cells" command-parameter="AllowMerging.Cells"></c1-menu-item> <c1-menu-item header="ColumnHeaders" command-parameter="AllowMerging.ColumnHeaders"></c1-menu-item> <c1-menu-item header="RowHeaders" command-parameter="AllowMerging.RowHeaders"></c1-menu-item> </c1-menu> <button class="btn btn-default" onclick="exportMergedGrid()">@Html.Raw(FlexGridRes.PDFExport_Export)</button> <br /><br /> <c1-flex-grid id="mergingFlexGrid" allow-dragging="None" allow-merging="All" class="grid" auto-generate-columns="false"> <c1-flex-grid-column binding="ID" align="right"></c1-flex-grid-column> <c1-flex-grid-column binding="Country" allow-merging="true"></c1-flex-grid-column> <c1-flex-grid-column binding="Product" allow-merging="true"></c1-flex-grid-column> <c1-flex-grid-column binding="Color" allow-merging="true"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount" header="Amount22" format="n0" aggregate="Sum" c1-data-type="DataType.Number" align="right"></c1-flex-grid-column> <c1-flex-grid-column binding="Discount" format="p0" aggregate="Avg" c1-data-type="DataType.Number" align="right"></c1-flex-grid-column> <c1-flex-grid-column binding="Active" allow-merging="true" c1-data-type="DataType.Boolean" align="center"></c1-flex-grid-column> <c1-items-source source-collection="mergedFlexGridData"></c1-items-source> </c1-flex-grid> <br /> <br /> <div> <h3>@Html.Raw(FlexGridRes.PDFExport_TreeViewSupportAndFontEmbedding)</h3> <p>@Html.Raw(FlexGridRes.PDFExport_Text5)</p> <p>@Html.Raw(FlexGridRes.PDFExport_Text6)</p> <p>@Html.Raw(FlexGridRes.PDFExport_Text7)</p> <ul> <li>@Html.Raw(FlexGridRes.PDFExport_Li7)</li> <li>@Html.Raw(FlexGridRes.PDFExport_Li8)</li> </ul> <button class="btn btn-default" onclick="exportTreeViewGrid()">@Html.Raw(FlexGridRes.PDFExport_Export)</button> <br /><br /> <c1-flex-grid id="treeFlexGrid" class="grid custom-flex-grid" child-items-path="Children" auto-generate-columns="false" allow-resizing="None" headers-visibility="Column" selection-mode="ListBox"> <c1-items-source source-collection="treeViewData"></c1-items-source> <c1-flex-grid-column binding="Header" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Size" width="80"></c1-flex-grid-column> </c1-flex-grid> </div> </div>