This days I've been developing a service that sends a PDF file to a network printer and effectively print it using C# and the GhostscriptProcessor library.
But now I'm really stuck with the next (and last) step I want to make. I need to know wether or not the file was really printed. I tried everything I could (for instance I tried implementing this powershell script but I'm not familiar at all with powershell and I got depressed since I got too many errors I can't solve) but I can't find the answer.
Is there ANY way using C# (any library) to retrieve if a document has been printed. Or retrieve the whole log of documents printed? Any script I can call through C# (or not, I am able to circumvent that) that tells me the information I need?
I'd like to add that I have access to the printer using the System.Drawing.Printing library as follows:
var printServer = new PrintServer();
var myPrintQueues = printServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
foreach (PrintQueue pq in myPrintQueues)
{
pq.Refresh();
string printerName = "ES7470 MFP(PCL)";
if (!pq.Name.ToUpper().Contains(printerName.ToUpper())) break;
PrintJobInfoCollection jobs = pq.GetPrintJobInfoCollection();
//And here I can use pq or jobs but I can't retrieve the log at all.
}
I think the problem is the PrintServer, I'm not in your environment so I cant tell your setup but LocalPrintServer.GetPrintQueue should do the trick.
string printerName = "ES7470 MFP(PCL)";
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueueCollection printQueues = localPrintServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
if (printQueues == null) return false;
PrintQueue queue = printQueues.Where(x => x.Name.Equals(printerName)).FirstOrDefault();
I'm guessing your application will know if someone printed something, otherwise you'll have to poll the Printer Queue each second or two to find out if a job went to the printer...
Related
After struggling whole day, I identified the issue but this didn't solve my problem.
On short:
I need to open a PDF, convert to BW (grayscale), search some words and insert some notes nearby found words. At a first look it seems easy but I discovered how hard PDF files are processed (having no "words" concepts and so on).
Now the first task, converting to grayscale just drove me crazy. I didn't find a working solution either commercial or free. I came up with this solution:
open the PDF
print with windows drivers, some free PDF printers
This is quite ugly since I will force the C# users to install such 3'rd party SW but.. that is fpr the moment. I tested FreePDF, CutePDF and PDFCreator. All of them are working "stand alone" as expected.
Now when I tried to print from C#, obviously, I don't want the print dialog, just select BW option and print (aka. convert)
The following code just uses a PDF library, shown for clarity only.
Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer();
viewer.BindPdf(txtPDF.Text);
viewer.PrintAsGrayscale = true;
//viewer.RenderingOptions = new RenderingOptions { UseNewImagingEngine = true };
//Set attributes for printing
//viewer.AutoResize = true; //Print the file with adjusted size
//viewer.AutoRotate = true; //Print the file with adjusted rotation
viewer.PrintPageDialog = true; //Do not produce the page number dialog when printing
////PrinterJob printJob = PrinterJob.getPrinterJob();
//Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
//System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument();
//prtdoc.PrinterSettings = ps;
//Set printer name
//ps.PrinterName = prtdoc.PrinterSettings.PrinterName;
ps.PrinterName = "CutePDF Writer";
ps.PrintToFile = true;
ps.PrintFileName = #"test.pdf";
//
//ps.
//Set PageSize (if required)
//pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
//Set PageMargins (if required)
//pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
//Print document using printer and page settings
viewer.PrintDocumentWithSettings(ps);
//viewer.PrintDocument();
//Close the PDF file after priting
What I discovered and seems to be little explained, is that if you select
ps.PrintToFile = true;
no matter C# PDF library or PDF printer driver, Windows will just skip the PDF drivers and instead of PDF files will output PS (postscript) ones which obviously, will not be recognized by Adobe Reader.
Now the question (and I am positive that others who may want to print PDFs from C# may be encountered) is how to print to CutePDF for example and still suppress any filename dialog?
In other words, just print silently with programmatically selected filename from C# application. Or somehow convince "print to file" to go through PDF driver, not Windows default PS driver.
Thanks very much for any hints.
I solved conversion to grayscale with a commercial component with this post and I also posted there my complete solution, in care anyone will struggle like me.
Converting PDF to Grayscale pdf using ABC PDF
I have a windows service that prints pdf files once it receives a request. It uses PrintDocument, but for some printers it seems to ignore printer and tray settings I give it in my code.
Currently every printer has a few trays which are all installed as seperate queues. For some printers I can just set the PrinterName property (of PrintDocument) to the name of the queue and it works fine. However a couple of printers seem to ignore this. I also tried setting the papersource, but this seems to alway be ignored.
Here's the code used to print:
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printer; //printer is send to the service along with the request
pd.Print();
Again: this works for some printers, but not for all.
I also tried using Papersource like this:
pd.PrinterSettings.DefaultPageSettings.PaperSource.SourceName =
pd.PrinterSettings.PaperSources[tray - 1].SourceName; //Tray is also send with the request
and like this:
pd.PrinterSettings.DefaultPageSettings.PaperSource =
pd.PrinterSettings.PaperSources[tray - 1];
What am I doing wrong here?
EDIT: The pdf file always has content in it, so it can't be empty.
I changed my code to use PrintQueue instead (https://msdn.microsoft.com/en-us/library/system.printing.printqueue(v=vs.110).aspx). This seems to work well, since I can directly call the queues instead of the printer.
My Requirement is to print invoices in pdf direct to local printer from web application developed in .net mvc framework.
I need to do exact like shipstation is doing with SHIPSTATION CONNECT
SHIPSTATION CONNECT
Does it use process like
REMOTE PRINTER SHARING CODEPROJECT
or using WMI library to share printer remotely.
Any expert thought will help me and my programmer to build the solution.I am not expecting code or spoon feeding but like to know the process and way to start on this in right direction.
Thanks in advance for the help!
regards
check printnode.com might be of some help.Seems like doing same thing what you want.The service is not free chargeable or alternatively you can build same using google cloud print.
you can write javascript function that print from local printer,
w=window.open();
w.document.open();
w.document.write("<html><head></head><body>");
w.document.write("HI");
w.document.write("</body></html>");
w.document.close();
w.print();
w.close();
working example:
http://jsfiddle.net/xwgq5ap4/
if you want to print from the server you need to send a request for the server for example :
www.mysite.com/print.aspx?file=invoice.pdf
to print it by the server you have 2 solutions the first is calling to other process to accomplish it like you can see in this answer:
Print Pdf in C#
the second is write your own implementation using PrintDocument namespace for example:
namespace PrintPDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
//Use the default printer to print all the pages
//doc.PrintDocument.Print();
//Set the printer and select the pages you want to print
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
PrintDocument printDoc = doc.PrintDocument;
dialogPrint.Document = printDoc;
printDoc.Print();
}
}
}
}
original taken from free 3rd party library
I am using iTextSharp for reading and managing PDF documents. Things such as stamping overlays for the background or logos and backers. The PDF's are statement files, so I cannot give an example. I am wondering how to view the settings of the PDF to see if the PDF file is Simplex or Duplex, and that sort of information. Any help or suggestions would be appreciated. At the moment I test for certain criteria of second page, and this is a poor and bad way to do this. Thanks in advance, and happy coding!
The duplex mode is stored in the document's /ViewerPreferences dictionary under the /Duplex key. It supports three values, /DuplexFlipLongEdge, /DuplexFlipShortEdge, and /Simplex. You can use the code below to inspect this:
//Assume false by default since this was introduced in PDF 1.7
Boolean isDuplex = false;
//Bind a reader to our file
using (var r = new PdfReader(testFile)) {
//Get the view preferences
var prefs = r.Catalog.GetAsDict(PdfName.VIEWERPREFERENCES);
//Make sure we found something
if (prefs != null) {
//Get the duplex key
var duplex = prefs.Get(PdfName.DUPLEX);
//Make sure we got something and it is one of the duplex modes
isDuplex = (duplex != null && (duplex.Equals(PdfName.DUPLEXFLIPLONGEDGE) || duplex.Equals(PdfName.DUPLEXFLIPSHORTEDGE)));
}
}
I know its 2 years later but I just spent hours searching, found this... but eventually found...
I create a button that runs this script (that pops up the printer dialogue with duplex pre selected if available... note that selecting another printer erases this pre selection.. also change "Long" for "Short" if you flip that way... q8)
var pp = this.getPrintParams();
pp.DuplexType = pp.constants.duplexTypes.DuplexFlipLongEdge;
this.print(pp);
I have a MonoTouch application that generates some PDFs locally and then prints them to a network printer. To get this working I initially just added a PDF resource to the project that I could try and print but am having a heck of a time. When I print just HMTL or a string value everything is good but printing the PDF has me lost. When debugging it looks like the app is getting the proper URL.
Any help would be greatly appreciated and my sampled code is below:
public void PrintSomePDF ()
{
var printInfo = UIPrintInfo.PrintInfo;
printInfo.OutputType = UIPrintInfoOutputType.General;
printInfo.JobName = "Test: PDF Print";
string pdfFileName = "printthispdf_01.pdf";
NSUrl url = NSUrl.FromFilename(pdfFileName);
var printer = UIPrintInteractionController.SharedPrintController;
printer.PrintInfo = printInfo;
printer.PrintingItem = url.Path;
printer.ShowsPageRange = true;
printer.Present (true, (handler, completed, err) => {
if (!completed && err != null){
Console.WriteLine ("error");
}
});
}
I was able to resolve the problem with the way I passed in the NSUrl to the PrintingItem. Before I was passing in printer.PrintingItem = url.Path; which was basically just passing in a string of the path and not the actual shape of NSUrl.
printer.PrintingItem = url;
What I've always preferred to do (and this depends very much on the deployed device - in my case it was a server I controlled) was to just install a PDF printer and then it's as easy as printing any other kind of document.
Something like BullZip is free and allows you to write any settings for the print to a RunOnce.ini (xml) file to have a silent mode print with settings for the filename and so forth.
Obviously not a great solution if you don't have control of the system you're deploying to, but a solid and easy one if you do.