我正在使用Visual Studio 2019和C#。当我尝试创建pdf文档时成功创建了该文档,但是当它通过Response方法打开时,它显示错误消息``Acrobat Reader无法打开文件,因为它不支持该文件类型”。
taskkill /im "myProgram.exe" /fi "STATUS eq NOT RESPONDING" /f >nul
if errorlevel 0 (
echo %time% %date% - myProgram is was not running so restarted it>> myProgram_restart_LOG.txt
START "" "C:\Program Files (x86)\myProgram\myProgram.exe"
)
我正在使用以下代码。
enter code here
答案 0 :(得分:0)
您尝试通过编写document
对象来返回生成的PDF:
Response.Write(document);
这是错误的,因为iText 7 Document
只是用于向PdfDocument
(您的对象pdf
)添加内容的立面,而PdfWriter
又不包含PDF而是写入了PDF通过其path+ "\\Appointments" + DateTime.Now.ToString("ddMMyyyy") + ".pdf"
,在您的代码中找到文件,位于
DateTime.Now
因此,您应该在响应中写入的内容就是该文件的内容。
与此同时,BinaryWrite
应该首先在变量中生成该路径,然后在两个地方都使用该变量。
此外,正如Albert D. Kallal所说,应使用MemoryStream
将其写为二进制。
或者,如果文件系统中实际上不需要pdf结果,则可以使用MemoryStream
代替实际的文件系统文件。
您在评论和答案中表示确实对文件系统中的文件完全不感兴趣。因此,您应该使用MemoryStream
。您已经找到了将using (MemoryStream stream = new MemoryStream())
{
using (PdfWriter writer = new PdfWriter(stream))
using (PdfDocument pdf = new PdfDocument(writer))
using (Document document = new Document(pdf, PageSize.A4))
{
Paragraph header = new Paragraph("MyTurns.in")
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(20);
// New line
Paragraph newline = new Paragraph(new Text("\n"));
document.Add(newline);
document.Add(header);
// Add sub-header
Paragraph subheader = new Paragraph("Appointment List Date : " + DateTime.Now.ToString("dd/mm/yyyy"))
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(15);
document.Add(subheader);
}
Response.Buffer = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Appointments" + DateTime.Now.ToString("dd/MM/yyyy") + ".pdf");
Response.BinaryWrite(stream.ToArray());
}
与iText 5一起使用的示例代码。iText 7的相应示例如下所示:
Response.End();
顺便说一句,关于您的电话
EndRequest
您可能应该阅读ASP.NET documentation of it
仅提供此方法是为了与ASP兼容-即与ASP.NET之前的基于COM的Web编程技术兼容。如果您想跳到
CompleteRequest
事件并向客户端发送响应,通常最好改为调用{ "ChatUsersList": [ { "receiverId": "5f08bd3e06dd732eb0968363", "receiverName": "John", "senderId": "5f08bd2406dd732eb096835e", "senderName": "Mike", "message": "i send message", "createdAt": "2020-07-11T05:56:13.965Z", "user": "5f08bd3e06dd732eb0968363" }, { "receiverId": "5f08bd2406dd732eb096835e", "receiverName": "Mike", "senderId": "5f08bd3e06dd732eb0968363", "senderName": "John", "isRead": false, "message": "recieved message", "createdAt": "2020-07-11T05:56:00.157Z", "user": "5f08bd2406dd732eb096835e" }, { "receiverId": "5f08bd3e06dd732eb0968363", "receiverName": "John", "senderId": "5f08bd2406dd732eb0968399", "senderName": "Smith", "message": "i send message there", "createdAt": "2020-07-11T05:50:13.965Z", "user": "5f08bd3e06dd732eb0968363" }, { "receiverId": "5f08bd2406dd732eb096835e", "receiverName": "Merry", "senderId": "5f08bd3e06dd732eb0968363", "senderName": "John", "message": "Merry sent messages", "createdAt": "2020-07-11T05:40:13.965Z", "user": "5f08bd3e06dd732eb0968363" } ] }
。
并重新考虑其用途。