Category / Section
How to preserve the form fields when silent printing the PDF document?
1 min read
PdfViewerControl does not preserve form field data when silent printing the PDF document. However, these details can be preserved by flattening the form fields before silent printing the PDF document.
Refer to the following code snippet.
//Load the template document PdfLoadedDocument doc = new PdfLoadedDocument(@"..\..\Data\FormFillingDocument.pdf"); PdfLoadedForm form = doc.Form; //Fill the fields from the first page (form.Fields["Name"] as PdfLoadedTextBoxField).Text = "xxx"; (form.Fields["Email"] as PdfLoadedTextBoxField).Text = "yyy"; //Flatten the form fields doc.Form.Flatten = true; MemoryStream stream = new MemoryStream(); //Save the changes doc.Save(stream); PdfViewerControl viewer = new PdfViewerControl(); viewer.Load(stream); //Initialize print dialog. PrintDialog dialog = new PrintDialog(); //Get the list of installed printers in a list. List<string> printersList = new List<string>(); foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { printersList.Add(printer); } dialog.AllowPrintToFile = true; dialog.AllowSomePages = true; dialog.AllowCurrentPage = true; dialog.Document = viewer.PrintDocument; //Assign any of the printers you wish to print to the printer settings dialog.Document.PrinterSettings.PrinterName = printersList[1]; dialog.Document.Print();