2014-02-24 74 views
0

我是.net開發人員。在這裏,我只是想創建PDF report.for更多信息我有這樣的代碼:如何使用asp.net 3.5創建PDF

Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f); 
     Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK); 
     using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) 
     { 
      PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); 
      Phrase phrase = null; 
      PdfPCell cell = null; 
      PdfPTable table = null; 
      Color color = null; 

      document.Open(); 

      //Header Table 
      table = new PdfPTable(2); 
      table.TotalWidth = 500f; 
      table.LockedWidth = true; 
      table.SetWidths(new float[] { 0.3f, 0.7f }); 

      //Company Logo 
      cell = ImageCell("../images/easyweb_logo.gif", 30f, PdfPCell.ALIGN_CENTER); 
      table.AddCell(cell); 

      //Company Name and Address 
      phrase = new Phrase(); 
      phrase.Add(new Chunk("Electrocom Technology India Limited\n\n", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED))); 
      phrase.Add(new Chunk("712, Sukhsagar Complex,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      phrase.Add(new Chunk("Nr. Hotel Fortune Landmark, Ashram Road,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      phrase.Add(new Chunk("Ahmedabad - 380 014,", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      phrase.Add(new Chunk("Gujarat (INDIA).", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
      cell.VerticalAlignment = PdfCell.ALIGN_TOP; 
      table.AddCell(cell); 

      //Separater Line 
      color = new Color(System.Drawing.ColorTranslator.FromHtml("#A9A9A9")); 
      DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color); 
      DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color); 
      document.Add(table); 

      table = new PdfPTable(2); 
      table.HorizontalAlignment = Element.ALIGN_LEFT; 
      table.SetWidths(new float[] { 0.3f, 1f }); 
      table.SpacingBefore = 20f; 

      //Post Detail 
      cell = PhraseCell(new Phrase("\n"+"Post Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, Color.BLACK)), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      table.AddCell(cell); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 30f; 
      table.AddCell(cell); 


      //Name 
      phrase = new Phrase(); 
      phrase.Add(new Chunk(dr["Title"] + "\n", FontFactory.GetFont("Arial", 10, Font.BOLD, Color.BLACK))); 
      cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
      cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; 
      table.AddCell(cell); 
      document.Add(table); 

      DrawLine(writer, 160f, 80f, 160f, 690f, Color.BLACK); 
      DrawLine(writer, 115f, document.Top - 200f, document.PageSize.Width - 100f, document.Top - 200f, Color.BLACK); 

      table = new PdfPTable(2); 
      table.SetWidths(new float[] { 0.5f, 2f }); 
      table.TotalWidth = 340f; 
      table.LockedWidth = true; 
      table.SpacingBefore = 20f; 
      table.HorizontalAlignment = Element.ALIGN_RIGHT; 

      //Title 
      table.AddCell(PhraseCell(new Phrase("Title :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["Title"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 


      //Description 
      table.AddCell(PhraseCell(new Phrase("Description :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["Description"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 

      //Category Name 
      table.AddCell(PhraseCell(new Phrase("Category Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["CategoryName"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 

      //Status Name 
      table.AddCell(PhraseCell(new Phrase("Status Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["StatusName"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 

      //Group Name 
      table.AddCell(PhraseCell(new Phrase("Group Name :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["GroupName"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 

      //Publisher 
      table.AddCell(PhraseCell(new Phrase("Publisher :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["Publisher"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 

      //To User 
      table.AddCell(PhraseCell(new Phrase("To User :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(dr["ToUser"] + "\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 


      //Publish Date 
      table.AddCell(PhraseCell(new Phrase("Publish Date :", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); 
      phrase = new Phrase(new Chunk(string.Format("{0:F}", dr["PublishDate"])+"\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK))); 
      table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); 
      cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); 
      cell.Colspan = 2; 
      cell.PaddingBottom = 10f; 
      table.AddCell(cell); 

      document.Close(); 
      byte[] bytes = memoryStream.ToArray(); 
      memoryStream.Close(); 
      Response.Clear(); 
      Response.ContentType = "application/pdf"; 
      Response.AddHeader("Content-Disposition", "attachment; filename=PostRecord.pdf"); 
      Response.ContentType = "application/pdf"; 
      Response.Buffer = true; 
      Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.BinaryWrite(bytes); 
      Response.End(); 
      Response.Close(); 

這裏有只顯示幾個字段。

-----------------------------------------更新--- ---------------------------------------------

這裏我包括我的輸出: enter image description here

告訴我wat是我的代碼中的問題。

for furthure更多的信息我包括這個article鏈接。

回答

2

轉到你的代碼,它說:

table.AddCell(cell); 
document.Close(); 

現在加上這樣一行:

​​

你正在創建一個表對象,但你不能將它添加到文檔。

+0

在這裏我有描述字段是ntext。我想展示它的原始視圖。你知道我的意思是什麼時候顯示HTML文本到原始視圖,我們使用文字控制。 –

+0

我不知道在這種情況下,ntext,原始視圖,啓用html的文本或文字控件的意思。你能否創建一個新的問題來澄清你的意思?我看到有人低估了你的問題。我的猜測是:你知道你想要問什麼,但是當你輸入你的問題時,你會省略一半的話。 –

+0

你可以看到我更新的問題。在此處顯示Description字段內容。這是[NTEXT]現在我想用它來對它進行html編碼/解碼,並且想要製作原始視圖。 –