Category / Section
How to silent print the PDF document from WCF service
1 min read
You can silent print the PDF document sent from the WCF service using PDF viewer Windows forms (Syncfusion.PdfViewer.Windows ) reference in web project. Refer to the following code.
public object CreatePDF()
{
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//Set the standard font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Save the document
MemoryStream ms = new MemoryStream();
document.Save(ms);
string base64 = Convert.ToBase64String(ms.ToArray());
return base64;
}
Code to send data to silent print the PDF document from client side of the web project.
$.ajax({
url: "http://localhost:12263/pdfviewer.asmx/CreatePDF",
type: 'POST',
crossDomain: true,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
traditional: true,
success: function (data) {
var jsonResult = new Object();
jsonResult["base64"] = data["d"];
$.ajax({
url: "../api/PdfViewer/PdfViewerPrint",
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(jsonResult),
crossDomain: true,
traditional: true,
success: function () {
var div = document.getElementById('result');
div.innerHTML += 'PDF document is printed';
}
});
}
});
Code to silent print the PDF document using windows reference in Web API controller.
public void PdfViewerPrint(Dictionary<string, string> jsonResult)
{
//Initializes the PDF viewer
PdfViewerControl viewer = new PdfViewerControl();
//Loading the document in PDF viewer control
if (jsonResult.ContainsKey("base64"))
{
var data = jsonResult["base64"];
byte[] byteArray = Convert.FromBase64String(data);
MemoryStream stream = new MemoryStream(byteArray);
viewer.Load(stream);
//Initializes the print dialog box
PrintDialog dialog = new PrintDialog();
dialog.AllowPrintToFile = true;
//Sets the PDF viewer print document to PrintDialog's document
dialog.Document = viewer.PrintDocument;
//Prints the PDF document
dialog.Document.Print();
}
}
WCF Service: http://www.syncfusion.com/downloads/support/directtrac/general/ze/WCFService-925629626 JavaScript sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfViewerSample_SilentPrinting1493644954
Note:
- Run the WCF service or host the service in IIS and provide the URL of the service to Ajax request URL.
- Then, run the web project to silent print the PDF document.