WPF 固定了打印时不保留文档大小的问题

本文关键字:文档 问题 保留 打印 WPF | 更新日期: 2023-09-27 18:37:27

我试图允许用户在保存或打印文档之前使用触摸屏注释文档。 从本质上讲,我正在用三个部分在代码中组装文档 - 一个带有在第三方应用程序中创建的一些动态文本的"图片"的 ImageBrush,一个带有空白表单(透明背景)的 ImageBrush,以及一个表示触摸屏输入的笔触集合。 我知道这不是创建 XPS 的首选方法,但是我没有运行时访问动态文本以将其生成为文本块等。 该文档在 XAML 中定义如下:

<Viewbox x:Name="viewboxDocView" Grid.Row="0">
      <FixedPage x:Name="fixedPage" Width="720" Height="960" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10">
          <InkCanvas x:Name="inkCanvas" Width="720" Height="960" HorizontalAlignment="Center" VerticalAlignment="Center" ></InkCanvas>
      </FixedPage>
</Viewbox>

在代码隐藏中,我将 FixedPage 背景设置为显示动态文本,将 InkCanvas 背景设置为显示透明表单。

inkCanvas.Background = myImageBrush;
fixedPage.Background = myOtherImageBrush;

它在我的应用程序中显示得很漂亮,保存为XPS文件,并在XPS查看器和Microsoft阅读器中都显示得很漂亮。 唯一的问题是,当我尝试从这些应用程序中的任何一个打印时,页面图像很小。 这两个图像将整体打印并缩小,笔触显示为全尺寸,但在大多数打印机上被裁剪,以便仅显示左上角部分。

我明确将页面尺寸定义为 720 x 960,因为 960 * 1/96" = 10 英寸和 720 * 1/96" = 7.5 英寸,强制在 8.5x11" 页面上至少留出 0.5 英寸的边距。 当我打印时,图像正好是2.4" x 3.2",这意味着打印机分辨率为960/3.2 = 300 dpi,而不是预期的96 dpi。 我知道打印机通常使用 300 dpi,所以这并不完全令人惊讶,但我认为 FixedDocument 的全部意义在于这里描述的所见即所得的概念。

有没有办法将页面明确定义为 7.5" x 10"(或 8.5" x 11",内容居中),以便正确打印? 似乎当我开始在 XAML 中调整大小或尝试使用转换时,它会搞砸屏幕上的显示,这最终对应用程序更为重要。

WPF 固定了打印时不保留文档大小的问题

我仍然想弄清楚如何使我的文件与标准Microsoft程序兼容,但现在我已经创建了一个简单的单窗口 WPF .xps 文件查看器应用程序,该应用程序接受文件路径作为命令行参数并正确打印这些内容。 显然,我本可以使用现有程序中的另一个窗口来完成此操作,除了我似乎无法解决Windows打印对话框的线程安全问题。 在 XAML 中:

<Window x:Class="Viewer.ViewerWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Document Viewer">
    <Grid>
        <DocumentViewer x:Name="Viewer" />
    </Grid>
</Window>

在代码隐藏中:

public partial class ViewerWindow : Window
    {
        XpsDocument doc;
        string fileName;
        public ViewerWindow()
        {
            InitializeComponent();
            // get file name from command line
            string[] args = System.Environment.GetCommandLineArgs();
            fileName = args[1];
            // size window
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            this.Width = ((System.Windows.SystemParameters.PrimaryScreenHeight * 8.5) / 11);
            // Open File and Create XpsDocument
            if (!File.Exists(fileName) || fileName == null || fileName == "") 
            {
                System.Windows.MessageBox.Show("File Not Found!");
                this.Close();
            }
            else if (!fileName.EndsWith(".xps") && !fileName.EndsWith(".oxps"))
            {
                System.Windows.MessageBox.Show("File not in XPS format");
                this.Close();
            }
            else 
            {
                try
                {
                    doc = new XpsDocument(fileName, System.IO.FileAccess.Read);
                }
                catch (UnauthorizedAccessException)
                {
                    System.Windows.MessageBox.Show("Unable to open file - check user permission settings and make sure that the file is not open in another program.");
                    this.Close();
                }
            }
            // Display XpsDocument in Viewer
            Viewer.Document = doc.GetFixedDocumentSequence();
        }
    }

这只是一个临时解决方法,所以请随时贡献!