Saving DOCX Documents to a server - c#

I put you in situation in C# I'm realizing a program that modifies DOCX documents and creates new documents both DOCX and PDF. In local I do it perfectly but when I want DOCX to read it from the server or save it on the server does not do it to me, the maximum is that it opens the folder in which it is stored on the server but with the document does nothing . Comment that the program I am developing for MAC OSX and the server is Windows Server.
Someone can tell me the code I can use.
To read usage:
Process.Start ("smb // servername / folder / doc.docx");
Also tried by giving you username and password:
Process.Start (serverpaths, user, password, "");
To save to the server and create document:
PDF use the iTextSharp library:
PdfWriter writer = PdfWriter.GetInstance (doc, new FileStream (path, FileMode.Create);

Related

How to download a pdf files having application/font-woff network type using selenium web driver

I am trying to download a pdf file which is having application/font-woff network type.I have used so many preferences but then also unable to download the pdf file still asking for the save loaction.enter image description here
options.SetPreference("browser.helperApps.alwaysAsk.force", false);
options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/font-woff");
options.SetPreference("browser.download.folderList", 2);

Not generating a PDF documents using CutePDF writer in windows service application C#

I converted text files to PDF documents using CutePDF Writer printer in windows forms application.It's working as expected but same block of code I used in c# windows service application to convert text files to PDF files.I'm not getting any exceptions while executing the code and not generating any PDF documents.
I created the following registry key settings for CutePDF writer
Setting the display mode:
HKEY_CURRENT_USER\Software\CutePDF Writer\BypassSaveAs
(0 = show Save as dialog box after spooling, 1 = do not show Save as dialog box.)
(This value is of type REG_SZ, not REG_DWORD)
Setting the filename:
HKEY_CURRENT_USER\Software\CutePDF Writer\OutputFile
(Use the key above to set the output file for the PDF. A full pathname
(e.g. d:\your folder\your file.pdf) is required.)
Not familiar with CutePDF Writer - but: as which user is the service running?
To clarify this: if the service runs as LocalService, the registry key HKEY_CURRENT_USER is not the same as the one from loged in user.
If that is the case, try HKEY_USERS\S-1-5-19 instead of HKEY_CURRENT_USER.

How to get file path and name to save this file?

I use open XML to export excel file. SpreadsheetDocument.Create requires a file destination.
I want to get this file destination from user similar to a SaveFileDialog in Winforms.
How I get file destination from user? I use Asp.Net 4.0 and OpenXML SDK 2.5 .
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(destination, SpreadsheetDocumentType.Workbook);
For this functionality even if you use open/save dialog, it would show the client locations not the server locations. So here question is, do you want to save that file to the client machine or at the server machine?
If you want to save it to client machine then first save it on the server by any name and location you want and then initiate a download after that. It will automatically prompt download dialog in the client browser and there user would be able to select the location and filename (depending on the client browser settings).
If you want to save it only on server then just before generating the spreadsheet, prompt user for entering the filename (on the page from where the request is generated). If some of your folders are accessible to user then you can also prompt user to select one of the folders, otherwise choose one as per your choice/requirement. Use the filename and the location to generate the spreadsheet.
when a webserver is streaming a file down to the client, you don't have control of the destination folder. All you can do is:
specify the contents type (to aid client in determining how to handle)
specify filename
(I think) tell the client browser to save the file vs. displaying it automatically in the registered application (for example, PDFs usually display in the browser, but setting up the streaming correctly could force Save File dialog instead)
If you need to "cache" the file ion the web server, you export to a server folder that your server process has write permissions to. Then you stream to the client - the client will get the prompt from their browser and save where they want.
Look at Server.MapPath for example - it'll map virtual path to physical on your server. the question of permissions remains.
Depending on your particular case, you may be able to avoid saving to the server, if the export library has a way of returning a byte array or a stream, instead of saving to a file. In that case you just stream the return result to requester.
I am not familiar with the SDK you're using, but quick googling reveals this method of returning the document in a stream:
using (MemoryStream stream = new MemoryStream())
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
{
... work with the spreadsheetDocument, if needed
... prepare and stream to browser
}
}
Here's one of the references to your SDK I found
Use Environment.SpecialFolder enumerations. If you are looking for my documents then:
var destination = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(destination, SpreadsheetDocumentType.Workbook);

Cannot open pdf created by ITextSharp

I am using ITextSharp for creating pdf in ASP.net, every thing works fine on my local machine, when I run it on IIS server, pdf is created successfully and can be open and view in the folder it is made, but I cannot open the pdf programmatically from C#.
I am using Process.Start(path) to open the file.
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("TransferLetter/" + filenamee), FileMode.Create));
doc.Open();
DateTime date = DateTime.Now.Date;
html = html.Replace("[Date]", Request["Date"] + "");
html = html.Replace("[Address]", Request["MailingAddress"].ToString());
html = html.Replace("[PlotNo]", Request["PlotNumber"].ToString());
html = html.Replace("[Block]", Request["Block"].ToString());
html = html.Replace("[Size]", Request["PlotSize"].ToString());
string pa = Server.MapPath("TransferLetter/" + filenamee);
System.Diagnostics.Process.Start(pa);
This will never work this way. System.Diagnostics.Process.Start() runs on the server, not the client. When you're developing on your local machine you are both the server and the client so it appears to work but once you separate these two you get your failure.
To say that again but in a different way, you are asking the server to build your PDF and then you are instructing the server to launch its local copy of Adobe Reader and display the PDF to whoever is physically logged into the server at the moment. (Ok, not 100% true but pretty close.)
Instead you need to send the PDF to the client using something like Response.Redirect() or Response.Write(). Looking at your code you should be able to perform:
Response.Redirect("TransferLetter/" + filenamee)
Using iTextSharp you don't actually have to even write the PDF to disk. You could use a MemoryStream instead of a FileStream, call MemoryStream.ToArray() write before disposing of it and using Response.Write() on that byte array. If you are writing small PDFs and/or are planning on multiple people accessing this that might be the safer way.
you need to use ~ tilde operator to specify the current project folder path
Try This:
string pa = Server.MapPath("~/TransferLetter/" + filenamee);

A default path when downloading PDF file in computer/mobile phone

I created a PDF webapp where users are able to generate various type of PDF on both the computer and mobile phone. However, i run my program on a localhost and this is how i save my PDF based on my computer's file directory
var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
However, when i publish my webapp onto azure, i wasn't able to download from both my computer and mobile phone. Therefore i believe that it could be due to my default file directory.
Hence i would like to ask how to do a default file directory for all computer and mobile phone?
Or could it be i left out something that is necessary when the webapp is published online
Thanks.
PS : I hardcoded a default file path in order for me to test my application on a localhost to ensure a perfect working condition. Therefore i'm finding a way to find a default common file directory for all mobile/computer users when they attempt to download the PDF instead of my usual hard-coded file path
UPDATE
I tried using the method Server.MapPath but receive some error.
var doc1 = new Document();
var filename = Server.MapPath("~/pdf") + "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
// var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
//iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
using (var output = File.Create(filename))
{
iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
}
doc1.Open();
This is the error i received
ObjectDisposedException was unhandled by user code
Cannot access a closed file.
When you write a Web Application you shall never use hard coded paths, and the last place where you should save files is C:\Users !! It does not matter whether this is Azure or not. It is general rule for any kind of web applications!
In your case I suggest that you create a folder within your application named pdf or something like that and save files there with the following code:
var fileName = Server.MapPath("~/pdf") + filename;
using (var output = File.Create(fileName) )
{
// do what you want with that stream
// usually generate the file and send to the end user
}
However there is even more efficient way. Use the Response.OutputStream and write the resulted PDF directly to the response. Will save you a lot of space on the local server, and the logic to delete unused generated files.

Categories