将Pdf文件保存在特定文件夹中而不是下载

我正在做html到pdf文件。 它立即下载。 我不想立即下载。 我希望转换后将文件保存在我的项目文件夹中。

我的C#代码

string html ="
some contents
"; Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=WelcomeLetter.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); StringReader sr = new StringReader(table); Document ResultPDF = new Document(iTextSharp.text.PageSize.A4, 25, 10, 20, 30); PdfPTable Headtable = new PdfPTable(7); Headtable.TotalWidth = 525f; Headtable.LockedWidth = true; Headtable.HeaderRows = 5; Headtable.FooterRows = 2; Headtable.KeepTogether = true; HTMLWorker htmlparser = new HTMLWorker(ResultPDF); PdfWriter.GetInstance(ResultPDF, Response.OutputStream); ResultPDF.Open(); htmlparser.Parse(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); 

现在不要使用Responce.Write因为您不想下载您的文件。并在结束时关闭您的流。

 stream.Close(); 

我将把每个人的答案合并到一个你应该能够投入使用的答案中。 如果这样做,我会接受Manish Parakhiya的回答,因为那里有最重要的部分。

首先,我假设您使用的是最新版本的iTextSharp。 我认为5.5.5是最新版本。 其次,因为这个原因,我将重新using你的代码以便使用using模式。 如果你坚持使用旧的过时的不受支持的版本,如4.1.6,你需要重新调整。

几乎每个教程都会向您显示您可以直接绑定Response.OutputStream 。 这是100%有效,但我认为这也是一个非常糟糕的主意。 相反,绑定到更通用的MemoryStream 。 这使得调试变得更加容易,并且您的代码将更容易移植和调整。

以下代码包含有关每个更改以及实际执行操作的注释。 最上面的部分是关于从HTML字符串创建PDF。 底部实际上对它做了一些事情,包括将其写入磁盘和/或将其流式传输到浏览器。

 //Will hold our PDF eventually Byte[] bytes; //HTML that we want to parse string html = "
some contents
"; //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();
 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);