We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Asp.net core web api silent printing with syncfusion.pdfviewer.windows

Dear Team.
I am trying to do a silent printing in asp.net core web api. I have reference the necessary assemblies, i.e system.windows.form from "C:\Windows\Microsoft.NET\Framework64\v4.0.30319". find below code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using EventWebApi;
using EventWebApi.Models;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.IO;
using Syncfusion.Drawing;
using System.Drawing;
using Syncfusion.Windows.Forms.PdfViewer;
using Syncfusion.Pdf.Parsing;
using System.Windows.Forms;
namespace PrintWebApi.Controllers
{
public async Task CreatePrintPDF()
{
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, 12, PdfFontStyle.Bold);
//Draw the text.
graphics.DrawString(participant.FullName.ToUpper(), font, PdfBrushes.Black, new RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the PDF document to stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
//Close the document.
document.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "NameTag.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
PdfViewerControl viewer = new PdfViewerControl(); error at this point : find error below.
viewer.Load(stream);
PrintDialog dialog = new PrintDialog();
dialog.Document = viewer.PrintDocument;
dialog.Document.Print();
return Ok();
}
}
error after executing the api.


9 Replies

DG Deepak Gunasekaran Syncfusion Team January 17, 2020 03:48 PM UTC

Hi Ali, 

Based on the exception details, we could see that the Accessibiity.dll is missing to be referred in the application which caused the issue. Kindly ensure whether the Accessibility.dll available in the location “C:\Windows\Microsoft.NET\Framework64\v4.0.30319” and it is referred properly in the project. You may refer the framework dependencies directly or install via nuget packages to avoid the kind of errors in the application.   

We have created a workable sample to perform silent printing with your code snippet in the ASP.Net core web API using Syncfusion.PdfViewer.Windows and it can be downloaded from the below location. 
 
Please try the sample and let us know whether it meets your requirement. 
 
Regards, 
Deepak G 



AL Ali January 18, 2020 01:48 PM UTC

Dear Deepak.

Thank you for the solution. Everything is working perfectly now.

However, I want to set the printer page size and orientation, since the page size I set when creating the pdf is different from the print out.

Regards.


US Uthandaraja Selva Sundara Kani Syncfusion Team January 20, 2020 12:53 PM UTC

Hi Ali, 
 
Currently we are checking on this and we will update you shortly. 
 
Regards, 
Uthandaraja S


US Uthandaraja Selva Sundara Kani Syncfusion Team January 20, 2020 02:20 PM UTC

Hi Ali, 
 
Thanks for your patience. 
 
We can achieve the requirement of customize the page orientation and size using the PrintDocument.DefaultPageSettings API. Please find the code snippet below to customize the page orientation of printing document, 
 
 
PrintDialog dialog = new PrintDialog(); 
 
dialog.Document = pdfViewerControl.PrintDocument; 
 
//Set the page orientation. 
dialog.Document.DefaultPageSettings.Landscape = true; 
 
 
Please let us know if you need further assistance. 
 
Regards, 
Uthandaraja S 



LO Lorne December 23, 2020 10:31 AM UTC

I have downloaded and run the example application from the link provided above.  It works as advertised.  However when I implement the code in my own .NET Core application, and run it from my hosted site rather than locally, it no longer works. I get the following error.  Again my own application runs fine locally, but not hosted.  Is there any recommendations for this?




DG Deepak Gunasekaran Syncfusion Team December 24, 2020 01:41 PM UTC

Hi Lorne, 
 
Based on the error details, we suspect that this error occurred due to the environment details isn’t set which might have disabled most debugging features. Can you please follow the steps provided in the following page to set the environment variable and let us know whether it resolves the issue? 
If you still face any issue, please share us the details along with the host details in which you are deploying (Azure or AWS). It will be helpful for us to analyze more and assist you better. 
 
Regards, 
Deepak G 



LO Lorne December 26, 2020 11:22 AM UTC

So if I enable debugging in the production (hosted) environment, I get the following error:  I am using SmarterASP.Net for hosting this site.



DD Divya Dhayalan Syncfusion Team December 28, 2020 05:35 PM UTC

Hi Lorne, 
 
Thanks for the stack trace details. 
 
We are currently trying to reproduce the reported issue with the given environment information. We will update you the further details on 30th December 2020. 
 
Regards, 
Divya 



DD Divya Dhayalan Syncfusion Team December 30, 2020 06:41 PM UTC

Hi Lorne, 
 
On analyzed further, from the given stack trace we suspect the issue might because of printer not available in the same network. Please check the below forum for more detail. 
 
Please kindly confirm whether the printer is installed in the same network as the server and has been set as the default printer. 
This would help us to analyze further on this. 
 
Regards, 
Divya 


Loader.
Live Chat Icon For mobile
Up arrow icon