gpt4 book ai didi

c# - 关闭文档时抛出“文档没有页面”错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:53 24 4
gpt4 key购买 nike

我在输出流上生成、保存和写入 PDF。

在输出流上写入时出现The document has no page错误。

问题是什么?

string contents;
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size","11px");
abs.RenderControl(hw);
string path = Server.MapPath("../Images/a.png");
contents = sw.ToString();
contents = contents.Replace("../Images/a.png", path);
sw.Close();
hw.Close();
StringReader sr = new StringReader(contents);

System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/pdf/") + fileName, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30,5,35,5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);

pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);

cpdfDoc.Close();

Response.Write(cpdfDoc);
Response.Flush();
Response.End();

保存没有问题。我在这一行 cpdfDoc.Close(); 上遇到错误。

最佳答案

使用此代码并设置您的 pdf 位置。和图像路径。它的工作。

string contents = "hi";
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size", "11px");

string path = Server.MapPath("~/Images/a.png");

contents = contents.Replace("~/Images/a.png", path);
sw.Close();
hw.Close();
StringReader sr = new StringReader(contents);

System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/") + DateTime.Now.Ticks, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);

pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);

不要使用这条线

contents = sw.ToString();

关于c# - 关闭文档时抛出“文档没有页面”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com