iTextSharp(将aspx页面转换为pdf)

时间:2016-05-27 17:35:53

标签: c# asp.net pdf itextsharp

我正在尝试为我的课程项目实施演示证书问题。在将aspx页面转换为pdf时,图像根本不会显示。此处使用的图像位于我自己的笔记本电脑中,与Certification.aspx文件位于同一文件夹中。在另一台笔记本电脑中,图像显示一次,但图像不再显示。它是随机发生的。

请帮忙。

感谢。

这是Certificatication.aspx页面

此页面显示课程门户徽标作为图像和有关证书的文本行。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Certification.aspx.cs" Inherits="Certification" EnableEventValidation="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

   <!-- <asp:Image ID="Image1" runat="server" Height="402px" ImageUrl="certi.png" Width="923px" />  -->
    <br />
    <br />
    <br />




    <asp:Panel ID="Panel1" runat="server" Height="120px">

       <!--  <img src = "http://www.aspsnippets.com/images/Blue/Logo.png" /><br />  -->

         <img src="E:\Pictures\1.jpg"  height="402" width="923" alt="Image" /><br/>
        <asp:Label ID="Label3" runat="server"></asp:Label>
    <br />
    <br />
    <b><i>Has successfully completed the requirements to complete the course on</i></b>&nbsp;&nbsp;
    <asp:Label ID="Label4" runat="server"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Click to download as PDF." OnClick="Button1_Click1" />

    </asp:Panel>




</div>
</form>

这是Certification.aspx.cs文件

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


public partial class Certification : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string marks="",name="",course="";
        int marks1=0;
        string id = Session["email"].ToString();
        SqlConnection conn = new SqlConnection(); //creating connection    reference object

    //conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\My Documents\Final Year Project\finalyear\finalyear\App_Data\newdatabase.mdf;Integrated Security=True";
    conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\My Documents\Final Year Project\finalyear\finalyear\App_Data\newdatabase.mdf;Integrated Security=True";
    conn.Open();  //opening connection
    string q = "select * from selectcourse where selectcourse.s_emailid='" + id + "'";
    SqlCommand cmd = new SqlCommand(q, conn);
    //execute query
    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read() == true)
    {
        marks = reader.GetValue(3).ToString();
        marks1 = Convert.ToInt32(marks);

    }
    reader.Dispose();
    cmd.Dispose();

        string q1 = "select * from studentinfo where studentinfo.s_emailid='" + id + "'";
        SqlCommand cmd1 = new SqlCommand(q1, conn);
        //execute query
        SqlDataReader reader1 = cmd1.ExecuteReader();

        if (reader1.Read() == true)
        {
            name = reader1.GetValue(1).ToString();
        }
        course = Session["course"].ToString();

        Label3.Text = name.ToString();
        Label4.Text = course.ToString();


}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
    string attachment = "attachment; filename=" + "abc" + ".pdf";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter s_tw = new StringWriter();
    HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
    h_textw.AddStyleAttribute("font-size", "7pt");
    h_textw.AddStyleAttribute("color", "Black");
    Panel1.RenderControl(h_textw);//Name of the Panel
    Document doc = new Document();
    doc = new Document(PageSize.A4, 5, 5, 15, 5);
    FontFactory.GetFont("Verdana", 80);
    PdfWriter.GetInstance(doc, Response.OutputStream);
    doc.Open();
    StringReader s_tr = new StringReader(s_tw.ToString());
    HTMLWorker html_worker = new HTMLWorker(doc);
    html_worker.Parse(s_tr);
    /*var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("certi.png"));
    logo.SetAbsolutePosition(0, 0);
    doc.Add(logo);*/ 
    doc.Close();
    Response.Write(doc);

}
}

单击上面的按钮,aspx页面将转换为pdf。

0 个答案:

没有答案