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

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.

Related

Open and Close a PDF using Process.Start and Process.CloseMainWindow

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.

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.

Asp.net c# File is used by another process

i am using Programming language C# asp.net 4.0. I have a situation where i upload an excel. Save it on hard drive using code then using SqlBulkCopy i dump all the content of this excel in to database. the code is working fine.
Problem arises if there is some problem any where in the program like i have less/Extra column in my excel file as specified in db. the exception is fired. i have handled the exception.
now if i again use the same file for upload it shows file is used by another process and code breaks.
Moreover even if file is dumped successfully. i cant upload same file due to same reason.
how can i release or clean up the code or free this file for other processing after my exception occurs or my code runs successfully
You should close the file which you have opened for reading/writing. I guess you are doing that in simple scenario but when exception occurs you have not closed the file.make sure that you close the file in exception as well.

Extract and open PPT from resources in C#

I want to view presentation in PowerPoint viewer, ppt file is in a resources. so the problem is that how can i access it and view in PowerPoint viewer.
Here is sample code
Process.Start(#"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**#"e:\presentation.ppt")**;
How can i replace this path by ppt containing in resources?
Actually, what you ask for is a common pattern and there are some related questions and answers here on SO.
Basically what you do in general is the following:
locate the resource in question and open a resource stream to it.
Save the stream to a (temporary) file if your target API cannot deal with streams or byte arrays directly.
Perform whatever operation on the file or directly on the stream/byte array (as I said, if supported).
Eventually remove the temporary file, if any, from step 1.
So, you first extract the PPT file (actually it doesn't really matter that it is a PPT file, could by any file or byte blob for that matter).
string tempFile = Path.GetTempFileName();
using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}
Then you open it using Process.Start(). You don't need to specify the path to the Powerpoint executable, as PPT should be a registered file extension with either PowerPoint or the PowerPoint Viewer. If you have both installed, you may still want to provide the path to the relevant executable to prevent launching the wrong application. Make sure that you don't hardcode the path though, but try to retrieve it from the registry (or similar, I haven't checked because that gets too specific now).
using (var process = Process.Start(tempFile))
{
process.WaitForExit();
// remove temporary file after use
File.Delete(tempFile);
}
Note: I left out quite some error handling that you might want to add in a real application.

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