Open and Close a PDF using Process.Start and Process.CloseMainWindow - c#

I am opening a PDF file using Process myProcess = Process.Start(fileLocation);. I need to close this PDF file before opening a new file. Otherwise I will end up with hundreds of instances open on my default PDF reader.
I attempted to use myProcess.CloseMainWindow(); but I got an error that the window had already closed. I think myProcess works by creating a secondary Process that displays the pdf. I have no information about this potential secondary process.
I am currently using MicrosoftEdge as my default pdf application and coding in Microsoft Framework 4.8.

Related

How can I split a corrupt pdf file using C#

I am trying to split a 'n' paged pdf file to 'n' number of pdf files containing 1 page each in ".net". For normal pdf files, PDFSharp is working fine but for corrupt file its showing errors listed down.
When I use Adobe Reader and 'Save As' the file, the new file is uncorrupted one. But I do not want to do it manually. I tried to open the pdf in Adobe reader using 'Process' but I can't save from there without manually saving it. If I use other DLLs the job gets done but it adds watermark.
Errors while opening the PDF Doc:
"Invalid entry in XRef table, ID=9, Generation=0, Position=0, ID of referenced object=1, Generation of referenced object=0"
{"Unexpected character '0xffff' in PDF stream. The file may be corrupted. If you think this is a bug in PDFsharp, please send us your PDF file."}
Object already in use exception.
For handling corrupt files through process I tried this:
Process p = new Process();
p.StartInfo.FileName = file;
p.Start();
p.Close();
corrupt = true;
inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
Dealing with a corrupt file gracefully is no simple task. You need a deep understanding of the file format. Extensive examples of what could go wrong from other broken implementations. And a strategy to resume parsing for each type of error.
If there isn't a nice pdf library that already has these features, you aren't going to find one here.

How to open a file (for reading) which is already opened in another program? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Open file ReadOnly
In my application I am writing some information to Log.txt using Debug.WriteLine(), and I want to provide a form which will show the contents of Log.txt.
But when I try to open the file Log.txt I am getting an exception:
The process cannot access the file 'F:\Rajeev\10-11-2012\Temp\Temp\bin\Debug\Log.txt' because it is being used by another process.
How do I overcome this problem?
And here another issue is, I am able to open the same file using Notepad. Then why can't I open the same file using my application?
The following is the code I am using for specifying the log file:
TextWriterTraceListener tr = new TextWriterTraceListener(System.IO.File.CreateText("Log.txt"));
Debug.Listeners.Add(tr);
Debug.AutoFlush = true;
The following is the code I am using for writing to the log file:
Debug.WriteLine("ERROR: Invalid Username " + s);
The following is the code I am using for opening a log file (which is already opened by "Debug") to show in Log Viewer (a "Form" in my application):
File.Open(LogFilePath, FileMode.Open, FileAccess.Read,FileShare.Read);
This is determined by the permissions the file is opened with. If a program opens the file in any type of exclusive mode, then other programs will have limited or no access.
So you can make sure you don't try to open the file exclusively (you didn't show your code). However, if you don't have the source code for the other program, then you can only hope that that program doesn't open the file exclusively. Otherwise, I don't see what you could do about it except terminate the other program.
EDIT
The following code:
File.Open(LogFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
Says that other files can also read the file that is open, but I believe they can't write to it. So if the other program was writing to the file, that would not be allowed. You could try FileShare.ReadWrite instead but I'm still not seeing where you've indicated if you have source to the other program. Did I just miss it?

How to check file process in visual studio in c# language?

I am working on pdf creator in Asp.net with c#. In my code it created a pdf file and after that i use flush, close and dispost the streamwriter and in the nest function i am opening the same file in the next function i am opening this same file to show in browser pdf viewer. my pdf creation method is used threading and pdf reading function is in normal mode function.
My question is that how to check that this pdf file is use in another process in c# code? Because if i have created very small pdf file it will works fine but if i am creating a pdf file of 5-10 pages in that case it will throws error. i.e. the process cannot access the file because it is being used by another process
I want to use check the process monitor code on that file and how to explicitly kill that process?
If you are starting process that creates PDF, just wait for it and check exitcode, of course if the process that creates PDF return proper exit codes for success or some error, here is some example code :
Process proc = Process.Start("createpdf.exe", "params");
proc.WaitForExit();
int result = proc.ExitCode;
/// do something based on result
If it doesn't make sense to open the Stream as FileShare.Read in the first place (because your PDF is being created and it's not yet viewable), then you may as well just try ... catch opening the stream.
At least that's how this popular thread seems to do it.

Cannot write to file while it is opened in MS Excel

I am writing to the text file some data. I am using this code:
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
using (TextWriter tw = new StreamWriter(fs))
{
tw.WriteLine("sample_data");
}
}
When file is opened by notepad my app can write into it. When this file is opened by MS Excel I get following error: The process cannot access the file myfile.csv because it is being used by another process. What can cause this situation and how can I solve this problem?
Notepad opens the file, reads in the entire contents and then closes the file. You can even delete the file that is open in notepad.
Excel on the other hand keeps the file open as long as it is displayed. There are some special sharing tools that can be enabled in Excel for excel format files. In that case I assume that it is opened non-exlusively. Otherwise Excel opens the file exclusively and keeps it open.
It doesn't matter that you specify a share option when opening, if the file is already opened by someone else in exclusive mode.
Excel will lock the file when it is open which prevents interaction with the file. One way I worked around this is that I wrote code to scan for excel processes on the local machine and would kill those processes before accessing a file that was open with excel. You could determine if a file is locked by looking at How to check for file lock? and then running the process killing code in the exception handler.
Open the csv file using OleDB and use INSERT and/or UPDATE statements.

Can you print a pdf file using ITextSharp (c#) ? If yes how?

I have an ASPX page which builds a report. I have a print button which builds a pdf file using ITextSharp. Now I want to print that file.
I have two questions:
How do I print it with out even saving the file ?
and If I can't do this, can I at least print the saved file ?
Thanks in advance.
You cannot use iTextSharp to print PDF document. iTextSharp can be only used for reading or building PDF's.
What you can do is to show it to the user and then he can choose to print it or not.
Here is a sample how to push PDF document to user via C# ASP.NET: How To Write Binary Files to the Browser Using ASP.NET and Visual C# .NET
#Jared. Well what we did was to start the acrobat reader with printing parameters after we saved it on the file system. Something like:
ProcessStartInfo newProcess = new ProcessStartInfo(pdfPath, dfArguments);
newProcess.CreateNoWindow = true;
newProcess.RedirectStandardOutput = true;
newProcess.UseShellExecute = false;
Process pdfProcess = new Process();
pdfProcess.StartInfo = newProcess;
pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdfProcess.Start();
pdfProcess.WaitForExit();
(please note this is not the actual code we used I got this from here) this should get you started.
For initializing adobe acrobat with printing parameters see this.
Hope it helps.
In ASP.NET you don't print anything, user does. Most you can do is bring up print dialog, but I personally find it very annoying when a web page suddenly opens a modal dialog.

Categories

Resources