你好那里有stackflowers,
我已经用这个问题作为例子:Export Asp.Panel to PDF?但我似乎无法让它发挥作用,因为我是一个愚蠢的菜鸟。
我觉得我的代码非常接近我的代码:
protected void PayslipToPDFButton_Click(object sender, EventArgs e)
{
var template = new SAL_PayslipTemplateGEN().Select(ucPayslipTemplateCombo.SelectedPayslipTemplateID);
GeneratePayslip();
int count = 1;
foreach (var pnlItem in panelList)
{
using (var sw = new StringWriter())
{
using (var hw = new HtmlTextWriter(sw))
{
pnlItem.RenderControl(hw);
using (var sr = new StringReader(sw.ToString()))
{
using (var pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f))
{
using (var htmlparser = new HTMLWorker(pdfDoc))
{
using (var myMemoryStream = new MemoryStream())
{
using (var writer = PdfWriter.GetInstance(pdfDoc, myMemoryStream))
{
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(HttpContext.Current.Server.MapPath(string.Format("~/{0}", template.Stylesheet)), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
byte[] content = myMemoryStream.ToArray();
using (FileStream fs = File.Create("C:\\temp\\Test" + count++ + ".pdf"))
{
fs.Write(content, 0, (int)content.Length);
fs.Close();
}
}
}
}
}
}
}
}
}
}
我使用htmlparser.parse(sr),但我需要css,这就是我尝试这种方式的原因。
它仍会创建pdf文件,但现在它们是空的。
非常感谢你的时间!