传播方向设置在iTextSharp.text.Document创建到打印对话框 [英] Propagate the orientation setup at iTextSharp.text.Document creation to the Print dialog

查看:481
本文介绍了传播方向设置在iTextSharp.text.Document创建到打印对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用iTextSharp的采用建立在横向方向PDF文档的 PageSize.A4.Rotate()设置其每页。该文件被送入,后来保存到数据库中的字节数组(在VARBINARY场)。

I use iTextSharp to create a PDF document in Landscape orientation by employing PageSize.A4.Rotate() to set its PageSize. The document is fed into a Stream and later saved into the database as a byte array (in a VARBINARY field).

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);

我遇到的问题是,当它通过浏览器获取和打开(或保存到磁盘),打印时,打印对话框中的默认的纵向方向打开。

The problem I run into is that when its retrieved and opened via a browser (or saved to disk), when printing, the Print dialog opens in the default Portrait orientation.

因为它不是一件简单的事情传达给所有的用户,他们应该先去页面设置,并设置方向为横向,怎么可能是传播在创建文档中使用一路打印对话框中的方向设置?

Since its not the easiest thing to communicate to all users that they should first go to Page Setup and set the orientation to Landscape, how possible is it to propagate the orientation setup used at document creation all the way to the Print dialog?

推荐答案

您可以尝试设置的 PICKTRAYBYPDFSIZE 属性的 PdfWriter 属性为true。较新版本的Adobe的Acrobat / Reader的会检测到这一点,并自动检查打印对话框上的选择纸张来源由PDF页面大小复选框。不幸的是,这是那些提示,并不是所有的PDF阅读器实现的。

You can try setting the PICKTRAYBYPDFSIZE property of your PdfWriter property to true. Newer versions of Adobe Acrobat/Reader will detect this and automatically check the "Choose paper source by PDF page size" checkbox on the print dialog. Unfortunately this is one of those "hints" that not all PDF readers implement.

        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();
                }
            }
        }

这篇关于传播方向设置在iTextSharp.text.Document创建到打印对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆