2013-04-18 35 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() 
相關問題