how to print file C# - c#

I wonder how to print any printable document such as doc/docx/txt/pdf using C# (WPF/WinForm whatever). MSDN documentation shows how to print .txt files (like this one https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396). But when i'm trying to print .docx i've got XML-like text. I need to print file from directory only. I don't want to edit it etc. So i wonder i can skip few steps and send my file to printer directly, can't i?

using (var pd = new PrintDialog())
{
pd.ShowDialog();
var info = new ProcessStartInfo()
{
Verb = "print",
CreateNoWindow = true,
FileName = #"D:\Desktop\00762.pdf",
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(info);
}

Related

Print XFA file to PDF without opening it

I am attempting to auto-fill/sign/print the federal i-9 form using Spire.PDF and C#. The i9 file is an XFA form and is protected and doesn't allow for signing. However, if I fill the i9 and print to PDF, then I can sign that new file.
The step I'm getting stuck on is printing the filled i9 to a PDF file without actually opening Acrobat or having direct interaction from the end-user to specify a file name. I say 'printing' because if I just save it as a PDF file it never flattens the XFA form and remains locked against signing.
So far I have automated printing of the file using this code:
Process proc = new Process();
proc.StartInfo.Verb = "PrintTo";
proc.StartInfo.FileName = filename;
proc.StartInfo.Arguments = "\"" + printername + "\"";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
and I think I might be able to force use of the Microsoft Print to PDF 'printer' here, but I don't know if there's a way to specify the file name to use so that the user isn't prompted?
If I try printing using the Spire.PDF control, I am only able to get a file with the "Please wait...
If this message is not eventually replaced by the proper contents of the document, your PDF
viewer may not be able to display this type of document.." message as a result.
When the form is opened to print via Acrobat I get a popup of "This form contains incomplete or invalid information. Are you sure you want to print?" If I click Yes then I can successfully print to PDF and then I can sign that file.
So, I believe whatever data-checking is happening is causing the failure to print via code and I'm hoping those wiser than I might have some ideas of ways around this issue.
Thank you in advance for your help! If you just search for Federal i9 you should find the file I'm working with. I didn't see a space to attach a file here.
This is the code that I'm using to try to accomplish my task via the Spire.PDF control.
PdfDocument doc = new PdfDocument();
string i9path = "locationofi9file"
string newi9path = "locationoffilledi9file"
doc.LoadFromFile(i9path);
/*fill form here*/
doc.Form.IsFlatten = true;
doc.SaveToFile(newi9path, FileFormat.PDF);
doc.Close();
doc.Dispose();
doc.LoadFromFile(newi9path);
string file = "printi9";
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
PrinterSettings settings = new PrinterSettings();
PageSettings pages = new PageSettings();
string printername = "Microsoft Print to PDF";
settings.PrinterName = printername;
settings.PrintToFile = true;
settings.PrintFileName = Path.Combine(directory, file + ".pdf");
PrintDocument printDoc = doc.PrintDocument;
printDoc.PrinterSettings = settings;
printDoc.Print();
doc.Close();
doc.Dispose();

Prevent default pdf reader open after call PrintTo c#

I'm trying to make a program that send pdf file to a thermal printer. The problem is that after the file has been sent to printer, the default PDF reader (foxit reader in this case) is always open up and unmanageable. And yes, I already spent days to search but nothing completely works for me.
Also, I have tried to turn off "View PDF Result" property on Foxit Reader PDF Printer but it seems does not work as well.
Here is my simple code:
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(#"D:\test.pdf");
info.Arguments = "\"XP-58\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
PrintProcess p = new PrintProcess();
p.StartInfo = info;
p.EnableRaisingEvents = true;
p.Start();
p.WaitForInputIdle(1500);
p.Stop();
Now I can completely close the pdf reader (foxit reader) or whatever using this way. But, still looking for another can be do this task silently without open then close it.
private static bool KillAdobe(string name)
{
foreach (Process clsProcess in Process.GetProcesses().Where(
clsProcess => clsProcess.ProcessName.StartsWith(name)))
{
clsProcess.Kill();
return true;
}
return false;
}

How to open save dialog with custom file view

I have a save file dialog in my C# application which opens user's desktop as default:
SaveFileDialog saveFileAddress = new SaveFileDialog
{
FileName = "MyXml",
CheckPathExists = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
OverwritePrompt = true,
Title = "Saveyour XML",
AddExtension = true,
ValidateNames = true,
Filter = "XML Document | *.xml"
};
As you see I've customized many of its options, But the one I can't figure out, is how to set default file view (thumbnail, tile, etc.. ) for it.
Is it possible programmatically?

How to Notify Print status of a pdf document after print?

Is it possible to print a PDF document using 'System.Printing' Namespace? How does it differ from System.Drawing.Printing'? I want to print a document and check whether it is printed or not! Presently i use a process to print pdf as
var fileName = filepath;
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = "HP LaserJet P1505n";
psInfo.FileName = fileName;
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.Verb = "print";
psInfo.CreateNoWindow = false;
psInfo.UseShellExecute = true;
process = Process.Start(psInfo);
Here i cannot determine page printed or not!
How can i accomplish this? It is possible to check status of printer using Win32_Printer with System.Management namespace but not "printing" status

C# Excel Interop: Opening and Showing CSV file

Hey I'm writing a wrapper for the excel interop, I want to be able to open a csv file in excel and show it to the user. I've got the basics down, but when i set visible to true and excel shows up, all columns are jammed into the first, and the separating commas are showing.
here's my helper.
public MyExcel(string filePath, bool readOnly)
{
_app = new Excel.Application();
_workbooks = _app.Workbooks;
_workbook = _workbooks.Open(_filepath, 0, _readOnly, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", !_readOnly, false, 0, true, true, true);
}
public void Show()
{
_app.Visible = true;
}
any suggestions?
When i open the file by double clicking Excel processes everything properly.
You will need to use the OpenText method, instead of Open, if you want Excel to parse for delimiters. Details: http://msdn.microsoft.com/en-us/library/bb223513%28v=office.12%29.aspx
An example in C#: http://msdn.microsoft.com/en-us/library/c9838808.aspx
It is MUCH easier than that if all you want to do is open the file...
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo("excel.exe", "output.csv");
proc.Start();

Categories