简体   繁体   English

无法将FixedDocument打印到自定义尺寸

[英]Can't print FixedDocument to custom size

I have a ready FixedDocument to be printed with the below pagesize for the user to choose accordingly: 我已经准备好以下面的页面大小打印的FixedDocument,供用户相应选择:

if (Globals.LayoutSettings.paperSize.ToUpper() == "LETTER")
                doc.DocumentPaginator.PageSize = new System.Windows.Size(8.5 * 96, 11 * 96);
            else if (Globals.LayoutSettings.paperSize.ToUpper() == "A4")
                doc.DocumentPaginator.PageSize = new System.Windows.Size(8.3 * 96, 11.7 * 96);
            else
                doc.DocumentPaginator.PageSize = new System.Windows.Size(8.5 * 96, 11 * 96);

But every time when I print the FixedDocument out via PDFCreator, it always stays as A4 size. 但是每次我通过PDFCreator打印出FixedDocument时,它始终保持为A4大小。

private bool printDocument(FixedDocument doc)
    {
        bool printed = false;
        try
        {
            System.Windows.Controls.PrintDialog pd = new System.Windows.Controls.PrintDialog();

            //pd.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "TempLabel_" + DateTime.Now.Ticks.ToString());
            pd.PrintDocument(doc.DocumentPaginator, "TempLabel_" + DateTime.Now.Ticks.ToString());

            printed = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in printing document: " + ex.ToString(), "Error in printing");
        }
        return printed;
    }

What can I do to fix this? 我该怎么做才能解决此问题? Appreciate the help. 感谢帮助。

Calling doc.DocumentPaginator gets the most "up-to-date" paginator. 调用doc.DocumentPaginator可获得最“最新”的分页器。 Pagination happens when that call is made, and the size of the page depends on the pages inside the document. 分页发生在进行该调用时,并且页面的大小取决于文档中的页面。

I haven't tried to reproduce the issue, but I have two things that you can try: 我没有尝试重现该问题,但是您可以尝试两件事:

Change the size of each FixedPage in the FixedDocument : 更改FixedDocument中每个FixedPage的大小:

var sizeOfPage = GetPageSizeToPrint(Globals.LayoutSettings.paperSize.ToUpper());
foreach(var page in doc.Pages)
{
    page.Child.Height = sizeOfPage.Height;
    page.Child.Width = sizeOfPage.Width;
}
pd.PrintDocument(doc.DocumentPaginator, "TempLabel_" + DateTime.Now.Ticks.ToString());

another option is to try to change the PrintDialog 's PrintTicket : 另一个选择是尝试更改PrintDialogPrintTicket

var sizeOfPage = GetPageSizeToPrint(Globals.LayoutSettings.paperSize.ToUpper());
pd.PrintTicket.PageMediaSize = new PageMediaSize(sizeOfPage.Width, sizeOfPage.Height);
pd.PrintDocument(doc.DocumentPaginator, "TempLabel_" + DateTime.Now.Ticks.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM