为什么我的程序没有在浏览器中加载pdf? [英] Whydoesn't my program load a pdf in my browser?

查看:76
本文介绍了为什么我的程序没有在浏览器中加载pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所做的全部是编写以下代码,并在项目中创建了一个文件夹,并将pdf文档放入名为"test1.pdf"的文件夹中.我不知道我的程序是否命中了test.pdf ...由于某种原因,当我单击按钮时,我的程序没有在浏览器中加载pdf吗?我的代码有什么问题?请给我一个上帝的理由.



All I did is wrote the following code and created a folder in the project and put a pdf document in to the folder named "test1.pdf". I dont know if my program hit the test.pdf or not... for some reason my program does not load a pdf in browser when I click the button? What''s wrong in my code? Give me a god reason please.



protected void Page_Load(object sender, EventArgs e)
        {
            //Create Document class object and set its size to letter and 
            //give space left, right, Top, Bottom Margin
             Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter wri = PdfWriter.GetInstance(doc, new System.IO.FileStream("Test1.pdf", FileMode.Create)); doc.Open();//Open Document to write

            //Write some content
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
            Phrase pharse = new Phrase("This is my second line using Pharse.");
            Chunk chunk = new Chunk(" This is my third line using Chunk.");

            // Now add the above created text 
            doc.Add(paragraph); 
            doc.Add(pharse); 
            doc.Add(chunk);
            doc.Close(); //Close document

        }   

        protected void Button1_Click(object sender, EventArgs e)
        {
         //Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin  

     Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);  

     try 

     {  

        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test1.pdf", FileMode.Create));  
        //Open Document to write  
          doc.Open();  

        //Write some content  
   Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");  
         Phrase pharse = new Phrase("This is my second line using Pharse.");  
         Chunk chunk = new Chunk(" This is my third line using Chunk."); 
 
         // Now add the above created text  
         doc.Add(paragraph);  
         doc.Add(pharse);  
         doc.Add(chunk);  

     }  

     catch (DocumentException dex)  

     {  

         //Handle document exception  

     }  

     catch (IOException ioex)  

     {  

         //Handle IO exception  

     }  

     catch (Exception ex)  

     {  

         //Handle Other Exception  

     }  

     finally 

     {  

         doc.Close(); //Close document  

     }  



           
        }

推荐答案

您的代码中没有任何地方实际尝试将PDF输出写入响应流.您在此处拥有的代码将文件写入FileStream,代表文件存储.您可以在其中写入任意数量的文件,但是无需实际选择一个文件并将其流式传输到响应流,就不会看到正在下载的文件.

当您写入响应流时,您应该真正记住要设置ContentType.这是您需要执行的操作的示例:
There''s nowhere in your code where you actually try to write the PDF output to the response stream. The code that you have here writes the file to the FileStream, which represents the file storage. You can write as many files as you like in there, but without actually picking one and streaming it to the response stream, you aren''t going to see the file being downloaded.

When you write to the response stream, you should really remember to set the ContentType. Here''s a sample of what you need to do:
private void WriteToResponseStream(string fileName)
{
  Response.ContentType = "Application/pdf";
  Response.WriteFile(MapPath(fileName));
  Response.End();
}


这篇关于为什么我的程序没有在浏览器中加载pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆