[英]Printing BlockUIContainer to XpsDocument/FixedDocument
[英]WPF FixedDocument Size Not Being Preserved When Printing
我試圖允許用戶在保存或打印文檔之前使用觸摸屏注釋文檔。 本質上,我將代碼分為三個部分進行組裝:一個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的,所以這並不完全令人驚訝,但我認為固定文檔的整點是作為描述的所見即所得的理念在這里 。
有沒有一種方法可以將頁面明確定義為7.5“ x 10”(或8.5“ x 11”的居中內容),以便正確打印? 似乎當我開始調整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>
並在代碼背后:
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();
}
}
這僅是一種臨時解決方法,因此請隨時貢獻!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.