Printing
Users print a document from the toolbar of the Document Viewer, and your code can start printing from buttons on your own page. For browsers that don't support printing from the viewer, your page can offer the document as a PDF file instead.
The examples change the view of the ASP.NET Core tutorial.
How Printing Works
The toolbar of the viewer has two buttons for getting a document out of the viewer:
- Print prints the document with the print function of the browser. The browser shows its print dialog.
- Download downloads the document as a PDF file, which users open and print with any application.
Not all browsers support printing from the viewer. For users of such browsers, offer the download.
Print and Download From Code
printDocument and downloadDocument do the same as the two buttons. The following view adds a Print and a Download PDF button below the viewer. Replace the content of Views/Home/Index.cshtml:
@using TXTextControl.Web.MVC.DocumentViewer
@using System.Text
<div style="width: 800px; height: 600px;">
@Html.TXTextControl().DocumentViewer(settings => {
settings.DocumentData = Convert.ToBase64String(
Encoding.ASCII.GetBytes("<strong>Sample Content</strong>"));
settings.Dock = DocumentViewerSettings.DockStyle.Fill;
}).Render()
</div>
<input type="button" id="printButton" value="Print" disabled />
<input type="button" id="downloadButton" value="Download PDF" disabled />
<script>
// TXDocumentViewer exists only after this event
window.addEventListener("documentViewerLoaded", function () {
var printButton = document.getElementById("printButton");
var downloadButton = document.getElementById("downloadButton");
printButton.disabled = false;
downloadButton.disabled = false;
// Uses the print function of the browser; not all browsers support it
printButton.addEventListener("click", function () {
TXDocumentViewer.printDocument();
});
// A PDF file that users can print with any application
downloadButton.addEventListener("click", function () {
TXDocumentViewer.downloadDocument();
});
});
</script>
The code works as follows:
- The buttons are disabled until the viewer has raised
documentViewerLoaded, because TXDocumentViewer doesn't exist before. printDocumentprints the content of the document with the print function of the browser. It takes no parameters.downloadDocumentdownloads the current document as a PDF file. It takes no parameters either.
Run the application and click Print: the print dialog of the browser opens with the text "Sample Content".
Both methods return nothing, and the viewer raises no events for printing. Your code can't find out whether the user printed the document or canceled the dialog.
To create a PDF document that contains the values users entered into form fields or the annotations they added, save the document with saveDocument instead, as described in Saving Documents.
The Formatting Printer
DocumentViewerSettings.FormattingPrinter sets the name of a printer whose text dimensions format the document, so that the displayed and the printed document have the same dimensions when the document is printed with this printer:
"Standard"uses the installed standard printer of the system."Display"uses the font dimensions of the display.- If the named printer isn't installed, the standard printer is used.
@Html.TXTextControl().DocumentViewer(settings => {
settings.DocumentPath = "App_Data/template.docx";
settings.Dock = DocumentViewerSettings.DockStyle.Fill;
settings.FormattingPrinter = "Display";
}).Render()
The reference doesn't state whether this setting has an effect with the Core engine. For the Document Editor, printer-specific formatting isn't supported by its cross-platform backend; see Viewing and Printing.
Next Steps
- Saving Documents: save the document with form values and annotations.
- User Interface: show, hide and disable the toolbar.
- Annotations: let users comment documents before they print them.