本文整理汇总了C#中iTextSharp.text.html.simpleparser.HTMLWorker类的典型用法代码示例。如果您正苦于以下问题:C# HTMLWorker类的具体用法?C# HTMLWorker怎么用?C# HTMLWorker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HTMLWorker类属于iTextSharp.text.html.simpleparser命名空间,在下文中一共展示了HTMLWorker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPDF
public byte[] GetPDF(string pHTML)
{
byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 10, 10, 10, 5); //era tudo 25
// 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
// 3: we create a worker parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);
// 4: we open document and start the worker on the document
doc.Open();
htmlWorker.StartDocument();
// 5: parse the html into the document
htmlWorker.Parse(txtReader);
// 6: close the document and the worker
htmlWorker.EndDocument();
htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
return bPDF;
}
示例2: createPDF
private MemoryStream createPDF(string html)
{
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
return msOutput;
}
示例3: RenderHtmlToPdfStream
public static MemoryStream RenderHtmlToPdfStream(string html)
{
var memoryStream = new MemoryStream();
var reader = new StringReader(html);
using (var document = new Document(PageSize.A4, 30, 30, 30, 30))
{
using (var worker = new HTMLWorker(document))
{
using (var writer = PdfWriter.GetInstance(document, memoryStream))
{
writer.CloseStream = false;
document.Open();
worker.StartDocument();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();
}
}
}
memoryStream.Seek(0, 0);
return memoryStream;
}
示例4: ExportToPDF
private void ExportToPDF()
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To Export all pages
this.gvExportToPdf.AllowPaging = false;
//this.BindGridView();
this.gvExportToPdf.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=DashboardReport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
}
示例5: btnPDF_Click
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
var sw = new StringWriter();
var hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "15%");
gvdetails.HeaderRow.Style.Add("font-size", "10px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "8px");
var sr = new StringReader(sw.ToString());
var pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
var htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
示例6: ExportToPDF
protected void ExportToPDF(DataTable dt)
{
GridView gvExport = new GridView();
gvExport.AllowPaging = false;
gvExport.DataSource = dt;
gvExport.DataBind();
gvExport.HeaderRow.Style.Add("font-size", "10px");
gvExport.HeaderRow.Style.Add("font-weight", "bold");
gvExport.Style.Add("text-decoration", "none");
gvExport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvExport.Style.Add("font-size", "8px");
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=LinkSpace.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvExport.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
示例7: SaveToPdf_Click1
protected void SaveToPdf_Click1(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=YourSchedule.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
GridView1.HeaderRow.Style.Add("width", "0");
GridView1.HeaderRow.Style.Add("font-size", "5px");
GridView1.Style.Add("text-decoration", "none");
GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView1.Style.Add("font-size", "5px");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 17f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
示例8: RenderStream
private static byte[] RenderStream(string htmlText, string pageTitle)
{
byte[] renderedBuffer;
//string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");
//FileStream fs = new FileStream(filePath + "\\pdf-" + "Test.pdf", FileMode.Create);
using (var outputMemoryStream = new MemoryStream())
{
using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
pdfDocument.Open();
using (var htmlViewReader = new StringReader(htmlText))
{
using (var htmlWorker = new HTMLWorker(pdfDocument))
{
htmlWorker.Parse(htmlViewReader);
}
}
}
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
return renderedBuffer;
}
示例9: PDF
private void PDF()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
divPDF.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();
//String basePath = Request.ApplicationPath;
//HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
//// Load HTML file
//Document doc = new Document(Request.ApplicationPath + @"\HtmlPage.html", htmloptions);
//// Save HTML file
//doc.Save(Request.ApplicationPath + @"\Output.pdf");
}
示例10: SendPDFEmail
public void SendPDFEmail(string dt, string Email)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringReader sr = new StringReader(dt.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
using (MemoryStream inputData = new MemoryStream(bytes))
{
using (MemoryStream outputData = new MemoryStream())
{
string PDFFileword = "adt";//you can also generate Dynamic word
PdfReader reader = new PdfReader(inputData);
PdfEncryptor.Encrypt(reader, outputData, true, "123", "adt", PdfWriter.ALLOW_SCREENREADERS);
bytes = outputData.ToArray();
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress(Email));
message.Subject = "Test1";
message.Body = "Content test";
message.Attachments.Add(new Attachment(new MemoryStream(bytes), "iTextSharpPDF.pdf"));
smtp.Port = 80;
smtp.Host = "smtpout.secureserver.net";
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("[email protected]", "123456aA");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
label1.Text = "Mail sent successfully";
}
catch (Exception ex)
{
label1.Text = "err: " + ex.Message;
}
}
}
}
}
}
}
示例11: CreatePDF
public bool CreatePDF(string text, string outPutPath)
{
var returnValue = false;
try
{
StringReader sr = new StringReader(text);
// Document pdfDoc = new Document(PageSize.LETTER, 30, 30, 40, 30);
Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 30);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
//PdfWriter.GetInstance(pdfDoc, new FileStream(@"d:\Temp\Test.pdf", FileMode.Create));
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream(outPutPath, FileMode.Create));
//pdfWriter.PageEvent = new ITextEvents();
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
sr.Close();
returnValue = true;
}
catch { }
return returnValue;
}
示例12: Render
public byte[] Render(string htmlText, string pageTitle)
{
byte[] renderedBuffer;
using (var outputMemoryStream = new MemoryStream())
{
using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
pdfWriter.CloseStream = false;
pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
pdfDocument.Open();
using (var htmlViewReader = new StringReader(htmlText))
{
using (var htmlWorker = new HTMLWorker(pdfDocument))
{
htmlWorker.Parse(htmlViewReader);
}
}
}
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
return renderedBuffer;
}
示例13: generatePDFButton_Click
protected void generatePDFButton_Click(object sender, EventArgs e)
{
DataTable aDataTable = aPatientManager.GetData(Convert.ToInt64(voterIdTextBox.Text));
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.ShowHeaderWhenEmpty = true;
GridView1.DataSource = aDataTable;
GridView1.DataBind();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=AllHistoryOfAPatient.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter aStringWriter = new StringWriter();
HtmlTextWriter aHtmlTextWriter = new HtmlTextWriter(aStringWriter);
GridView1.RenderControl(aHtmlTextWriter);
StringReader aStringReader = new StringReader(aStringWriter.ToString());
Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker aHtmlWorker = new HTMLWorker(pdfDocument);
PdfWriter.GetInstance(pdfDocument, Response.OutputStream);
pdfDocument.Open();
aHtmlWorker.Parse(aStringReader);
pdfDocument.Close();
Response.Write(pdfDocument);
Response.End();
}
示例14: btnExport_Click
protected void btnExport_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To Export all pages
grid_monthly_attendanceDetailed.AllowPaging = false;
//this.BindGrid();
grid_monthly_attendanceDetailed.RenderBeginTag(hw);
grid_monthly_attendanceDetailed.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in grid_monthly_attendanceDetailed.Rows)
{
row.RenderControl(hw);
}
grid_monthly_attendanceDetailed.FooterRow.RenderControl(hw);
grid_monthly_attendanceDetailed.RenderEndTag(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Report.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
示例15: ASPXToPDF
public void ASPXToPDF(HtmlTable objhtml1, HtmlTable objhtml2)
{
string fileName = "AsignacionFolios.pdf";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Clear();
StringWriter sw1 = new StringWriter();
HtmlTextWriter hw1 = new HtmlTextWriter(sw1);
objhtml1.RenderControl(hw1);
StringWriter sw2 = new StringWriter();
HtmlTextWriter hw2 = new HtmlTextWriter(sw2);
objhtml2.RenderControl(hw2);
StringReader sr1 = new StringReader(sw1.ToString());
StringReader sr2 = new StringReader(sw2.ToString());
Document pdfDoc = new Document(PageSize.A2, 5f, 5f, 5f, 5f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr1);
pdfDoc.NewPage();
htmlparser.Parse(sr2);
pdfDoc.Close();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}