C# 值不能为null。参数名称:PDFsharp中的elementId
我正在使用C#和PDFSharp将功能导出为PDF。我得到这个错误:C# 值不能为null。参数名称:PDFsharp中的elementId,c#,.net,pdfsharp,C#,.net,Pdfsharp,我正在使用C#和PDFSharp将功能导出为PDF。我得到这个错误: Value cannot be null. Parameter name: elementId 错误在这一行: PdfDocument document=PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(),PdfSharp.PageSize.A4,30) 以下是整个方法: public bool ExportPdf(string htmlcontenttbl) {
Value cannot be null.
Parameter name: elementId
错误在这一行:
PdfDocument document=PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(),PdfSharp.PageSize.A4,30)代码>
以下是整个方法:
public bool ExportPdf(string htmlcontenttbl)
{
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=Myfile.pdf");
//Response.AddHeader("Content-Disposition", "inline;filename=file.pdf");
//Response.AppendHeader("Content-Disposition", "attachment; filename=Myfile.pdf");
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);
var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.PageSize = PageSize.A4;
config.MarginBottom = 30;
config.MarginTop = 30;
//PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl, config);
byte[] bytes = null;
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, true);
bytes = stream.ToArray();
}
//var path1 = Server.MapPath("~/Images/" + DateTime.Now.TimeOfDay.Ticks + "result.pdf");
//System.IO.File.WriteAllBytes(path1, bytes);
//Response.TransmitFile(path1, 0, bytes.Length);
//Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return true;
}
如果您有一个空白的锚定标记,则会发生该异常
它很可能最终是由以下因素产生的
你应该删除空白锚标签。< /P>我们能看到堆栈跟踪吗?堆栈跟踪会很有用,没有名为elementId的参数。
ScrollToElement
是该项目中引发此异常的唯一位置:但不知道它来自何处,以及为什么参数为null
或空。可能还有@TimSchmelter?标题错误-这不是PDFsharp的问题,这是PDFGGenerator的问题。什么是“空白锚定标记”?我有一些包含图标、nbsp和各种其他元素,但渲染仍然失败。
是锚定标记@AlenSiljak。谢谢,我知道这一点,但是,正如我前面所问的,什么是破坏渲染器的“空白锚定标记”?我没有空白的锚定标记,渲染器仍然抛出此异常。查看代码(),我将猜测其中一个没有id
(我查看此内容已经快两年了)。如果将id
s添加到中,您是否有任何
标记没有href
,或者href
只是#
@alensijak?