将网页转换为pdf [英] Convert webpage to pdf

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

问题描述

我想将我的网页转换为pdf,因为有必要先构建html,然后将该html转换为pdf?

在此先感谢.

I want to convert my webpage to pdf , for that is it necessary to to build html then convert that html to pdf?

Thanks in Advance.

推荐答案

您可以使用iTextSharp库将网页内容转换为PDF.下载ITextsharp并将其引用添加到您的项目中. ITextsharp是位于 http://sourceforge.net/projects/itextsharp/ [
You can convert the web page content into PDF using iTextSharp library. Download ITextsharp and add its reference to your project. ITextsharp is a free HTML to PDF Library at http://sourceforge.net/projects/itextsharp/[^]

using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
......
protected void btnExport_Click(object sender, EventArgs e)
{
    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);
}


这里,这些应该有所帮助:
如何从ASP.NET页面创建PDF文件 [ ^ ]
使用iTextSharp(GridView)将HTML导出到Pdf [ ^ ]
Here, these should help:
How to create PDF files from ASP.NET pages[^]
Export Html to Pdf using iTextSharp(GridView)[^]


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

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