C# 打印时未保留WPF Fixed文档大小
我试图允许用户在保存或打印文档之前使用触摸屏对文档进行注释。从本质上讲,我用代码将文档分为三个部分——一个带有第三方应用程序中创建的某些动态文本的“图片”的ImageBrush,一个带有空白表单(透明背景)的ImageBrush,以及一个代表触摸屏输入的笔划集合。我知道这不是创建XPS的首选方法,但是我没有运行时访问动态文本以生成文本块等。该文档在XAML中定义如下:C# 打印时未保留WPF Fixed文档大小,c#,wpf,printing,fixeddocument,C#,Wpf,Printing,Fixeddocument,我试图允许用户在保存或打印文档之前使用触摸屏对文档进行注释。从本质上讲,我用代码将文档分为三个部分——一个带有第三方应用程序中创建的某些动态文本的“图片”的ImageBrush,一个带有空白表单(透明背景)的ImageBrush,以及一个代表触摸屏输入的笔划集合。我知道这不是创建XPS的首选方法,但是我没有运行时访问动态文本以生成文本块等。该文档在XAML中定义如下: <Viewbox x:Name="viewboxDocView" Grid.Row="0"> <F
<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>
它在我的应用程序中显示得很漂亮,保存为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的全部要点是所见即所得的概念
是否有办法明确定义页面为7.5“x10”(或内容居中的8.5“x11”),以便正确打印?似乎当我开始在XAML中调整大小或尝试使用转换时,屏幕显示会出现问题,这对应用程序来说最终更为重要。我仍然想知道如何使我的文件与标准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>
这是一个临时的解决办法,所以请随时提供帮助 您正在打印
Viewbox
或FixedPage
?我正在将FixedPage作为子项创建一个fixedpocument并打印它。更具体地说,我正在将该fixedpocument保存为.xps文件,并尝试从Microsoft xps Viewer(Windows 7)或Microsoft Reader(Windows 8)打印该文件可能会显示一些您正在使用的代码?对于PrintDialog和文档设置,我有很多错误的想法,我愿意看看。此外,如果您想深入了解它,您可以阅读XPS规范。如果在创建后将.xps
更改为.zip
,并查看文档内容,则可以以XML形式查看内容。
<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();
}
}