Print PDF To Printer Programmatically C# - c#

I have an application that writes SSRS reports as a PDF to a file directory and I would like each time a report is added to the folder for it to be printed to a specific network printer. The reports are generated using the SQL SSRS web service.
This folder and application is on a server and I cannot use Adobes silent printing to accomplish this. Does anyone have any advice?
Thanks.

You could try sending your document as raw or you might be able to convert your file to a stream and send it to your printer using TcpClient.

Realized I left this question unanswered and wanted to give a followup in case anyone else encounters this problem. I ended up using the code from the answer found here...
Print a ReportViewer Without Preview
I was able to create the reports using my web service and put them into a report viewer and then simply pass the report and the printer I wanted to print to as parameters to the code above and it handled the printing for me. The only thing I did was extend the functionality to accept a printer name as a parameter and use assign that as the specified printer that I wanted to print to.
Here is some sample code in case anyone wants to see the general flow I used.
List<ReportParameter> reportparms = new List<ReportParameter>();
ServerReport rpt = new ServerReport();
reportparms.Add(new ReportParameter("param1", param1));
rpt.ReportServerUrl = reportserver;
rpt.ReportPath = myReportPath;
rpt.SetParameters(reportparms);
//I created a class "ReportPrintDocument" for the code from the question linked above.
ReportPrintDocument rdp = new ReportPrintDocument(rpt, myPrinter);
rdp.PrinterSettings.PrinterName = ps.Printer;
if (p.PrinterSettings.IsValid)
{
rdp.Print();
}
There is some other logic here and there but that is the basic idea that will get the job done.

Related

C# Windows Service > Render Web Page and Output to PDF

I am at a loss and could use some direction. I have a windows service that performs an Audit on customers. For new customers, I need to create a profile for each one. I already have a ASP.net C# web page that displays a single customer profile for the user:
http://webserver/showprofile.aspx?id=CustomerID
I would like to run some type of loop in the service that would render each new customer's profile and output all of those profiles into a PDF, Word Document, etc. Is there an easy way to do this using the existing Show Profile webpage?
If not, what is the best way to do this in C#? If it requires a component, I would prefer something free to very inexpensive. I currently have licenses for Telerik's complete line of tools if there is something there that can help. The bottom line, this has to be done programmatically as the user will have nothing to do with the generation/export to PDF. They will only access the resulting exported file.
Thanks in advance for your help.
You can use PdfCreator using the following command:
private PDFCreator.clsPDFCreator printer;
printer = new PDFCreator.clsPDFCreator();
printer.cDefaultPrinter = "PDFCreator";
printer.cOptions.UseAutosave = 0;
// Format in which file is to be saved. 0 if for pdf.
printer.cOptions.AutosaveFormat = 0;
printer.cClearCache();
printer.cStart();
foreach(int CustomerId in CustomerIDs)//array of customer ids as an example
{
printer.cPrintURL("http://webserver/showprofile.aspx?id=" + CustomerID.ToString());
}
You can download the software and necessary dll at the following link and look at the examples in the .net folder.
http://sourceforge.net/projects/pdfcreator/

PDF Printing happens in random order

I am using Foxit Reader (a PDF Reader) and passing command line arguments to print a pdf pro-grammatically. I understand that we cannot specify the number of copies through command line as from this discussion.
I am developing a win-forms desktop application and for printing multiple copies of PDF document I am using the below code
string foxitReaderInstalledPath = GetFoxitReaderInstalledPath();
while (noOfCopies > 0)
{
Process process = new System.Diagnostics.Process();
process.EnableRaisingEvents = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = foxitReaderInstalledPath;
string arguments = String.Format(#"-t ""{0}"" ""{1}""", this.Path, printerName);
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit();
noOfCopies = noOfCopies - 1;
}
Issue happens when multiple users are giving muliple copies of print to the same printer. The issue is the printed documents gets mixed up in the order in which they get printed. Anyone please let me know how I can avoid this issue?
Many thanks.
You can't avoid this client-side...
IF you really want to avoid it the "client" app which is used by the users has to just send the file to some "centralized server process" with all relevant params... this "centralized process" can then "serialize" the printing so it occurs in correct order...
BUT if the printer you are printing to is accessible from the users systems then it could still happen that a user sends something to the printer (like an image or word document...) which will be printed and disturbing the order a bit...
I think it would make much more sense if you described what your goal is... perhaps there is some better way to solve all this... are you implementing a print server ?
EDIT - as per comment:
Put the location for the PDF files on a network share... and run your printing code on the same machine which provides the share... ideally the printer is directly connected to that machine... this should provide enough performance and since it is only one central application accessing the printer it should work fine...
I would strongly recommend the use of a PDF library or Acrobat reader so that the printing can use a parameter for NumberOfCopies !
EDIT 2 - as per comment:
Some PDF Libraries:
.NET library to print PDF files
http://www.gnostice.com/PDFOne_dot_NET.asp
http://www.gdpicture.com/products/dotnet/plugins/pdf-plugin.php
http://itextpdf.com/

Crystall Report not working at server

I am using crystal report for printing . But after hosting in the server i got a page with following url while printing from a client machine
http://192.168.50.104/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer4/html/crystalexportdialog.htm
and shows
Page not found
error
It is displayed in the report viewer, but the problem comes after clicking the print button of crystal report.
I am using the following code to bind report
CrystalReportViewer1.ReportSource = CrystalReport;
CrystalReportViewer1.DataBind();
If anybody knows the solution please help me.... Thanks in advance....
On a similar problem, this is what I did
This happens because crystal repots viewer assumes a very specific directory structure for the images and scripts. If you are running IIS on your development machine, you can find the structure here "C:\inetpub\wwwroot\aspnet_client\System_Web\2_0_50727\CrystalReportWebFormViewer4"
Now all you have to do is that copy the content of this whole structure, right from aspnet_client to your webserver and create a virtual directory to point to this path. Even if you dont want to create a virtual directory or dont have access to do so, you can simply copy the full structure to the root of your webapp and that should do the magic.
If you are not using IIS and using a inbuilt webserver with Visual studio (cassini i.e.), still you will have this structure , only thing that in this case it would be available at C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ASP.NETClientFiles

C# PrintDialog.PrinterSettings.CanDuplex Reports Wrongly

I'm trying to setup some code to print to different trays on a photo copier depending on what the document is (different sizes, paper colours...). It is one particular type of copier so I am not too worried about the code working in other scenarios. I still want to show the print dialog, just with the settings having better defaults for each document.
I have managed to setup the majority of what I want using properties in
PrintDialog.PrinterSettings.
However on trying to set the duplexing using
PrintDialog.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Vertical;
It fails, remaining the same as it was before. If I check if duplex is supported using
PrintDialog.PrinterSettings.CanDuplex;
It returns false which is not the case I can change it on the dialog and it prints fine. Has anyone else had this problem? Is there a work around? Perhaps something involving COM (please be gentle not used interop code much)
It's a Gestetner 2212 copier and I believe the print server is a Windows Server 2008 machine.
Edit:
I found this link
http://bytes.com/topic/c-sharp/answers/238860-using-setprinter-c-set-duplex-option-print-prefs
Which seems to be a similar problem it seems to be some kind of problem related to using a networked printer and trying to set duplex. However the link doesn't post the solution it was emailed to them (I hate it when people do that). Anyone know how I can set the duplexing using COM interop code.
Seems network printers duplex property cannot be set in .NET code easily, even when it says it has changed the property it doesn't output correctly. There is a way to do it using com interop but it still requires modifying security levels for the printer so is more hassle than it is worth.
Try implementing a handler for the PrintPage event with following code:
if (e.PageSettings.PrinterSettings.IsValid && e.PageSettings.PrinterSettings.CanDuplex)
e.PageSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Vertical;
Tried it with a HP Laserjet M3035. It didn't show in the PrintDialog window but printed the document in duplex anyway.

How to printing crystal report directly to network printer in Vb.net or C#.net in Windows Applications

I have written the following code as,
Dim report As New ReportDocument
report.PrintOptions.PrinterName = "\\network\printer"
report.Load(CrystalReportName.rpt, OpenReportMethod.OpenReportByDefault)
report.PrintToPrinter(1, False, 0, 0)
when i am trying to run this code , it shows the error message as "Invalid Printer Specified". If i give the local printer name, it is working fine. But i can't able to print the crystal report directly to the network printer. Kind help needed. Thanks in advance.
Sivakumar.P
Use this code to know the installed printers
Imports System.Drawing
Imports System.Drawing.Printing
and this code on the load function... you will fill a combobox with the printers and their names correctly, and then use your code
For Each Printer In PrinterSettings.InstalledPrinters
cmbPrinters.Items.Add(Printer)
Next
In case anyone else still has this problem:
In the crystal report document you are trying to print, go to:
Design --> Page Setup
A default printer not installed on your server side may have been selected. Remove or replace it and save the document. Then run your .Net code to set the printer dynamically if you want.
User 4 backslashes i.e \\ before Network
and 2 backslashes i.e \ before printername
\\\\[Network Address]\\[printer name]
May be the printer name is wrong.
Please use the following code to debug what name is coming while choosing the network printer
http://www.codeproject.com/KB/printing/printtoprinter.aspx
and then assign proper name.
Still if incase it did not work out, there might be a permission issue then look at
http://forums.asp.net/t/1383129.aspx
Best of luck,.
For anyone having a similar problem this can be a solution:
report.PrintOptions.PrinterName = "\\\\\\\\network\\\\printer"
In ASP.Net, a simple and sweet solution consists in install same printer drivers(like hp or zebraa) including version in both server and clinet meachine it will work with #\\\ipaddress\printername.

Categories