我在C#中尝试过以下代码.Html内容有子弹,但事实并非如此 显示在创建的PDF文件中。在给定的例子中,我添加了两种类型的子弹,一种是unorderlist&另一个是订单。 你能帮我解决一下吗?
以下是代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DocToPdfConvertor;
using ClosedXML;
using ClosedXML.Excel;
using System.Configuration;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DocToPdfConvertor.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
// get the HTML code of this view
string htmlToConvert = @"Types of fruits<div><ul><li>Apple</li><li>Mango</li><li>Banana</li></ul><div>Types of Vegetables</div></div><div><ol><li>Onion</li><li>Tomato</li><li>Potato</li></ol></div><div><br></div>";
string filename = "PDFfile" + DateTime.Now.ToString("dd-MM-yyyy(hh-mm-sstt)") + ".pdf";
string filePath = ConfigurationManager.AppSettings["ProfileImages"];
string fullpath = Path.Combine(filePath, filename);
System.IO.File.WriteAllBytes(fullpath, GetPDF(htmlToConvert));
return View();
}
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,25, 25, 25, 25);
FontFactory.RegisterDirectories();
// 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;
}
}
}