Open a PDF from a server in Edge or Chrome - c#

I am working on a program created some years ago by a different programmer that, when a user clicks a link on a webpage, allows for a series of selections via dropdown boxes and such, which are used to populate a template PDF that is then saved as a new PDF. The program is then supposed to open that new PDF in that browser for the user to review and perhaps make further edits. This program is located on a virtual server along with many other web apps and related files. The template PDF and the new PDF file are located in the same place as the rest of the program.
From my testing, I can confirm that the link, the selections, and the populating of the template PDF work fine. A new PDF is saved with the proper selections, and I can find it in File Explorer on the server. I can even copy that new PDF from the server and paste it to my local device to then manually open for review.
The problem is that for some reason whenever the program itself tries to open the PDF, it comes up as a webpage of garbled text. I actually get the exact same output if I copy and paste the PDF to my local device and then change the extension to .html instead of .pdf (for what that is worth). I have tried multiple different means of opening the file.
The program uses iTextSharp to manipulate the PDF. I do not know much about iTextSharp so I do not know if that is part of the issue or not. I believe the code that is having the issue is as follows:
Server.Transfer("xp2Forms/completed_PDF" + indID + ".pdf");
File.Open(#"D:\Web Apps\IT Testing\xp2Forms\completed_PDF" + indID + ".pdf", FileMode.Open);
I have also tried replacing the last line with:
System.Diagnostics.Process.Start(#"D:\Web Apps\IT Testing\xp2Forms\completed_PDF" + indID + ".pdf");
The original code had:
OpenFile(#"D:\Web Apps\xp2Forms\Completed\completed_PDF" + indID + ".pdf");
Final piece of information. The PDF opens just fine in Internet Explorer. It just will not open in Chrome or Edge, at least not directly from the program. Again, I can open/paste and "Open With" Chrome or Edge no problem...
Thoughts?

I was able to address the issue by changing the "Server.Transer" line to "Response.Redirect" and now the PDF loads properly in both Microsoft Edge and Google Chrome.

Related

How can I open in the Adobe Reader application("AcroRd32.exe"), a PDF online with url?

So my problem is I'm trying to open a PDF file to sign the document, but the document only exists online. To sign the PDF I would like to click in a Button and it would open the PDF that i want, but in the Adobe Reader application. So that the user could do whatever he wants in the PDF.
Need some help on the part of open a pdf that only exists online, but in the application od the adobe Reader.
Thanks any help!!
EDIT1:
So the problem is that my boss do not want the PDF to be download. He wants to click the button and open in the Adobe Reader Application...
In order to open the file from AdobeReader locally, you are going to have to copy at minimum a temp file locally. Otherwise, you would have to either create or use a web application that is capable of accessing these files. Either way whatever is accessing the PDF is going to need a copy to display whether it's on your local machine or on a server somewhere.
Any PDF that exists online can be downloaded. Once downloaded, you can open it in any PDF reader like AdobeReader. Are you sure you are not able to download it?

Cannot open PDF Document. The browser loads for a second or two but then nothing happens

Actually the issue is only with the PDF files in my asp.net application, is not opening (or) showing print print preview.
I click on a specific PDF Document and then click the print button. The browser loads for a second or two but then nothing happens. No document opens, nothing is printed, where as Microsoft Office documents (Word, Excel) will open up when I click the printer icon, which is normal. Documents with extension .txt and .jpg also behave normally. However there is a definite problem with PDFs.
On my machine (Windows 10) I tested IE 11 and Chrome and had these same exact issues on both browsers.
again i tested this on another machine (Windows 7 and IE 9) and experienced NONE of these issues. Everything worked as expected.
Have you correctly set the content type header to be
"Content-type:application/pdf"
and the content disposition header to be?
"Content-Disposition:attachment;filename='somefile.pdf'"
If you using some library for displaying pdf files - you need to reinstall it. Also such problem can be from ad blocker, try to disable it.

Two files that are binary identical, yet exhibit different behavior

I'm posting with tags asp.net and excel because that is the origination of my problem, but I'm not really sure this is the right place - ultimately, my problem is that I have two files (served by an ASP.Net application) which are identical based on a binary file compare using
fc /B A.xls B.xls
However, they exhibit different behavior: the first one opens fine in Excel; the second one does not. I conclude, then, that there is something different about the files beyond what the FC utility checks.
I have tried sending these two files to a friend to ask for his help, but discovered that when I do so, the problem file gets "fixed". In fact, if I do just about anything with this file, it gets "fixed". By fixed, I mean that it then opens fine in Excel. For example, if I zip it, then extract it from the zip, it is fine. If I open in Notepad++ and "Save As", it is fine. Same with Wordpad. Using plain old Notepad does NOT fix it.
So, obviously, there is some difference about these two files that I am missing.
I'm not sure if I will have any luck asking people to visit a random website, but if you want to see an example of the behavior, I have created a minimal page to duplicate the problem at http://rodj.me/ExcelTest
Click on the link for "MinimalHtml.aspx", and the app will serve an HTML based xls file using the following in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "filename=MinimalHtml.xls");
}
Depending on your browser and browser settings (my tests have been in Chrome), you may get Excel opened with a blank page. Regardless, you should get the file MinimalHtml.xls downloaded. It is a plain text file. You should find that this file will NOT open in Excel. However, if you zip the file, then extract it from zip, it WILL open.
I'm curious about what other file differences I'm missing when just doing an FC compare, but ultimately, I need to get the ASP.Net application corrected to serve the HTML version of the Excel file correctly. Interestingly, if I create an XML version of the spreadsheet, it downloads/opens fine. That is what the "MinimalXml.aspx" link does.
Can anyone help with either 1) how to figure out what is different about the two files; or 2) what must change in the ASP.Net application to get it to serve the file correctly?
I think your problem might be a Microsoft security patch. See this article:
Infoworld article
When you open the file directly, the patch causes the issue which results in a blank page because the file contents is HTML not Excel. When you download the file in a Zip file and unzip it, it is deemed safe and opens correctly.

C# open file from physical path

sorry for asking a silly question it seems to be,
I am new to programming and trying to write a asp .net web application.
One part of the application had generated and saved some document
(like pdf, excel, words, gif, jpg, etc.) to a physical path in server side,
User can choose to download the file by clicking a button
is there something like "window.open(url)" that can raise a file download dialog box with known document type in javascript / c#?
Thanks a lot!!

Link to open a PDF file that's saved in a folder?

Hi there. I think this one is a tough one, but I hope someone can help.
I have a folder which is downloaded from my website. The folder contains a Main PDF report on a housing property, and another folder containing pictures and more PDF reports on similar houses in the area.
On my main PDF report I would like to have a link to open the other PDF folders.
How would I go about creating a link to open a PDF report saved in a file?
Looking at the picture below, where it says CS1.pdf, that is the name of the PDF and where the link will be. This is a screen shot of a portion of my main PDF.
I don't mind if the PDF reports are opened in a webpage, in fact I would prefer it. But, at the moment, I just need a simple link created that will open a PDF located in a folder.
An explanation of the app flow:
Here below I save the PDF that I want to link to later. It's saved to a temp location; the exact location is not important as it will be deleted after the file uploads.
The file I upload contains a few pictures, some PDFs and a data.XML file. When a person downloads the file, the data.XML file is used to create my main PDF, or report, in any format. I need to send a link through the XML file that will appear on the report making it able to link to the other PDF files that were uploaded with the data.xml file.
CS1[12].TagValue = ReportDS.Tables[9].Rows[0].ItemArray[0].ToString();
//Save PDF
//PDFName = System.Guid.NewGuid().ToString() + ".pdf";
string PDFName = "CS1" + ".pdf";
WebClient webClient = new WebClient();
webClient.DownloadFile(CS1[12].TagValue, "C://Users//Shaun//Documents//FormValue//" + PDFName);
CS1[12].TagValue = PDFName; //This is the value passed to the main PDF so this is where i should pass the link through.
The line CS1[12].TagValue = PDFName; is where I set the value to send though to the main PDF, and this is where my link should be sent through.
I need something like CS1[12].TagValue = "http://C://Users//Shaun//Downloads//CS1.pdf"; but with the file path of the downloaded location.
you are on a right way but you have some mistake like, when you giving location of pdf file from downloaded location, that must be a file type, its not from a server...
CS1[12].TagValue = "http://C://Users//Shaun//Downloads//CS1.pdf";
but it must like this
CS1[12].TagValue = "file://C://Users//Shaun//Downloads//CS1.pdf";
so that you have change one line in your code...
webClient.DownloadFile(CS1[12].TagValue, "file://C:/Users//Shaun//Documents//FormValue//" + PDFName);

Categories