Opening a certain page on a PDF file in WPF - c#

I Need to open a specific page of a pdf file.
I tried:
private void Button_Click_20(object sender, RoutedEventArgs e)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.Arguments = "/A \"page=5\"";
startInfo.FileName = #"J:temp.pdf";
process.Start();
}
but it still opens the first page. Still unsolved.
if i Change to this
private void Button_Click_20(object sender, RoutedEventArgs e)
{
{
Process process = new Process();
process.StartInfo.Arguments = #"/A \"page=5\" \"J:\\temp.pdf"";
process.StartInfo.FileName = #"J:\temp.pdf";
process.Start();
}
}
i get seven Errors (Semicolon, page no context...)

It's unclear on how the arguments need to look like. Assuming it's J:\temp.pdf /A page=5
this should work:
Process process = new Process();
process.StartInfo.Arguments = #"/A page=5";
process.StartInfo.FileName = #"J:\temp.pdf";
process.Start();
However I'm not sure if you can pass arguments to a file name like this, I'd assume you need to call your PDF viewer's executable and pass both the file name and the page argument like in the question already linked in the comments:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
startInfo.Arguments = "/A \"page=5\" \"E:\\Users\\You\\temp.pdf\"";
Process.Start(startInfo);
This works on my machine (replace paths as needed of course).

Related

How to run 2 files c# without opening window

I got 1 mapper (.exe) and 1 driver (.sys) and i want to do so that when i execute them i want to run mapper as admin with the driver and not create a window (keep it hidden). can anyone help. I have the following code down but nor does it run the mapper as admin with the spoof and it still also creates a window! help!
string map = "C:\\SCSpoofer\\mapper.exe";
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.Verb = "runas";
myProcess = Process.Start(map, sys);
System.Threading.Thread.Sleep(150);
myProcess.Kill();```
// Create a Process to launch a command window (hidden) to create the item templates
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = Project.ServicesFolder;
startInfo.Arguments = "/C " + CreateServices;
process.StartInfo = startInfo;
process.Start();
This is from my open source project DataTier.Net github.com/DataJuggler/DataTier.Net

Run taskmgr.exe without administrator rights?

I NEED to run Task Manager with the very specific code that I have, but it is appearing with an access denied error.
I have attempted to run in administrator mode before.
FileStream fs = new FileStream(System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
The expected result I want is that Task Manager opens using the code above, without administrator rights! (Is there anyway around this?)
Use this:
using System.Diagnostics;
ProcessStartInfo startInfo = new ProcessStartInfo(); //a processstartinfo object
startInfo.CreateNoWindow = false; //just hides the window if set to true
startInfo.UseShellExecute = true; //use shell (current programs privillage)
startInfo.FileName = System.IO.Path.Combine(Environment.SystemDirectory, "taskmgr.exe"); //The file path and file name
startInfo.Arguments = ""; //Add your arguments here
Process.Start(startInfo);
Resources:
ProcessStartInfo - MSDN
This is a start process function I have
using System.Diagnostics;
private static void StartProcess(string exeName, string parameter)
{
using (Process process = new Process())
{
process.StartInfo.FileName = exeName;
process.StartInfo.Arguments = parameter;
process.EnableRaisingEvents = true;
process.Start();
}
}
Then call it like
StartProcess("exename.exe", fileParameter);
Process Class

Problem with running batch files from within my application

I've been trying to create a simple application to backup my Windows Server databases aswell as a whole server backup.
For this I want to use batch files which are being executed by my application.
I tried several approaches but for some reason it always fails so I'd be happy if you could help me out.
Batch file BACKUPSERVER:
wbadmin start backup -backupTarget:D: -include:C: -allCritical -quiet
I have to run the bat as administrator or it fails due to missing permissions.
C# code:
static Task<int> RunProcessAsync(string fileName)
{
............
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C \"D:\\SQLBACKUP\\BACKUPSERVER.bat\"";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
Debugging says 'wbadmin wasnt found'. 'runas' activated or not doesn't make any difference.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fileName;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = false;
// startInfo.Verb = "runas";
var process = new Process
{
StartInfo = { FileName = fileName },
EnableRaisingEvents = true
};
process.StartInfo = startInfo;
process.Exited += (sender, args) =>
{
tcs.SetResult(process.ExitCode);
process.Dispose();
};
process.Start();
Also doesn't work.
Any ideas?
EDIT:
I'm able to run commands like shutdown but wbadmin doesn't work whatsoever...
This is how I solved the problem:
Make sure ure compiling for 64bit if u intend to use your application on 64bit system, otherwise it will redirect to different subfolders and wont find 'wbadmin.exe'.
Run wbadmin with ProcessStart or run a batch but without direct cmd input, so use this with filename = batch file or wbadmin with startInfo.Arguments:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = fileName;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = true;
startInfo.CreateNoWindow = false;
// startInfo.Verb = "runas";
var process = new Process
{
StartInfo = { FileName = fileName },
EnableRaisingEvents = true
};
process.StartInfo = startInfo;
process.Exited += (sender, args) =>
{
tcs.SetResult(process.ExitCode);
process.Dispose();
};
process.Start();
Make sure u request administrator rights

Open bat file by c#, but it's closed immediately [duplicate]

I currently have a portion of code that creates a new Process and executes it from the shell.
Process p = new Process();
...
p.Start();
p.WaitForExit();
This keeps the window open while the process is running, which is great. However, I also want to keep the window open after it finishes to view potential messages. Is there a way to do this?
It is easier to just capture the output from both the StandardOutput and the StandardError, store each output in a StringBuilder and use that result when the process is finished.
var sb = new StringBuilder();
Process p = new Process();
// redirect the output
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// hookup the eventhandlers to capture the data that is received
p.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
p.ErrorDataReceived += (sender, args) => sb.AppendLine(args.Data);
// direct start
p.StartInfo.UseShellExecute=false;
p.Start();
// start our event pumps
p.BeginOutputReadLine();
p.BeginErrorReadLine();
// until we are done
p.WaitForExit();
// do whatever you need with the content of sb.ToString();
You can add extra formatting in the sb.AppendLine statement to distinguish between standard and error output, like so: sb.AppendLine("ERR: {0}", args.Data);
This will open the shell, start your executable and keep the shell window open when the process ends
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
p.StartInfo = psi;
p.Start();
p.WaitForExit();
or simply
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
if(p != null && !p.HasExited)
p.WaitForExit();
Be carefull espacially on switch /k, because in many examples is usually used /c.
CMD /K Run Command and then return to the CMD prompt.
CMD /C Run Command and then terminate
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/k yourmainprocess.exe";
p.Start();
p.WaitForExit();
Regarding: "Member Process.Start(ProcessStartInfo) cannot be accessed with an instance reference; qualify it with a type name instead"
This fixed the problem for me....
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
p.WaitForExit();

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);

Categories

Resources