Aspx页面到Pdf转换 [英] Aspx page to Pdf convert

查看:76
本文介绍了Aspx页面到Pdf转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整个aspx页面转换为Textbox,label,Button和gridview [所有控件]为pdf。?



任何帮助都会非常感激。

How to Convert Whole aspx page with Textbox, label , Button and gridview[All the controls] to pdf.?

Any help would be really appreciated.

推荐答案

他们有很多解决方案...



1.你可以将它打印到。 PDF



2.使用iTextSharp,您可以使用以下代码(最初位于 http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html [ ^ ]



Their are many solutions to this ...

1. You can print it to a .PDF

2. Using iTextSharp you can use the following code (originally found on http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html[^]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Xml;
using iTextSharp.text.html.simpleparser;
public partial class Pdf : System.Web.UI.Page
{
    protected override void Render(HtmlTextWriter writer)
    {
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);
        strmRdr.BaseStream.Position = 0;
        string pageContent = strmRdr.ReadToEnd();
        strmRdr.Dispose();
        mem.Dispose();
        writer.Write(pageContent);
        CreatePDFDocument(pageContent);


    }
    public  void CreatePDFDocument(string strHtml)
    {

        string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
        // step 1: creation of a document-object
        Document document = new Document();
        // step 2:
        // we create a writer that listens to the document
        PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
        StringReader se = new StringReader(strHtml);
        HTMLWorker obj = new HTMLWorker(document);
        document.Open();
        obj.Parse(se);
        document.Close();
        ShowPdf(strFileName);
     
   
     
    }
    public void ShowPdf(string strFileName)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        Response.ContentType = "application/pdf";
        Response.WriteFile(strFileName);
        Response.Flush();
        Response.Clear();
    }
}





3.(在我看来最糟糕的方式)你可以将网页保存为JPEG并从这些图像创建PDF。



3. (The worst way in my opinion) You can save the web pages as a JPEG and create a PDF from those images.


这篇关于Aspx页面到Pdf转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆