1

I have copied code previously used throughout the system i am working on. However this code opens the content in a word document. I am looking it to be opened in a PDF.

I have already tried changing the string declaration 'filename' to end in (.pdf) as opposed to (.doc) but when attempting to open it it says "could not open the document because it is either not a spported file type or because the file has been damaged....".

What changes need to be made to this code in order to open it as an adope pdf. I wouldnt imagine it would be alot.

string content = sw.GetStringBuilder().ToString();
string fileName = "IRPBestPracticeArticle.doc";

Response.AppendHeader("Content-Type", "application/msword; charset=utf-8");
Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
Response.Charset = "utf-8";
Response.Write(content);
Paul Kirkason
  • 227
  • 2
  • 4
  • 20
  • Changing the filename or content type will not magically convert your document. What you are showing are in no way relevant. Are you sure you know what you are doing? – rasmus Aug 26 '13 at 11:38
  • obviously not, that is why i am asking on this!!! i am looking for the easiest possible method to change the code provided to have it opening the content in PDF format. – Paul Kirkason Aug 26 '13 at 11:40
  • you want to open the .doc into .pdf file format? – backtrack Aug 26 '13 at 11:42
  • 1
    What you are looking for is a method to convert the actual content that you get from sw to a PDF. Have a look at this [question](http://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically). – rasmus Aug 26 '13 at 11:43
  • yes, i want it to open in .pdf (adope reader) format as opposed to .doc (microsoft word) format which it is opening in currently. – Paul Kirkason Aug 26 '13 at 11:45

4 Answers4

1

I cannot say for certain, but I am going to assume you're trying to save your data as a pdf and have it open in whatever application the system uses to read pdf files?

//Note the change from application/msword to application/pdf
Response.AppendHeader("Content-Type", "application/pdf; charset=utf-8");

Make sure to change the mime type as well as the doc ending (See here for full list of mime types): That being said, I cant guarantee it will open properly in your PDF reader

Melvin DVaz
  • 1,234
  • 6
  • 5
1

Just try this set of code.

Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + fileName);
Response.ContentType = "application/pdf";
Response.WriteFile("FullFilePath");
Response.Flush();
Response.Clear();
Response.End();

The mime type need to be set correctly before opening the file.

Robin Joseph
  • 1,210
  • 1
  • 11
  • 12
  • This the closest I've got to having it work i think. However, when you state to enter "Response.WriteFile("FullFilePath");" i am not opening the PDF via it being previously created, it is being created on the fly or dynamically when clicking the button. Currently the content is being converted to a .doc format and opened in word, i want the content to be converted into pdf and opened in adobe reader. – Paul Kirkason Aug 26 '13 at 13:35
  • In what format you are having the content.Is it in Byte format? – Robin Joseph Aug 26 '13 at 14:50
  • the content is in string form. It is being accessed through a html writer webcontrol, where the ID is passed in and the content is •returned.this.TopicDocumentControl.IRPBestPracticeTopicID = this._irpBestPracticeTopicID; • Control myControl = this.TopicDocumentControl; • myControl.RenderControl(textWriter); – Paul Kirkason Aug 26 '13 at 15:02
0

Andy try this one. You must have ItextSharp.dll to use this code. Download it from here. Then add its reference in your page.

try this code to create pdf from string and download it

Document document = new Document(PageSize.A4); 
 using (MemoryStream ms = new MemoryStream())
            {
                PdfWriter.GetInstance(document, ms);
                document.Open();
System.Xml.XmlTextReader _xmlr;
                if (String.IsNullOrEmpty(errorMsg))
                    _xmlr = new System.Xml.XmlTextReader(new StringReader(GetTransferedData(content)));
                else
                    _xmlr = new System.Xml.XmlTextReader(new StringReader(@"<html><body>Error Message:" + errorMsg + "</body></html>"));
                iTextSharp.text.html.HtmlParser.Parse(document, _xmlr);
                document.Close();                 
                ms.Flush();
                byte[] data = ms.ToArray();

                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.Buffer = true;
                Response.ContentType = "application/pdf";
                Response.BinaryWrite(data);
                Response.End();
                ms.Close();
            }
Robin Joseph
  • 1,210
  • 1
  • 11
  • 12
0

First, convert the .doc files to PDF files. Here is a sample of how to achieve this: Convert Doc file to PDF in VB.Net

After you have the PDF files, stream them to the browser using the "application/pdf" content type.

Community
  • 1
  • 1
Nicholas Post
  • 1,857
  • 1
  • 18
  • 31