Skip to main content
2 de 2
conteúdo editado
Marconcilio Souza
  • 12,6mil
  • 10
  • 40
  • 106

Seu HTML esta faltando a tag de fechamento .

Veja abaixo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

namespace WebApplication1tiraduvidas
{
    public partial class PaginaQueSeraConvertida : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            criatabela();
            ConverteAspx2Pdf();
        }

        private void criatabela()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table width='95%' border='0'><tr><td width='8%' >Protocolo:</td>");
            sb.Append("<td width='8%' >Devedor:</td>");
            sb.Append("<td width='16%' >CNPJ/CPF:</td>");
            sb.Append("</tr><tr>");
            sb.Append("<td>Título:</td>");
            sb.Append("<td >Apresentante:</td>");
            sb.Append("<td >Data Vencimento:</td>");
            sb.Append("</tr><tr>");
            sb.Append("<td >Valor:</td>");
            sb.Append("<td >Saldo:</td>");
            sb.Append("<td >Data apontamento:</td>");
            sb.Append("</tr><tr>");
            sb.Append("<td >Livro:</td>");
            sb.Append("<td >Folha:</td>");
            sb.Append("<td >Data protesto:</td>");
            sb.Append("</tr></table>");
            Principal.InnerHtml = sb.ToString();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        private void ConverteAspx2Pdf()
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            this.Page.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }
    }
}

Também use o método:

public override void VerifyRenderingInServerForm(Control control)
            {
            }

e no aspx

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="PaginaQueSeraConvertida.aspx.cs"
  Inherits="WebApplication1tiraduvidas.PaginaQueSeraConvertida" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div id="Principal" runat="server">
    Exportação de ASPX para PDF
  </div>
  </form>
</body>
</html>
Marconcilio Souza
  • 12,6mil
  • 10
  • 40
  • 106