PDF to XPS Converting via Microsoft XPS Document Writer - c#

Printing pdf document with Microsoft XPS Document Writer:
string filename = "C:\\1.pdf";
Process process = new Process();
process.StartInfo.Verb = "PrintTo";
process.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 9.0\Reader\acrord32.exe";
process.StartInfo.Arguments =
"/t \"C:\\1.pdf\" \"Microsoft XPS Document Writer\" \"xps\" XPSPort:";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardOutput.ReadToEnd();
process.WaitForExit();
The only problem is Save Dialog, which requests file name (*.xps) where to save result. Everbody advices DOCINFO to solve this problem, but I didn't find any example of using.
I need programatically print PDF File via Microsoft XPS Document Writer with default output file name. How should I use DOCINFO in this situation?
Can you help me?

You can't reliably print by spawning Acrobat Reader unless you give it a desktop session and there will be a user there, because it sometimes pops up dialogs that need user attention.
Also it violates Adobe's licence if used unattended.
You can, however print using Ghostscript.
There is a C# interface to Ghostscript called Ghostscript.Net that I've used successfully on some very large projects. Both Ghostscript and Ghostcript.Net are free & open source.

Related

C# Print Word Document without opening Word or Printerselection

I tried using the code below, using PrintDocument, etc.. but can't get it to work.
Every time I run the Code below it opens a window asking me to select a printer. Using PrintDocument always led to empty pages, but the docs got printed.
How can I print Word documents without opening any windows?
foreach (string doc in dirFiles)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = doc;
info.Arguments = SelectedDrucker; //Printername
info.CreateNoWindow = true;
Process.Start(info);
}
Only the Word application can correctly print a Word document as it interprets the content/layout as it's sent to the printer. You first need to open the document in the Word application, then use its PrintOut method.
(This is also what Windows does when a user right-clicks a Word file and selects "Print".)
So there's no way to print a document without opening a window. You can, however, minimize the Word document window once it's been opened.

Print PDF hidden from user

I have a PDF printed from C# with this code:
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "printto";
info.FileName = segnToPrint;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = "\""+ stmp+ "\"";
Process p = new Process();
p.StartInfo = info;
p.Start();
p.EnableRaisingEvents = true;
p.WaitForInputIdle();
System.Threading.Thread.Sleep(1000);
// Close Acrobat regardless of version
if (p != null)
{
p.WaitForInputIdle();
p.CloseMainWindow();
}
stmp is the address of the printer. The print works fine and it's perfect but i see the windows of Acrobat Reader any time that i call this function and the page of Acrobat Reader remains open after the last file printed.
How can i hidden all the process to the user?
If you are looking to hide some window then you may try move its window left and top position outside the screen using SetWindowPos function (see C# code here).
But please be aware of user interactions as user could be confused by the program running in the taskbar but not available on desktop.
Unfortunately (as you have found) acrobat reader will always open a window. If you wish to silently print without ever seeing acrobat then the only approach would be to use something other than acrobat reader. Two possible options are to use another PDF reader like FoxIt or to try to send your PDF directly to the printer in raw form, getting round the need for 3rd party applications all-together.

PDF or Other "report viewer" options for Asp.net C#

I'm in a bind with my current project. It's a payroll application and I'm developing in ASP.net webforms with C#. My boss said that the ideal function of this site is to click on the pay check date, and a PDF opens and shows the paycheck information. I have done research for a few days to try to find the best solution. So far, I have had no luck. I have found a few different things such as iTextSharp but the license for one year is too expensive right now. I have also seen the tcpdf which is php based. I have also looked into CrystalReports, and Active Reports. To purchase the license is too expensive as well. I have looked into XML to PDF solutions also but I'm not finding anything definite. Since I'm pretty new to the business world of software development and don't have senior developers to rely on, I'm pretty much dead in the water. I know we will be downloading the paychecks into a CSV file from a DOS based application that does our clock-ins and outs. I will then be importing the CSV files into SQL Server 2012 Express. Your ideas are greatly appreciated as I don't know where to go from here! Thank you in advance!
A HTML to PDF converter that I've recently discovered is WKHTMLtoPDF
It's open source and uses WebKit to convert HTML to PDF so it's pretty standards compliant.
An example of how you might use it is
using (var pdfStream = new FileStream(dlg.FileName, FileMode.OpenOrCreate))
{
// pass in the HTML you want to appear in the PDF, and the file stream it writes to
Printer.GeneratePdf(htmlStream, pdfStream);
}
where GeneratePdf is defined as
public static void GeneratePdf(Stream html, Stream pdf)
{
Process process;
StreamWriter stdin;
var psi = new ProcessStartInfo();
psi.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\Lib", "wkhtmltopdf.exe");
psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName);
// run the conversion utility
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.Arguments = "-q -n --disable-smart-shrinking - -";
process = Process.Start(psi);
try
{
stdin = process.StandardInput;
stdin.AutoFlush = true;
//stdin.Write(html.ReadToEnd());
stdin.Write(new StreamReader(html).ReadToEnd());
stdin.Dispose();
process.StandardOutput.BaseStream.CopyTo(pdf);
process.StandardOutput.Close();
pdf.Position = 0;
process.WaitForExit(10000);
}
catch (Exception ex)
{
throw ex;
}
finally
{
process.Dispose();
}
}
In your case, instead of writing it to a file stream, you'd write it to the HTTP output stream as a PDF.
Please note however, that this example is more suitable to writing PDF files to disk, rather than the output stream so you'd need to do it differently slightly for it to work for you.
I also agree that html -> pdf is the best option nowadays if you want to render complex report and stay productive.
I have implemented a wrapper for html -> pdf conversion called jsreport.
If you are using asp.net mvc, you can check out my post Rendering pdf from Asp.Net MVC views or more generic post Pdf reports in c#.
Disclaimer: I am the author of jsreport

How to print various file types programmatically

I am writing an app which performs some tests and produces a number of different reports. These may be any combination of Labels, PDF for end customer, PDF for repair department, XML file etc.
Depending on the report type, I need to send the file to either the file system or to one of a number of different printers (A4, label etc). Ideally there should be no popup windows - just straight to paper.
How can I send a file (PDF, XML) to a printer? I had thought that for XML/Text I could just File.Copy to LPTn but that doesn't seem to work. For PDF I'm guessing that I could call Acrobat with some parameters which cause the PDF to be printed.
The printers I use are mapped to LPTn. Is there a better way to do this and to store the definitions in the app? i.e. Labels go to MyLabelPrinter and A4 PDFs got to MyA4Printer.
Has anyone done this?
ProcessStartInfo info = new ProcessStartInfo("[path to your file]");
info.Verb = "PrintTo";
info.Arguments = "\"[printer name]\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Take a look at this webpage. You should find the info you are looking at for PDF.
For example, it will look like that:
ProcessStartInfo infoOnProcess = new ProcessStartInfo("C:/example.pdf");
info.Verb = "PrintTo";
//Put a if there, if you want to change printer depending to file extension
info.Arguments = "\"HP-example-Printer\"";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(infoOnProcess);

Open pdf with Adobe Acrobat Reader and jump to bookmark

I try to open the Adobe Acrobat Reader and then jump to a bookmark. But it doesn't work with the following code:
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "acrord32.exe";
myProcess.StartInfo.Arguments = "/A \"nameddest=S2\" C:\\temp\\xxx.pdf";
myProcess.Start();
How can I do this? - Important: I have to use the Acrobat Reader not Acrobat.
When I use page=2 as a parameter it works. But not with the nameddest.
When I open the pdf, I see S2 as a bookmark in the second page.
Bookmarks and named destinations are not the same thing. You will have to create a destination in your PDF. If the rest of your code is correct, that should solve your problem.

Categories