Direct to printer freezes page

I am using a .doc file as a template and populating merge fields in it to generate my document.
I’m using the latest version of Aspose.Words.
What I’m trying to achieve is the auto printing of a pdf when my code is finished populating all of the merge fields.
Currently I use the line of code:

resultDoc.Save("test.pdf", Aspose.Words.SaveFormat.Pdf, Aspose.Words.SaveType.OpenInBrowser, Response);

in order to load it as a PDF in the browser.
What I want to do now instead is have the generated PDF go directly to the printer.
I’ve tried things like
resultDoc.Print(), but I find the browser page loads blank and just freezes and does nothing.
I’ve alos tried

Aspose.Words.Rendering.AsposeWordsPrintDocument abc = new Aspose.Words.Rendering.AsposeWordsPrintDocument(resultDoc);
abc.Print();

but again it just freezes.
I would have expected it to go directly to the printer, but I see nothing happening.
What am I doing wrong?
thanks.

Hi

Thanks for your request. Document.Print() method will print document on the server side. So, make sure the printer on your server is properly installed and accessible.
Note, there is no way to print the document on client side, since you have no access to the client’s printers.
Best regards.

I’m running it localhost right now, so I should have access to the printer without issues (as I can print no problem in other programs).
This is essentially my page load:

Document resultDoc = new Document();

… build up document…

resultDoc.FirstSection.Remove();
resultDoc.UpdateTableLayout();
resultDoc.Print();

But I just get a frozen browser window instead of anything printing (I’m monitoring the print queue).
What else can I test? I don’t get why (when debugging) it hits the “.Print()” line, but then it just locks up.

I’ve got the “freezing” problem to go away by testing it out on .net3.5 instead of the .net1.1 I was using before.
So now I so something like:

resultDoc.Print("Dell Laser Printer 5310n");

But I am getting the error
Settings to access printer ‘Dell Laser Printer 5310n’ are not valid.
What settings do I need to check/change now that I got beyond the original problem? I’m ok if this only works in .net3.5 as we are upgrading our systems currently.

Hi

Thanks for your request. If you are developing Web application, I think, you should make sure that ASPNET (IUSR) user, which runs Web applications, has access to the printer.
Best regards.

The Website is running under the domain admin account for testing.
I’ve also set the on in the web.config file but I still get the same “settings no valid” error.
How else can I test that it has access to the printer?

Hi

Thanks for your request. Please try to create simple console application, which will print a Word document using Aspose.Words (just for test purposes). Use code like the following:

Document doc = new Document(@"C:\Temp\test.doc");
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.PrinterName = @"hp LaserJet 1010 Series Driver";
doc.Print(printerSettings);

Also, you can try specify another printer, for example you can try using Image Writer

Document doc = new Document(@"C:\Temp\test.doc");
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.PrinterName = "Microsoft Office Document Image Writer";
printerSettings.PrintToFile = true;
printerSettings.PrintFileName = @"C:\Temp\out.tif";
doc.Print(printerSettings);

Best regards.

In my web app, I’ve tried using the image writer and that worked out perfectly…it created the file on my C drive.
However, even after trying more than one network printer, I get settings not valid error.
Correct me if I’m wrong, but I’ve set the user for the IIS website to be the domain administrator account (to eliminate potential security problems during testing), but I still get the message. Do I really care about the ASPNET (IUSR) user account if I have the website running under the domain admin?
Is there another web based test I could test to see if I can access the printer?
Thanks.

Hi

Have you tried to allow ASPNET (IUSR) user to access your network printer? Maybe you should try impersonation. Here is nice article about that:
https://weblog.west-wind.com/posts/2005/Feb/24/Using-programmatic-Impersonation-from-an-ASPNET-Page
Best regards.

I am using impersonation (via my web.config) already.
As a side note, when I use the following code, I don’t see any network printers listed (only local printers). This seems to be the problem. Not sure why (when using impersonation) it won’t see the network printers.

System.Drawing.Printing.PrintingPermission PrintingPermission = new System.Drawing.Printing.PrintingPermission(System.Security.Permissions.PermissionState.Unrestricted); 

PrintingPermission.Level = System.Drawing.Printing.PrintingPermissionLevel.AllPrinting;
foreach(string printerStr in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
    Response.Write("Printer: " +printerStr);
}

New update.
I am now able to get the network printers to show up in my response.write code as written in my last post.
I did this by making sure to impersonate the windows account where I installed the printers (i.e. myusername, and not the admin account).
So, I can see the network printers now, but I STILL get the settings not valid error.
Any other help?

Hi

Maybe you can try the solution described here:
https://social.msdn.microsoft.com/Forums/en-us/325206c1-66a3-4fea-8fb9-3d3e6374f1ab/settings-to-access-printer-herculesprtmark01-are-not-valid
Best regards.

I tried this:

using (System.Security.Principal.WindowsImpersonationContext wic = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero))
{
    resultDoc.Print("Dell Laser Printer 5310n");
}

However, still the same error message. Like I said before, I can now see the network printer when I check all installed printers using impersonation, but cannot get to print to it.

Hi

Unfortunately, I have no more ideas what the problem is. Does this code work on your side?

PrintDocument doc = new PrintDocument();
doc.PrinterSettings.PrinterName = @"\\192.168.0.2\hp LaserJet 1010 Series Driver";
doc.PrintPage += Doc_PrintPage;
doc.Print();
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.DrawImage(Image.FromFile(@"Common\test.jpg"), e.MarginBounds.Left, e.MarginBounds.Top);
}

This code just prints an image.
Best regards.

I FINALLY got it!
It was the stupid server name that I was missing from the printer name
I added "networkservername\ before the printer and I heard the sweet noise of the printer firing up.
Thanks for you r help.

A post was split to a new topic: Problem printing documents