如何允许用户在特定位置保存PDF

时间:2015-02-14 08:50:49

标签: asp.net itextsharp

我有一个PDF文件保存到下载文件夹中,但我想允许用户选择位置。这是我的代码。 我正在使用iTextSharp进行PDF创建。 它总是将PDF下载到下载文件夹,请帮助。

Document document = new Document(PageSize.A4, 0, 0, 15, 12);
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
pdfPage ab = new pdfPage();

PdfPTable table = new PdfPTable(4);
table.SetWidths(new int[] { 3, 1, 2, 3 });
table.SpacingBefore = 5f;
var blackListTextFont = FontFactory.GetFont("Arial", 8f, Color.BLACK);

phrase = new Phrase();
phrase.Add(new Chunk("", blackListTextFont));
cell = new PdfPCell(new Phrase(phrase));
cell.Padding = 5f;
cell.BackgroundColor = new Color(85, 142, 213);
cell.Colspan = 4;
table.AddCell(cell);

phrase = new Phrase();
phrase.Add(new Chunk("I", blackListTextFont));
cell = new PdfPCell(new Phrase(phrase));
cell.Padding = 5f;
cell.BackgroundColor = new Color(85, 142, 213);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);

phrase = new Phrase();
phrase.Add(new Chunk("D", blackListTextFont));
cell = new PdfPCell(new Phrase(phrase));
cell.Padding = 5f;
cell.BackgroundColor = new Color(85, 142, 213);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);

phrase = new Phrase();
phrase.Add(new Chunk("O", blackListTextFont));
cell = new PdfPCell(new Phrase(phrase));
cell.Padding = 5f;
cell.BackgroundColor = new Color(85, 142, 213);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);


phrase = new Phrase();
phrase.Add(new Chunk("S", blackListTextFont));
cell = new PdfPCell(new Phrase(phrase));
cell.Padding = 5f;
cell.BackgroundColor = new Color(85, 142, 213);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell);
table.CompleteRow();

document.Add(table);

document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf");
Response.End();
Connection.Close();

1 个答案:

答案 0 :(得分:1)

由浏览器决定它将保存文件的默认位置,无法在服务器端更改它。

See this article,了解有关如何根据每个浏览器更改默认下载位置的信息。