Printing a Text file programmatically - c#

I'm tring to make a program that saves a textbox text to a text file and prints the text file.
I found this code:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(#"TempDocument.txt");
psi.Verb = "PRINT";
Process.Start(psi);
Here
But it doesn't open a dialog it's just printing.
I want to have a dialog in order to choose another printer or open in OneNote.

To show a printDialog, you can try :
However, I don't know which kind of project your talking about, so maybe this will not fit.
printDialog = new PrintDialog();
//when you click on OK
if (printDialog.ShowDialog() == DialogResult.OK)
{
//path is your documents to print location
ProcessStartInfo info = new ProcessStartInfo(path);
info.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.Verb = "PrintTo";
System.Diagnostics.Process.Start(info);
}

Related

Output of CMD-window displayed in a TextBox doesn't update in Real-Time

I am trying to display the output of a command-window in a textbox, but the code doesn't update. It only shows the first action, nothing more. What I want is it to update if new text appears in the commandline aswell.
Here is the code I have so far, which all get triggered on a button-click:
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string result = proc.StandardOutput.ReadToEnd();
TextAreaOutput.AppendText(result + "\n");
}
What am I missing?

Open a specific page in a PDF file c#

I open a pdf file when my form is loaded with the following code:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = #"F:\STAGE\test.pdf";
process.Start();
This works fine but now I want to open a specific page. For example page number 5 of the document test.pdf? Does any one have an idea? Tried some stuff but dind't work!
Thanks!
Try
process.StartInfo.Arguments = "/A \"page=n\" \"F:\\STAGE\\test.pdf"";
changing n to the page number you want
Checkout this : http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf
It explain what arguments Adobe Reader can receive.
And it has a Page argument.
Your code must be :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.Arguments = "/A \"page=N\"";
startInfo.FileName = #"F:\STAGE\test.pdf";
process.Start();
Where N is your page number.
call it like what was suggested here: Adobe Reader Command Line Reference
So it would be:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "EXE_PATH\\AcroRd32.exe";
startInfo.Arguments = "/A \"page=PAGE_NUM\" \"FILE_PATH\"";
Process.Start(startInfo);
you can try this code.
Process myProcess = new Process();
myProcess.StartInfo.FileName = #"C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe";
myProcess.StartInfo.Arguments = "/A \"page={pagenum}\" \"c:\\Classic\\Manual\\DocumentationManual.pdf\"";
myProcess.Start();
please change the path of AcroRd32.exe as per your directory.
Thanks
Try this.
Note: you must have acrobat reader installed in your pc before you can use axAcroPDF .
int n = 5; //page number
string filePath = "F:\STAGE\test.pdf";
axAcroPDF1.LoadFile(filePath);
axAcroPDF1.setCurrentPage(n);

print existing pdf file

I have made a Windows Form application and I want to print an existing PDF document with my default printer.
I have the file, stored in c:\users\marten\document.pdf.
I have searched a long time for some examples, but the only examples I found was printing a text file or printing a string into a document.
Can someone give me a good example or tutorial?
This uses the installed pdf reader to print the file against the default printer on the machine.
string path = "" <- your path here.
if (path.EndsWith(".pdf"))
{
if (File.Exists(path))
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = path;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
}

How to print a PDF to something other than the default printer

I used this code to print a pdf file from acrobat reader.
private void SendToPrinter()
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"c:\output.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
However, it prints through default printer.
How can I select the printer to send it to? default.
I have tried with a property for ex: info.Arguments, but that doesn't work.
Use the /t command line argument to force adobe to use a specific printer:
AcroRd32.exe /t path "printername" "drivername" "portname"
See the PDF developer FAQ for more info:
http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/intro_to_sdk/DeveloperFAQ.pdf
How about using the "printto" verb? pasing "\\\server\printer" for info.Arguments

How to print a document using PrintDialog in C#

Here is my sample code:
printDocument1.DocumentName = "C:\a.pbf";// PrintDocument printDocument1
printDialog1.Document = printDocument1;
printDialog1.AllowPrintToFile = true;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.PrintToFile = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
printDocument1.Print();
It runs, but it's printing an empty page. What's wrong with this?
You need to handle the PrintPage event to actually provide the contents; MSDN has a full example. The DocumentName is purely something to show to the user - it is not the path of an existing file to magically print.
For printing an existing PDF, maybe look at this question
do this :
public static void PrintToASpecificPrinter()
{
using (PrintDialog printDialog=new PrintDialog ())
{
printDialog.AllowSomePages = true;
printDialog.AllowSelection = true;
if (printDialog.ShowDialog() == DialogResult.OK)
{
var StartInfo = new ProcessStartInfo();
StartInfo.CreateNoWindow = true;
StartInfo.UseShellExecute = true;
StartInfo.Verb = "printTo";
StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.FileName = fileName;
Process.Start(StartInfo);
}
}
}

Categories

Resources