pdf中没有显示任何内容

人气:176 发布:2022-09-22 标签: c#

问题描述

先生, 贝娄是我转换为pdf的代码。但没有任何内容以pdf显示

sir, bellow is my code for convert to pdf.but nothing displays in pdf

protected void Button1_Click(object sender, EventArgs e)

        {
            GeneratePDF();
        }
        private void GeneratePDF()
        {
            DataTable dtTemp = new DataTable();
            dtTemp.Columns.Add("Customer ID");
            dtTemp.Columns.Add("First Name");
            dtTemp.Columns.Add("Last Name");
            dtTemp.Columns.Add("Address");
            dtTemp.Rows.Add("0001", "Peter", "p", "2376, 00800, calicut");
            dtTemp.Rows.Add("0002", "Alfred", "k", "3453, 00800, calicut");
            dtTemp.Rows.Add("0003", "Alice", "n", "6056, 00800, calicut");
            dtTemp.Rows.Add("0004", "Denis", "h", "5672, 00800, calicut");
            dtTemp.Rows.Add("0005", "Paul", "k", "8734, 00800, calicut");
            Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
            var filename = Server.MapPath("test4.pdf");
            

            // var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);

            //iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);

            using (var output = File.Create(filename))
            {
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, output);
            }
            
            //System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            //PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
            int cols = dtTemp.Columns.Count;
            int rows = dtTemp.Rows.Count;
            pdfDoc.Open();
            iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
            pdfTable.BorderWidth = 1;
          
            pdfTable.Padding = 1;
            pdfTable.Spacing = 1;

            //creating table headers
            for (int i = 0; i < cols; i++)
            {
                Cell cellCols = new Cell();
                Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
                Chunk chunkCols = new Chunk(dtTemp.Columns[i].ColumnName, ColFont);
                cellCols.Add(chunkCols);
                pdfTable.AddCell(cellCols);

            }
            //creating table data (actual result)
            for (int k = 0; k < rows; k++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Cell cellRows = new Cell();
                    Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
                    Chunk chunkRows = new Chunk(dtTemp.Rows[k][j].ToString(), RowFont);
                    cellRows.Add(chunkRows);
                    pdfTable.AddCell(cellRows);

                }
            }

            pdfDoc.Add(pdfTable);
            
           
          
            //Response.ContentType = "application/octet-stream";
            //Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
           
            //Response.Clear();
            //Response.BinaryWrite(mStream.ToArray());

            //Response.End();
           
           

          
     

        }

[edit]已添加代码块 - OriginalGriff [/ edit]

[edit]Code block added - OriginalGriff[/edit]

推荐答案

嗯。 你会意识到什么是使用 bloc k,我假设? 那么,你怎么期望这个工作: Um. You do realise what a using block does, I assume? So, how do you expect this to work:
using (var output = File.Create(filename))
{
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, output);
}

你创建一个内存流,你将它连接到一个PDF文档......然后你关闭它然后把它扔掉...... 你不要试着写它,保存或发送到任何地方,所以你的整个代码总是没有...:笑: 所以看看扩展使用块,并在添加表格之后实际对数据执行某些操作,例如!

You create a memory stream, you connect it to a PDF document...and then you close it and throw it away... You don't try to write it, save it or send it anywhere, so the whole of your code does a total of nothing...:laugh: So look at extending the using block, and actually doing something with the data once you have added your table and such like!

831