打印时不保留WPF FixedDocument大小

时间:2015-01-14 16:29:39

标签: c# wpf printing fixeddocument

我试图允许用户在保存或打印文档之前使用触摸屏注释文档。本质上,我在代码中组装文档有三个部分 - 一个ImageBrush,其中包含在第三方应用程序中创建的一些动态文本的“图片”,一个带有空白表单的ImageBrush(透明背景),以及一个表示来自触摸屏。我知道这不是创建XPS的首选方法,但是我没有动态文本的运行时访问权限来生成它作为TextBlocks等。文档在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 Viewer和Microsoft Reader中都能很好地显示。唯一的问题是,当我尝试从这些应用程序中的任何一个打印时,页面图像很小。这两个图像完整打印并缩小,并且笔划显示为全尺寸,但在大多数打印机上被裁剪,因此只有左上部分可见。

我明确将页面尺寸定义为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的重点是所描述的here所见即所得。

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

1 个答案:

答案 0 :(得分:0)

我仍然想弄清楚如何使我的文件与标准的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();
        }
    }

这是一个临时的解决方法,所以请继续做出贡献!