Propagate the orientation setup at iTextSharp.text.Document creation to the Print dialog

依然范特西╮ 提交于 2019-12-11 07:35:03

问题


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?


回答1:


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



回答2:


Use this:

var rect = new Rectangle(0, 0, PageSize.A4.Height, PageSize.A4.Width, 0);
var document = new Document(rect, 0, 0, 0, 0);


来源:https://stackoverflow.com/questions/8090806/propagate-the-orientation-setup-at-itextsharp-text-document-creation-to-the-prin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!