我使用 iTextSharp 创建横向 PDF 文档,方法是使用 PageSize.A4.Rotate() 设置其 PageSize。文档被送入 Stream,随后作为字节数组(在 VARBINARY 字段中)保存到数据库中。
Stream stream = new MemoryStream();
iTextSharp document = new Document();
document.SetPageSize(PageSize.A4.Rotate());
var writer = PdfWriter.GetInstance(document, stream)
document.Open()
// Write to the document
document.Close();
byte[] file = stream.ToArray();
/* In the actual environment the byte array is stored in the database, to be retrievable later */
// WHERE context: HttpContext in a class that implements IHttpHandler
context.Response.AppendHeader("Content-Disposition", "attachment;filename=Test.pdf");
context.Response.AppendHeader("Content-Length", file.Length.ToString());
context.Response.ContentType = MediaTypeNames.Application.Pdf;
context.Response.BinaryWrite(file);
我遇到的问题是,当它通过浏览器检索和打开(或保存到磁盘)时,打印时,“打印”对话框以默认的纵向方向打开。
由于向所有用户传达他们应该首先转到页面设置并将方向设置为横向并不是最简单的事情,因此如何将文档创建时使用的方向设置一直传播到打印对话框?
请您参考如下方法:
您可以尝试将 PdfWriter 属性的 PICKTRAYBYPDFSIZE 属性设置为 true。较新版本的 Adobe Acrobat/Reader 会检测到这一点,并自动选中打印对话框中的“按 PDF 页面大小选择纸张来源”复选框。不幸的是,这是并非所有 PDF 阅读器都实现的“提示”之一。
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Landscape.pdf");
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(PageSize.LETTER.Rotate()))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
{
writer.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE);
doc.Open();
doc.Add(new Paragraph("test"));
doc.Close();
}
}
}
