C# 将Pdf文件保存在特定文件夹中,而不是下载
我正在做html到pdf文件。即时下载。我不想立即下载。转换后,我想将文件保存在我的项目文件夹中 我的C代码C# 将Pdf文件保存在特定文件夹中,而不是下载,c#,itextsharp,C#,Itextsharp,我正在做html到pdf文件。即时下载。我不想立即下载。转换后,我想将文件保存在我的项目文件夹中 我的C代码 string html=“一些内容”; Response.ContentType=“application/pdf”; AddHeader(“内容处置”、“附件;文件名=WelcomeLetter.pdf”); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw=新的StringWriter(
string html=“一些内容”;
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=WelcomeLetter.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
StringReader sr=新的StringReader(表);
Document ResultPDF=新文档(iTextSharp.text.PageSize.A4、25、10、20、30);
PdfPTable Headtable=新的PdfPTable(7);
床头台。总宽度=525f;
Headtable.LockedWidth=true;
Headtable.HeaderRows=5;
Headtable.FooterRows=2;
Headtable.KeepTogether=true;
HTMLWorker htmlparser=新的HTMLWorker(ResultPDF);
GetInstance(ResultPDF,Response.OutputStream);
ResultPDF.Open();
解析(sr);
ResultPDF.Close();
Response.Write(ResultPDF);
Response.End();
要在项目文件夹中本地保存pdf文件,可以像这样使用FileStream
类
FileStream stream = new FileStream(filePath, FileMode.Create);//Here filePath is path of your project folder.
现在,在创建PdfWriter
对象的实例时,使用此流,而不是使用Response.OutputStream
PdfWriter.GetInstance(ResultPDF, stream);
现在不要使用response。写,因为你不想下载你的文件。最后关闭你的流
stream.Close();
我将把每个人的答案合并成一个你应该能够访问并使用的答案。如果这能奏效,我会接受Manish Parakhiya的答案,因为这是最重要的部分
首先,我假设您使用的是最新版本的iTextSharp。我认为5.5.5是最新的版本。第二,由于这个原因,我将稍微重新构造代码,以便使用using
模式。如果您停留在旧的过时的不受支持的版本上,如4.1.6,则需要重新调整
几乎所有教程都向您展示了可以直接绑定响应.OutputStream
。这是100%有效的,但我认为这也是一个非常糟糕的想法。相反,请绑定到更通用的MemoryStream
。这使得调试更加容易,您的代码将更容易移植和调整
下面的代码包括关于每个更改的注释以及实际操作的内容。顶部的部分是关于从HTML字符串创建PDF的。底部实际上对其进行了一些操作,包括将其写入磁盘和/或将其流式传输到浏览器
//Will hold our PDF eventually
Byte[] bytes;
//HTML that we want to parse
string html = "<table><tr><td>some contents</td></tr></table>";
//Create a MemoryStream to write our PDF to
using (var ms = new MemoryStream()) {
//Create our document abstraction
using (var ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30)) {
//Bind a writer to our Document abstraction and our stream
using (var writer = PdfWriter.GetInstance(ResultPDF, ms)) {
//Open the PDF for writing
ResultPDF.Open();
//Parse our HTML using the old, obsolete, not support parser
using (var sw = new StringWriter()) {
using (var hw = new HtmlTextWriter(sw)) {
using (var sr = new StringReader(html)) {
using (var htmlparser = new HTMLWorker(ResultPDF)) {
htmlparser.Parse(sr);
}
}
}
}
//Close the PDF
ResultPDF.Close();
}
}
//Grab the raw bytes of the PDF
bytes = ms.ToArray();
}
//At this point, the bytes variable holds a valid PDF file.
//You can write it disk:
System.IO.File.WriteAllBytes("your file path here", bytes);
//You can also send it to a browser:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf");
Response.BinaryWrite(bytes);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Never do the next line, it doesn't do what you think it does and actually produces corrupt PDFs
//Response.Write(ResultPDF); //BAD!!!!!!
Response.End();
//最终将保存我们的PDF
字节[]字节;
//我们想要解析的HTML
string html=“某些内容”;
//创建一个MemoryStream,将我们的PDF文件写入其中
使用(var ms=new MemoryStream()){
//创建文档抽象
使用(var ResultPDF=新文档(iTextSharp.text.PageSize.A4,25,10,20,30)){
//将作者绑定到文档抽象和流
使用(var writer=PdfWriter.GetInstance(ResultPDF,ms)){
//打开PDF进行写作
ResultPDF.Open();
//使用旧的、过时的、不支持的语法分析器解析我们的HTML
使用(var sw=new StringWriter()){
使用(var hw=新的HtmlTextWriter(sw)){
使用(var sr=newstringreader(html)){
使用(var htmlparser=newhtmlworker(ResultPDF)){
解析(sr);
}
}
}
}
//关闭PDF
ResultPDF.Close();
}
}
//抓取PDF的原始字节
字节=ms.ToArray();
}
//此时,bytes变量保存一个有效的PDF文件。
//您可以将其写入磁盘:
System.IO.File.writealBytes(“此处的文件路径”,字节);
//您还可以将其发送到浏览器:
Response.ContentType=“application/pdf”;
AddHeader(“内容处置”、“附件;文件名=WelcomeLetter.pdf”);
响应。二进制写入(字节);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//永远不要做下一行,它不会做你认为它做的事情,实际上会产生腐败的PDF
//Response.Write(ResultPDF)//糟糕!!!!!!
Response.End();
下载PDF文件或任何其他媒体文件由浏览器自行决定。因此,您必须在浏览器的内容设置中更改浏览器处理PDF的方式。你不能在代码中这样做。
string tempDirectory = Session.SessionID.ToString();
string location = Path.Combine(Server.MapPath(
WebConfigurationManager.AppSettings["PathSet"].ToString()), tempDirectory);
if (!Directory.Exists(location))
{
Directory.CreateDirectory(location);
}
string fileName="abc.pdf";
filePath = Path.Combine(location, fileName);
//Will hold our PDF eventually
Byte[] bytes;
//HTML that we want to parse
string html = "<table><tr><td>some contents</td></tr></table>";
//Create a MemoryStream to write our PDF to
using (var ms = new MemoryStream()) {
//Create our document abstraction
using (var ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30)) {
//Bind a writer to our Document abstraction and our stream
using (var writer = PdfWriter.GetInstance(ResultPDF, ms)) {
//Open the PDF for writing
ResultPDF.Open();
//Parse our HTML using the old, obsolete, not support parser
using (var sw = new StringWriter()) {
using (var hw = new HtmlTextWriter(sw)) {
using (var sr = new StringReader(html)) {
using (var htmlparser = new HTMLWorker(ResultPDF)) {
htmlparser.Parse(sr);
}
}
}
}
//Close the PDF
ResultPDF.Close();
}
}
//Grab the raw bytes of the PDF
bytes = ms.ToArray();
}
//At this point, the bytes variable holds a valid PDF file.
//You can write it disk:
System.IO.File.WriteAllBytes("your file path here", bytes);
//You can also send it to a browser:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf");
Response.BinaryWrite(bytes);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Never do the next line, it doesn't do what you think it does and actually produces corrupt PDFs
//Response.Write(ResultPDF); //BAD!!!!!!
Response.End();