2016-04-22 107 views
1

你好,這是我的C#代碼:未能在C#中加載PDF文檔

Byte[] bytes; 

int orderID = Convert.ToInt32(e.CommandArgument); 

var templateData = ordersBL.GetTemplateDetails(orderID); 

using (MemoryStream ms = new MemoryStream()) 
{ 

using (Document document = new Document(PageSize.A4, 10, 10, 10, 10)) 
{ 
PdfWriter writer = PdfWriter.GetInstance(document, ms); 
foreach (var temp in templateData.ToList()) 
{ 

string message = temp.Message; 
string tempimage = Convert.ToBase64String(temp.logo); 
string base64 = tempimage; 
byte[] imageBytes = Convert.FromBase64String(base64); 
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes); 
if (image.Height > image.Width) 
{ 
float percentage = 0.0f; 
percentage = 700/image.Height; 
image.ScalePercent(percentage * 100); 
} 
else 

{ 

float percentage = 0.0f; 
percentage = 140/image.Width; 
image.ScalePercent(percentage * 100); 
} 

if (!document.IsOpen()) 
{ 

document.Open(); 
} 
document.Add(image); 
using (var htmlWorker = new HTMLWorker(document)) 
{ 

using (var sr = new StringReader(message)) 
{ 


htmlWorker.Parse(sr); 
} 
} 
Paragraph paragraph = new Paragraph(); 
paragraph.SpacingBefore = 10; 
paragraph.SpacingAfter = 10; 
paragraph.Alignment = Element.ALIGN_LEFT; 
// paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN); 

document.Add(paragraph); 
document.NewPage(); 
} 
bytes = ms.ToArray(); 
document.Close(); 

} 
} 
Response.ContentType = "application/pdf"; 
string pdfName = "User"; 
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf"); 
Response.Buffer = true; 
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); 
Response.BinaryWrite(bytes); 
Response.End(); 
Response.Close(); 
Response.Flush(); 
} 

全文下載,但它顯示未能加載PDF文檔。

無法加載PDF文檔中的C#

我沒有找到,我在代碼中的錯誤。

請給我正確的代碼

謝謝。

+0

你''End''Close'代碼Flush'會不喜歡這樣的工作。請閱讀:https://blogs.msdn.microsoft.com/aspnetue/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation/ 和 http://stackoverflow.com/questions/2816194/use-of-response-flush-before-response-end – Alexander

回答

2

您並未創建完整的PDF文件。請看看我的問題的答案Trying to get a memory stream from a pdfstamper into a pdfreader but getting: "PDF startxref not found"

,當你關閉一個PDF文件定稿:

document.Close(); 

的PDF是不完整的,你這樣做之前

但是,你問的是MemoryStream關閉文檔前:

bytes = ms.ToArray(); 
document.Close(); 

這意味着bytes不包含完整的PDF。

此外,您正在使用HTMLWorker。該課程已於多年前被放棄,請閱讀documentation about XML Worker的介紹。

有可能HTMLWorker隱含關閉document對象(我不記得,因爲我說:HTMLWorker不再使用)。在這種情況下,將paragraph對象添加到文檔的代碼將引發異常,說明文檔已關閉,並且您不能再添加任何額外的內容。

2

@shivakumar,請嘗試像這個樣本,並更改​​您的數據。

Byte[] bytes = ImageToByteArray(System.Drawing.Image.FromFile(@"D:\Test\Sample1.png")); 
     //Converted Image to byte[] 

     using (MemoryStream ms = new MemoryStream()) 
     { 
      Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f); 
      PdfWriter.GetInstance(document, Response.OutputStream); 
      document.Open();  
      byte[] imageBytes = bytes; 
      ms.Write(bytes, 0, bytes.Length); 
      iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes); 
      if (image.Height > image.Width) 
      { 
       float percentage = 0.0f; 
       percentage = 700/image.Height; 
       image.ScalePercent(percentage * 100); 
      } 
      else 
      { 

       float percentage = 0.0f; 
       percentage = 140/image.Width; 
       image.ScalePercent(percentage * 100); 
      } 

      if (!document.IsOpen()) 
      { 

       document.Open(); 
      } 
      document.Add(image); 
      var htmlWorker = new HTMLWorker(document); 
      string message="hi"; 
      using (var sr = new StringReader(message)) 
      { 


       htmlWorker.Parse(sr); 
      } 

      bytes = ms.ToArray(); 
      document.Close(); 
     } 
     Response.ContentType = "application/pdf"; 
     string pdfName = "User"; 
     Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf"); 
     Response.Buffer = true; 
     Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); 
     Response.BinaryWrite(bytes); 
     Response.End(); 
     Response.Close(); 
     Response.Flush(); 
1

如果你有一個包含PDF一個有效的流,而你得到「無法加載PDF文件」從Chrome中,這肯定會導致一個問題:

Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf"); 

「附件」專告訴瀏覽器不是在瀏覽器中顯示此文檔,而是直接發送文件。我花了幾個小時瞭解到Chrome的PDF閱讀器在這種情況下窒息,可能是通過設計!

試試這個:

Response.AddHeader("Content-Disposition", "inline; filename=" + pdfName + ".pdf"); 

更多:Content-Disposition:What are the differences between "inline" and "attachment"?