2013-04-18 38 views
1

我正在向我的页面发送PDF,并且我想在用户尝试保存PDF文档时设置默认名称。使用itextsharp将默认名称设置为PDF文档

我使用iTextSharp的和VB.Net

Using s As MemoryStream = New MemoryStream() 
    Dim Pdf_Writer As PdfWriter = PdfWriter.GetInstance(DocumentPDF, s) 
    DocumentPDF.Open() 

    DocumentPDF.SetMargins(10.0F, 10.0F, 10.0F, 10.0F) 

    DocumentPDF.Add(Table) 

    DocumentPDF.Close() 

    contentX= s.ToArray() 
    HttpContext.Current.Response.Buffer = False 
    HttpContext.Current.Response.Clear() 
    HttpContext.Current.Response.ClearContent() 
    HttpContext.Current.Response.ClearHeaders() 
    HttpContext.Current.Response.ContentType = "Application/pdf" 
    HttpContext.Current.Response.BinaryWrite(contentX) 
    HttpContext.Current.Response.Flush() 
    HttpContext.Current.Response.End() 
End Using 

Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf"""); 

这种方式下载的文件(是的,它设置默认名称),但我只是想证明文件,如果用户想要保存它,好...保存(使用默认名称)

如何设置默认名称到我的PDF文档?

+1

([在asp.net动态PDF指定文件名]的可能重复http://stackoverflow.com/questions/74019/specifying-filename-for-dynamic- PDF-在-ASP网) –

回答

0

我有一个类似的问题,通过处理程序页面(.ashx)提供PDF。无论我在HTTP头文件中设置了什么,从浏览器中保存PDF阅读器总是会在使用此URL时将文件名设置为“getpdf.pdf”。

http://www.thepdfchef.com/handlers/getpdf.ashx?id=5188p

所以我所做的就是在那个末尾,则处理程序路径后添加一个转义字符串查询字符串,像这样:

http://www.thepdfchef.com/handlers/getpdf.ashx/Wellbeing%20And%20Domestic%20Assistance%20From%20John%20Paul?id=5188p

您应该检查无效字符和去掉任何可能导致名称有危险的东西。

0

尝试使用此代码:

Response.ContentType = "application/pdf" 
Response.AppendHeader("Content-Disposition", "inline; filename="filename".pdf") 
Response.TransmitFile("filename") 
Response.End() 
相关问题