【问题标题】:Print several BitmapSources with different page orientations打印多个具有不同页面方向的 BitmapSource
【发布时间】:2014-11-21 12:59:12
【问题描述】:

在 C# 中,我有一个位于同一文档中的 BitmapSource 集合。这些 BitmapSource 各自具有独立的页面方向,我想在同一个打印作业中打印它们,同时保持为每个页面设置的页面方向。

今天,我无法做到这一点,因为我使用的是 PrintDialog 类,并且每个 BitmapSource 都被添加到 FixedPage 中的 FixedDocument 中 - 使用这种方法,我只能将页面方向设置为整个文档(每张打印票一个方向)。在一个打印作业中(在 PrintDialog 类中,仅使用一张打印票)打印多个具有不同页面方向的 BitmapSource 应该怎么做?

【问题讨论】:

    标签: c# printing landscape-portrait printdialog bitmapsource


    【解决方案1】:

    我还不能离开 cmets,因为我只有 11 个代表,但我会尽量提供我所掌握的信息,这可能会有所帮助。

    有几件事:这可能取决于您如何创建FixedDocument 和生成FixedPages。如果您首先生成FixedPage,然后将其添加到FixedDocument,我可以想到您可以尝试的一件事。

    此外,这取决于您打印页面的方式、打印每一页的方式,还是您想调用 print 一次,然后打印整个文档。

    无论如何,一种方法是在将固定页面添加到文档之前调整它们的大小。如果您使用打印对话框,请获取打印票属性并将您的控件或您拥有的控件缩放到它们需要的大小。

    这对我有用:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var fp1 = new FixedPage() {Height=1056, Width =816};
        var g = new Grid();
        g.Children.Add(new Rectangle { Height = 1056, Width = 816, Fill = Brushes.Orange });
        fp1.Children.Add(g);
    
        var fp2 = new FixedPage() { Height = 816, Width = 1056 };
        var g1 = new Grid();
        g1.Children.Add(new Rectangle { Height = 816, Width = 1056, Fill = Brushes.Pink });
        fp2.Children.Add(g1);
    
        var fd = new FixedDocument();
    
        fd.Pages.Add(new PageContent{Child = fp1 });
        fd.Pages.Add(new PageContent{Child = fp2 });
    
        var d = new XpsDocument(@"C:\users\me\desktop\tmp.xps",
                                FileAccess.ReadWrite);
    
        var doc =
            XpsDocument.CreateXpsDocumentWriter(d);
    
        doc.Write(fd);
        d.Close();
    }
    

    tmp.xps 在 Windows 资源管理器预览中看起来像:

    假设您不想将其打印到文件中,或者您也想将其发送到打印机。

    var hardCopy = fd.DocumentPaginator;
    var pd = new PrintDialog();
    if(pd.ShowDialog() == true)
    {
        pd.PrintQueue.AddJob("My Document",
                             @"C:\users\me\desktop\tmp.xps",
                             false);
    }
    

    注意:只有使用 XPS 设备打印时,打印输出才会有正确的方向。见PrintQueue.IsXpsDevice

    如果这不符合您的需要,您必须编写自己的DocumentPaginator,以便您处理每一页的打印。您在这里的一个选择是转换您的视觉效果/页面,因为它们在DocumentPaginator.GetPage(int index) 中被调用。如果您更改任何 UI 属性,请记住为每个页面调用 Measure、Arrange 和 UpdateLayout。

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      相关资源
      最近更新 更多