C# 画布到pdf错误
我有一个应用程序,它的需求就像我需要在弹出窗口中将画布保存为pdf文档一样。 为此,我使用了第三方工具将图像数据转换为pdf 现在我是如何做到这一点的:C# 画布到pdf错误,c#,pdf,canvas,formatexception,C#,Pdf,Canvas,Formatexception,我有一个应用程序,它的需求就像我需要在弹出窗口中将画布保存为pdf文档一样。 为此,我使用了第三方工具将图像数据转换为pdf 现在我是如何做到这一点的: 首先,我创建了将画布数据从UI发送到服务器的web方法 在JSON格式中,我将其转换为MemoryStream 我调用服务器按钮单击事件,将MemoryStream转换为pdf 文件。它正在成功创建 但当我使用以下代码返回它时: HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
// inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
// let the browser know how to open the PDF document
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("{0}; filename=HtmlToPdf.pdf;size={1}", "attachment", pdfBuffer.Length.ToString()));
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
//HttpContext.Current.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
它给了我以下例外:
[FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. ]
System.Convert.FromBase64String(String s) +0
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
System.Web.UI.HiddenFieldPageStatePersister.Load() +147
[ViewStateException: Invalid viewstate.
Client IP: ::1
Port:
Referer: http://[mysite]/Report.aspx
Path: /Report.aspx
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
ViewState: /wEPDwUJNTc2MDY1MzA3D2QWAgIDD2QWBGYPZBYEZg9kFgxmD2QWAgIBDxYCHglpbm5lcmh0bWwFrgk8c2VsZWN0IGNsYXNzPSdzZWxlY3QnIElEPSAnZGltZW5zaW9uU2VsZWN0JyBvbkNoYW5nZT0nRXhwYW5kQWNjb3JkaW9uKHRoaXMsbGJsVGV4dCknPiA8b3B0Z3JvdXAgbGFiZWw9J1RpbWUnIGlkPSdUX1lfRGltZW5zaW9uXzEnPiA8b3B0aW9uIGlkID0nWScgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPlllYXJseTwvb3B0aW9uPiA8b3B0aW9uIGlkID0nTScgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPk1vbnRobHk8L29wdGlvbj4gPG9wdGlvbiBpZCA9J0QnIHZhbHVlID0nMF8yXzFfMV9JTl9UaW1lJz5EYWlseTwvb3B0aW9uPiA8b3B0aW9uIGlkID0nSCcgdmFsdWUgPScwXzJfMV8xX0lOX1RpbWUnPkhvdXJseTwvb3B0aW9uPiA8L29wdGdyb3VwPiA8b3B0Z3JvdXAgbGFiZWw9J0dlb2dyYXBoeScgaWQ9J0dfWV9EaW1lbnNpb25fMic...
请帮助我,我在这里遗漏了什么?这一点例外似乎非常有用: [FormatException:输入不是有效的Base-64字符串,因为它是 包含非基64字符、两个以上填充字符或 填充字符中的非空白字符。] 以这种方式尝试:
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName );
Response.AddHeader( "Content-Length", strFileSize );
Response.ContentType = "application/pdf" ;
添加了这个:Response.AddHeader(“内容长度”,strFileSize);另外两个已经在那里了。但还是没有运气一样的错误!!!!!!!您是否使用任何第三方工具进行此操作?如果不是这样,您将不得不使用第三方工具,尝试使用iText Sharp库免费。像HttpContext.Current.Response.Write(pdfBuffer)一样放置pdfuffer;