Diagnostics.Process doesn't open PDF file using adobe reader - c#

I'm having a problem while trying to open a PDF file using Process.Start().
The program runs the process as a background process but no adobe reader window show up. A solution i found was to use chrome.exe, but my client want to open on adobe-reader, as it's the default windows program.
(I have also trying without forcing "AcroRd32.exe" and passing the full path of "AcroRd32.exe". Same problem).
Anyone solved this?
My code:
var p = new Process
{
StartInfo = new ProcessStartInfo(#"AcroRd32.exe", #"D:\Tests\81803130000326__.pdf")
};
p.Start();
Background Processes:
I also have checked this question: Process.Start(/* path to pdf */) doesn't work with Adobe Reader on Windows 8
Edits
EDIT INFO: Forgot to say i'm running Windows 10 with Adobe Reader DC 2018.009.20044
EDIT 2: Found a "problem" of Adobe Reader DC. It mantains a level of security that doesn't let me run it as administrator (don't ask me why). Check this link for more information: https://forums.adobe.com/thread/1955530
Now I'll try to find a solution to run it without administrator privileges, or ask my client to uncheck the checkbox
Solution
So, I've found a solution for my problem. The Adobe Reader, since version 10.0, has a "Protected Mode" setting. I wrote a code to change that setting in the registry, so the user won't need to do that. Link: https://www.adobe.com/devnet-docs/acrobatetk/tools/AppSec/protectedmode.html
I hope it helps others in the future!
Solution Code:
var registroAdobe = Registry.LocalMachine.OpenSubKey(#"Software\Policies\Adobe\Acrobat Reader\DC\", true);
registroAdobe.SetValue("bProtectedMode", 0);
This works for Acrobat Reader DC. You can run through "Software\Policies\Adobe\" Products and Versions if you need it.

Actually, I found that this didn't work on my computer, but it led me to look deeper.
I found my solution to be:
var regAdobe = Registry.CurrentUser.OpenSubKey(#"Software\Adobe\Acrobat Reader\DC\Privileged", true);
regAdobe.SetValue("bProtectedMode", 0);
Very similar, but ended up having to edit the user setting rather than the local machine's setting.
Cheers!

You can directly use System.Diagnostics.Process.Start("") to open with default pdfreader program on respective client system.

Related

Unable to get Selenium with Edge to work in a basic C# console application

I am trying for the very first time to use Selenium. I would like to have a console application that opens a browser, goes to a website, and performs some actions.
To do this I looked up some tutorials. The first one I found is here:
https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=c-sharp
I would like to automate Edge.
The first thing I did was get my Edge version, which incidentally decided to update when I went to the about page, something that will no doubt be a problem going forward.
I downloaded the file that corresponded to my version, although for some reason that version is x86 only while all the others are x64...
I then went to NuGet and searched for Selenium.WebDriver. Probably because I'm targeting 4.7.2 the latest version is 3.141 not version 4, but whatever. I install version 3.
I also add a nuget reference to Microsoft.Edge.SeleniumTools as instructed in the guide.
the guide then says use this code:
var options = new EdgeOptions();
options.UseChromium = true;
var driver = new EdgeDriver(options);
However I don't have a UseChromium property, so this does not work.
I removed it, and used the default constructor, however this also doesn't work because it can't find the driver.
So next I passed in the path to the folder the driver is in as a parameter.
This also did not work because it is looking for a file with a different name to the one in the edge driver zip download. It looks for microsoftwebdriver.exe while the zip file contains msedgedriver.exe. Perhaps this is the reason for the error? It actually has a link in the exception when it can't find it of where to download it, however the link is dead.
I renamed the file, and ran the code. A console window appeared with debug information, and the browser opened. Success!
Next I tried to go to a website, with this code:
driver.Url = "https://www.google.com";
However with this I got these errors in the console window:
Starting MSEdgeDriver 94.0.992.38
(55a0a486d5c4c1a7374dc28a7be702fee43b3b39) on port 1307 Only local
connections are allowed. Please see
https://chromedriver.chromium.org/security-considerations for
suggestions on keeping MSEdgeDriver safe. MSEdgeDriver was started
successfully.
DevTools listening on
ws://127.0.0.1:1310/devtools/browser/c4e7f6f6-46d3-447c-b26b-3ad231a6122f
[29296:584:1001/203949.853:ERROR:fallback_task_provider.cc(119)] Every
renderer should have at least one task provided by a primary task
provider. If a fallback task is shown, it is a bug. Please file a new
bug and tag it as a dependency of crbug.com/739782.
[29296:26032:1001/203954.115:ERROR:chrome_browser_main_extra_parts_metrics.cc(250)]
crbug.com/1216328: Checking default browser status started. Please
report if there is no report that this ends.
[29296:584:1001/203954.143:ERROR:profile_manager.cc(1057)] Cannot
create profile at path
C:\Users\NibblyPig\AppData\Local\Microsoft\Edge\User Data\Default
[29296:584:1001/203954.143:ERROR:profile_manager.cc(2010)] Cannot
create profile at path
C:\Users\NibblyPig\AppData\Local\Microsoft\Edge\User Data\Default
[29296:26032:1001/203954.176:ERROR:chrome_browser_main_extra_parts_metrics.cc(254)]
crbug.com/1216328: Checking default browser status ended.
The code then crashes with OpenQA.Selenium.WebDriverException: 'unknown error: net::ERR_CONNECTION_TIMED_OUT
I've tried googling the fallback task provider error and also looking at the profile error, but there is very little information and I am at a loss of what to do.
I wonder if anyone could advise what steps I might take to get this working.
First, if you use WebDriver with Selenium 3, the Selenium Tools for Microsoft Edge package must be installed.
Did you import the wrong package? Make sure it is using Microsoft.Edge.SeleniumTools; not using OpenQA.Selenium.Edge;.
This is my simple test, and it works well:
public static void Main() {
var options = new EdgeOptions();
options.UseChromium = true;
options.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var driverPath = #"C:\Users\Administrator\Desktop";
var driver = new EdgeDriver(driverPath, options);
driver.Url = "https://www.google.com/";
}
Note: Please modify the path parameters according to your own situation.

windows explorer cant see files written by c# app File.WriteAllLines

I am having a strange problem that I am unable to access files written by my c# application. My app basically does :
var file = "C:\\Users\\Public\\Documents\\something.txt";
List<string> content = new List<string> { "one thing", "two things" };
Console.WriteLine(System.IO.File.Exists(file));
System.IO.File.WriteAllLines(file, content);
Console.WriteLine(System.IO.File.Exists(file));
The first time I run the app, the output is
False
True
Yet I cannot see the written file in Windows Explorer (Windows 10). I get no exceptions attempting to write the file. The second time I run the app, the output is :
True
True
According to my application the file is being written however Windows thinks differently. As a sanity check I spun up a second app that opens a dialog using OpenFileDialog. When I run that, I am able to see my written files! Windows explorer still cannot. Attached is a screenshot of windows explorer and my openfiledialog side by side.
If I go to notepad and browse for the file I cannot see it or manually type in the name.
Its been a long week of work, there must be some dumb explanation...? Help! :-)
Screenshot - windows explorer on left, c# app open dialog on right :
https://imgur.com/a/8ZTDIe6
per #BACON 's suggestion in the comments above I discovered that after disabling the Comodo anti-virus I am able to write and see my files.
I believe the software is running my app or either only allowing IO from my app in some kind of container. I need to figure out how to grant my apps proper permissions through the anti-virus software, but that was the culprit.

Using LibreOffice(soffice.exe) as Process.Start() from Code behind not working on IIS server

I am using LibreOffice as command line for conversion of docx to pdf. I am using below code snippet.
using (Process pdfprocess = new Process())
{
pdfprocess.StartInfo.UseShellExecute = true;
pdfprocess.StartInfo.LoadUserProfile = true;
pdfprocess.StartInfo.FileName = "soffice.exe";
pdfprocess.StartInfo.Arguments = "-norestore -nofirststartwizard -headless -convert-to pdf C:\\test.docx";
pdfprocess.StartInfo.WorkingDirectory = #"C:\Program Files\LibreOffice\program\";
pdfprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdfprocess.Start();
if (!pdfprocess.WaitForExit(1000 * 60 * 1)) {
pdfprocess.Kill();
}
pdfprocess.Close();
}
Everything works fine under IISExpress or Console application. When I try to run under IIS server, it doesn't work.
I am running under DefaultAppPool and I have given permission to DefaultAppPool to access LibreOffice directory but I am not able to get result.
I don't want to change Identity to LocalSystem as security concerns.
How can I able to run soffice.exe using Process.Start() under default ApplicationPoolIndentity?
I had the same problem and I just found solution that worked for me. When I was executing conversion under CMD console everything was working fine. But soffice.exe executed under iis app didn't work.
Although, application pool has it's own user profile directory it looks like libreoffice cannot create its files there. What I did is I created temp folder under iis www directory and gave it apppool permisions. Then I passed this location with other parameters like that: "-env:UserInstallation=file:///C:/www/temp/libreoffice"
Had same issue. The problem was that process started, but didn't exit and give no results, any StandardError output. It was definitely a problem of permissions. So I've started procmon and it showed me, it needs access to this path:
C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\LibreOffice
Looks like similar problem here:
Why does systemprofile need Desktop folder to open excel file
So not sure what to do next from security point of view, but solution is to give access to that folder for ApplicationPoolUser.
UPDATED:
Looks like it works not for all documents, and even after I eliminated all ACCESS DENIED issues from Procmon for other docx file, "soffice" still stuck, and I cannot kill process from Task Manager, it gives "Access Denied" dialog. Looks like it depends on some fonts, it opens something, and because it hold it, process cannot be closed, and no more files proceed.

Printing any file type

I'm creating a service that will monitor a specific folder and print any file that is put in this folder. I'm having difficulties with the various file types that could be sent to the folder to be printed.
My first attempt is with Microsoft Office files. What I'm trying to do is start the office to print the file. It's more like a catch, I'm not really using a library or anything like it.
So far this approach would work, but when no Office application has ever started on the machine the Office asks for the user initials. So, in this case my application would just not work, since I'm doing this programatically.
ProcessStartInfo info = new ProcessStartInfo(myDocumentsPath);
info.Verb = "Print";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Right now I am trying with Microsoft Office files, but I will apply the same approach for other types as well.
There is anyway to get around the Initials required by the first Office run?
Or any better approach to my problem?
Any help is appreciated, thanks in advance.
There's not really anything that you can do; for the application which will print each type of file that you're going to support, you need to make sure the application is configured correctly.
This means that for office (since it is run as an out-of-process COM server) you should run it under the account that is performing the printing so you can enter the initials and won't be prompted for it when the server attempts to print it.
The same for every other application (assuming the application is executed to print it), it needs to be run as the account the process is going to be run under and configured correctly.
Use Winternals's regmon registry activity monitor to figure out where Office stores the initials in the windows registry, then write these keys prior to printing.

Starting serverside print job via PHP

This is most likely not an easy one but here is the situation:
I have written a C# command line application which:
creates a PDF using ITextSharp
writes it to disk
uses Acrord32.exe (this is Acrobat Reader) via System.Diagnostics.Process in order to silently print the generated PDF
If I build my solution and double click the pdfGen.exe, it works as expected. The PDF is created and printed.
Now, my app has to be deployed on a internal server with Windows Vista running IIS 7. This server has some PHP webapp running. And it will be called via PHP using shell_exec() so that the resulting PDF will be printed on the printer attached to the server.
So my PHP page looks basically like this:
shell_exec('/path/to/pdfGen.exe');
But here things go wrong. What happens is according to task manager etc.:
pdfGen.exe starts
the PDF is created
Acrord32.exe starts
pdfGen.exe hangs forever (and so does the PHP script) and nothing is printed
I am pretty sure it is some permission related problem. I already gave IIS_IUSRS access to the default printer, and to the directory where Acrord32.exe is located. But still, no printing. However, if I start my pdfGen.exe manually it works.
Any idea what I am missing?
EDIT:
I am not bound to use Acrobat Reader in order to print the PDF. If there is another way in order to silently print the created PDF serverside, I would not mind at all.
In order to check what is going on, try to run
the process monitor from Sysinternals and filter the events to the adobe acrobat process. You will see the system calls of acrobat and it will allow you to know more or less what is going wrong.
I know a small improvement to your solution: SumatraPDF has a nice command-line interface that can be used to auto-close Sumatra after printing.
I used PHP "system" or "exec" functions to execute a batch file to open SumatraPDF:
sumatrapdf.exe -print-to-default -exit-on-print <path_to_PDF_file>
(you can also specify the printer name to print on)
that's an interesting program.
IIS_IUSRS
seems to have no permission to print, try adding IIS_IUSRS to Print Operators Group / grant Print permission to the user.
Shell_exec() is almost intended for shell commands (ls/dir, cp, etc.)
Have you tried to use exec() instead of shell_exec() ?
Thanks all for your comments. Unfortunately this "php start printjob" thing was part of a larger project that was cancelled today because of, well... I dont know... political reasons. Guess the project is pretty much dead.
Anyway, I tried myself a few more times in the last days and could not get it to work with IIS. My solution that I implemented and tested already: remove IIS, install a XAMPP or WAMPP package with a local apache and PHP that runs with admin access rights.
This did the trick. I used pclose(popen('...command...', 'r')); in PHP in order to start the .exe and so that PHP does not wait until the PDF is finished. It all worked great.
Here is my C# code which starts the print job using Acrobat Reader
public void Print(string pathname, string acrobatDirectory)
{
var proc = new Process
{
StartInfo =
{
Arguments = String.Format("/t \"{0}\"", pathname),
FileName = acrobatDirectory,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = false,
RedirectStandardError = false,
}
};
proc.Start();
}
The first argument is the path to the PDF that should be printed, the second parameter is the absolute path to the AcroRd32.exe.
The only problem left was that AcroRd32.exe was started, printed and never got closed again. So every printjob started a new instance of AcroRd32.exe (I am using Acrobat Reader 9.0). So if you printed 10 times, 10 acrobat reader instances were created.
What I did was starting the print job, then waiting X seconds, hoping that the printer was finished and then killing all AcroRd32.exe instances:
public void Print(string pathname, string acrobatDirectory)
{
Debug.WriteLine("Printing...");
Printer.Print(pathname, acrobatDirectory);
Thread.Sleep(30000);
try
{
Debug.WriteLine("Trying to kill runnung AcroRd32.exe's ");
FindAndKillProcess("AcroRd32");
}
catch (Exception)
{
Debug.WriteLine("AcroRd32.exe could not be killed...");
}
}
private bool FindAndKillProcess(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.StartsWith(name))
{
clsProcess.Kill();
return true;
}
}
return false;
}
This worked out quite well.
Note that the above (killing all AcroRd32.exe and running PHP with admin privilegs) was only doable because: The whole thing is only used by one user at a time and has a very limited area of use.
It should be used on a touchscreen application deployed at the clients POS. A salesman would use the PHP app in order to configure a product, and then PHP would call my .exe which would create and print a PDF in the background. The printed document is then handed to the client. So security etc. was not really a concern in this case.
If anyone has a solution in order to use it with IIS, I am still willing to accept it as an answer.

Categories

Resources