2016-04-22 11 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#

でPDF文書のロードに失敗しました私はどこ私は、コードに誤りが見つかりませんでした。

正しいコードを教えてください。

ありがとうございます。

+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ファイルを作成していません。 PDFは、あなたがこれを行う前完全なものではありません

document.Close(); 

:あなたはそれを閉じると、PDF文書が確定される質問Trying to get a memory stream from a pdfstamper into a pdfreader but getting: "PDF startxref not found"

に私の答えをご覧ください。

はしかし、あなたは、ドキュメントを閉じるMemoryStreamを尋ねる:

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

これはbytesは完全なPDFを含まないことを意味します。

さらに、HTMLWorkerを使用しています。そのクラスは何年も前に放棄されました。documentation about XML Workerのイントロを読んでください。

それはHTMLWorkerは、暗黙的に(私は言っていないとして、私は、覚えていない:HTMLWorkerはもはや使用されている)documentオブジェクトをクローズしている可能性があります。その場合、ドキュメントにparagraphオブジェクトを追加するコードは、ドキュメントが閉じられており、余分なコンテンツを追加できなくなるという例外をスローします。

2

@shivakumarこのサンプルのようにして、データを変更してください。特に

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

「添付ファイル」:あなたがPDFを含む有効な流れを持っている、とあなたがChromeから「PDFドキュメントを読み込むことができませんでした」取得している場合

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

、これは間違いなく問題が発生しますブラウザにこの文書を表示する代わりに、直接ファイルを送信するように指示します。そして私はちょうどChromeのPDFビューアがそのような状況で、たぶん設計上の問題を引き起こしていることを覚えています。

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

より:

はこのお試しくださいContent-Disposition:What are the differences between "inline" and "attachment"?

関連する問題