我是.net开发者。在这里,我只想创建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();
这里只显示少数字段。
----------------------------------------- Updated-- ----------------------------------------------
这里我包含我的输出:
告诉我我的代码中有问题。
如需了解更多信息,请添加此article链接。
答案 0 :(得分:2)
转到您的代码:
table.AddCell(cell);
document.Close();
现在添加如下一行:
table.AddCell(cell);
document.Add(table);
document.Close();
您正在创建一个表对象,但您没有将其添加到文档中。