动态配置打印设置

时间:2014-05-08 14:26:29

标签: c# wpf

我的WPF应用程序中有这样的类,这里我想分配打印设置,如页面大小,托盘而不向用户显示对话框。 我可以设置除打印托盘以外的其他值,如果有人可以帮助我,这将是非常有帮助的。

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

public class Print
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Print"/> class.
    /// </summary>
    public Print() { }

    /// <summary>
    /// Prints the document.
    /// </summary>
    /// <param name="outputStream">The output stream.</param>
    public void PrintDocument(MemoryStream outputStream)
    {
        FlowDocument fd = new FlowDocument();

        TextRange tr = new TextRange(fd.ContentStart, fd.ContentEnd);
        tr.Load(outputStream, DataFormats.Rtf);

        PrintDialog printDlg = new PrintDialog();



        fd.PageHeight = printDlg.PrintableAreaHeight;
        fd.PageWidth = printDlg.PrintableAreaWidth;
        fd.PagePadding = new Thickness(25);

        fd.ColumnGap = 0;
        fd.ColumnWidth = (fd.PageWidth -
                               fd.ColumnGap -
                               fd.PagePadding.Left -
                               fd.PagePadding.Right);

        if (printDlg.ShowDialog() == true)
        {              
            IDocumentPaginatorSource idpSource = fd;
            idpSource.DocumentPaginator.PageSize = new Size { Height = 600, Width = 600 };
            printDlg.PrintDocument(idpSource.DocumentPaginator, "Printing Document");
        }
    }

1 个答案:

答案 0 :(得分:1)

请使用以下代码在C#中设置默认页面大小

    PaperSize paperSize = new PaperSize("papersize", 200, 100);//set the paper size 

PaperSize接受参数NewPageSizeName,Width,Height