0
我有一個非常簡單的WPF應用程序,只有一個按鈕。在WPF中打印每頁固定頁數的FixedDocument
<Button x:Name="btnPrintCard" Grid.Row="2" HorizontalAlignment="Center" Content="Print Card" MinWidth="140" Foreground="White"
Cursor="Hand" Background="#008080" Click="btnPrintCard_Click" />
我試圖在典型A4頁面上打印多個尺寸爲3.370 x 2.125的卡片。 如果安排得當,它應該從左至右依次安排10張牌。酷似,土坯讀者打印命令和每片設置自定義頁面至2×5
我生成和印刷卡利用以下代碼:
private void btnPrintCard_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDialog printDialog = new PrintDialog();
bool? pdResult = printDialog.ShowDialog();
if (pdResult != null && pdResult.Value)
{
FixedDocument document = CreateFixedDocument();
printDialog.PrintDocument(document.DocumentPaginator, "ID Card Printing");
}
MessageBox.Show("Printing done.");
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " :: " + ex.InnerException);
}
}
private FixedDocument CreateFixedDocument()
{
FixedDocument fixedDocument = new FixedDocument();
// fixedDocument.DocumentPaginator.PageSize = new Size(96 * 3.370, 96 *2.125);
fixedDocument.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);
for (int i = 0; i < 10; i++)
{
PageContent page = new PageContent();
FixedPage fixedPage = CreateOneFixedPage();
((IAddChild)page).AddChild(fixedPage);
fixedDocument.Pages.Add(page);
}
return fixedDocument;
}
private FixedPage CreateOneFixedPage()
{
FixedPage page = new FixedPage();
page.Background = Brushes.Red;
page.Width = 96 * 3.370;
page.Height = 96 * 2.125;
TextBlock tbTitle = new TextBlock();
tbTitle.Text = "xxx xxxxx Public School";
tbTitle.FontSize = 24;
tbTitle.Foreground = new SolidColorBrush(Colors.White);
tbTitle.FontFamily = new FontFamily("Arial");
FixedPage.SetLeft(tbTitle, 96 * 0.4); // left margin
FixedPage.SetTop(tbTitle, 96 * 0.04); // top margin
page.Children.Add((UIElement)tbTitle);
Image image = new Image
{
Source = new BitmapImage(new Uri("http://www.ready-range.co.uk/_assets/images/products/BHSRR40R0R.jpg")),
Height = 30,
Width = 30
};
Border b = new Border();
b.BorderThickness = new Thickness(1);
b.BorderBrush = Brushes.Yellow;
b.Child = image;
FixedPage.SetLeft(b, 96 * 0.3);
FixedPage.SetTop(b, 96 * 0.6); // top margin
page.Children.Add((UIElement)b);
//measure size of the layout
Size sz = new Size(96 * 3.370, 96 * 2.125);
page.Measure(sz);
page.Arrange(new Rect(new Point(), sz));
page.UpdateLayout();
return page;
}
它導致成功地打印但每頁每張卡這樣的:
的問題是我想像上面第一張圖像那樣打印,即定製2每張5張。
很多謝謝。