Issue
Customer wants to print pdf files being exported from server side automatically on client printer
Solution
- Function of exporting pdf file is done from server side, so a web application communicating with server side is needed
- Function of printing pdf files automatically on printer can be done by using .NET library on client Windows, so a Windows application communicating with .NET library on client Windows is needed
- WPF (Windows Presentation Foundation) can do this. WPF is like a browser that can loads not Web application but also can run server code by itself to communicate with .NET library
Work flow
- WPF calls a Web application of exporting pdf file from server
- WPF downloads the pdf file into client PC
- WPF calls .NET library on client Windows PC to print the pdf
Techniques
- Create a Web application exporting pdf file
- Create WPF execution file
- Create function of printing pdf file in WPF
Create function of printing pdf file in WPF
Install pdfium library
- Install PdfiumViewer
- Install PDFium.Windows to get pdfium_x86.dll and pdfium_x64.dll file
- Rename pdfium_x86.dll into pdfium.dll and put it into same folder with WPF exe file
[sql]
Install-Package PdfiumViewer -Version 2.13.0
[/sql]
[sql]
Install-Package PDFium.Windows -Version 1.0.0
[/sql]
C# code
[js]
private bool StartPrintPDF(
string printer,
string paperName, // A4
string filename, // C:\tempFolder\abc.pdf
int copies)
{
try
{
// Create the printer settings for our printer
var printerSettings = new PrinterSettings
{
PrinterName = printer,
Copies = (short)copies,
};
// Create our page settings for the paper size selected
var pageSettings = new PageSettings(printerSettings)
{
Margins = new Margins(0, 0, 0, 0),
};
foreach (PaperSize paperSize in printerSettings.PaperSizes)
{
if (paperSize.PaperName == paperName)
{
pageSettings.PaperSize = paperSize;
break;
}
}
// Now print the PDF document
using (var document = PdfDocument.Load(filename))
{
using (var printDocument = document.CreatePrintDocument())
{
printDocument.PrinterSettings = printerSettings;
printDocument.DefaultPageSettings = pageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.Print();
}
}
return true;
}
catch
{
return false;
}
}
[/js]
Leave a Reply