【发布时间】:2022-10-04 17:48:58
【问题描述】:
我在运行时生成了几张图像,我需要打印它们。 这些是条形码,因此可以固定高度,但事先不知道数量,因此可能需要更多页面。例如,每个 A4 页面最多可以容纳 4 张图像。在图像下方,带有条形码内容的文本框可能会有所帮助,但这不是必需的。
对于打印我使用
PrintDialog printDialog = new PrintDialog();
bool? pdResult = printDialog.ShowDialog();
if (pdResult != null && pdResult.Value)
{
FixedDocument document = CreateFixedDocument();
printDialog.PrintDocument(document.DocumentPaginator, "ID Card Printing");
}
这很容易。但在我需要创建页面之前
private FixedDocument CreateFixedDocument()
{
FixedDocument fixedDocument = new FixedDocument();
fixedDocument.DocumentPaginator.PageSize = new Size(???); <---have not understood how to set A4 here
//for (int i = 0; i < (numBarcodes/4); 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.Width = ???
page.Height = ???
TextBlock tbTitle = new TextBlock();
tbTitle.Text = <----------the barcode content
tbTitle.FontSize = 24;
tbTitle.Foreground = new SolidColorBrush(Colors.White);
tbTitle.FontFamily = new FontFamily("Arial");
FixedPage.SetLeft(tbTitle, ????)
FixedPage.SetTop(tbTitle, ?????)
page.Children.Add((UIElement)tbTitle);
Image image = new Image
{
Height = 30,
Width = 30
};
image.Source = imgbarcode.Source;
FixedPage.SetLeft(b, ???);
FixedPage.SetTop(b, ???); // top margin
page.Children.Add((UIElement)b);
//measure size of the layout
Size sz = new Size(???);
page.Measure(sz);
page.Arrange(new Rect(new Point(), sz));
page.UpdateLayout();
return page;
}
感谢任何帮助,因为我已经打印了太多页面! 谢谢
【问题讨论】:
标签: c# wpf image printing fixeddocument