使用C#导出资源HTML [英] Export Resource HTML using C#
本文介绍了使用C#导出资源HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码:
i have a code bellow:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Net;
using iTextSharp.text.html;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Text = ReadHTMLCode("Default2.aspx");
}
private string ReadHTMLCode(string URL)
{
try
{
WebClient webClient = new WebClient();
byte[] reqHTML = webClient.DownloadData(URL);
UTF8Encoding objUTF8 = new UTF8Encoding();
return objUTF8.GetString(reqHTML);
}
catch (Exception Ex)
{
}
return "error";
}
protected void Button1_Click(object sender, System.EventArgs e)
{
this.TextBox1.Text = this.TextBox1.Text.Replace("\"\"", "''''");
String htmlText = this.TextBox1.Text;
HTMLToPdf(htmlText, "PDFfile.pdf");
}
public void HTMLToPdf(string HTML, string FilePath)
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\PDFfile.pdf", FileMode.Create));
document.Open();
//Image pdfImage = Image.GetInstance(Server.MapPath("logo.png"));
//pdfImage.ScaleToFit(100, 50);
//pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);
//document.Add(pdfImage);
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(HTML));
document.Close();
ShowPdf("PDFfile.pdf");
}
private void ShowPdf(string s)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + s);
Response.ContentType = "application/pdf";
Response.WriteFile(s);
Response.Flush();
Response.Clear();
}
}
我想读取资源default2.aspx并将其转换为字符串.
请帮助我
i want to read resource default2.aspx and convert it to string.
please help me
推荐答案
您要查看HttpWebRequesat
对象.当您使用该对象发出Web请求时,可以将返回的网页转换为字符串.这是关于它的CodeProjecxt文章:
简单地说,HttpWebRequest/响应-第1部分 [
You want to look at theHttpWebRequesat
object. When you make a web request with that object, the web page returned can be converted to a string. here''s a CodeProjecxt article about it:
HttpWebRequest/Response in a Nutshell - Part 1[^]
这篇关于使用C#导出资源HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文