wpf 如何在每页上打印多个图像

f0brbegy  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(120)

我在运行时生成了几张图像,需要打印它们。这些都是条形码,所以高度可以固定,但数量事先不知道,所以可能需要更多的页面。例如,每个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;
}

任何帮助都是感激的,因为我已经打印了太多的页面!谢谢

5lhxktic

5lhxktic1#

您可以使用它:

const double dPageWidth_Cm = 21.7;
const double dPageHeight_Cm = 29;
const double dBarcodeImgWidth_Px = 800;
const double dBarcodeImgHeight_Px = dBarcodeImgWidth_Px/4;
const double dVerticalSpacing_Cm = 5;
const double dVerticalSpacingInitial_Cm = dVerticalSpacing_Cm/ 2;

private FixedPage CreateOneFixedPage()
{
    #region 0 Page
    FixedPage page = new FixedPage();
    page.Width = 96 * dPageWidth_Cm;
    page.Height = 96 * dPageHeight_Cm;
    #endregion

    #region 1 Header
    var tbkTitle = new TextBlock();
    tbkTitle.Text = "Title for Barcodes";
    tbkTitle.HorizontalAlignment = HorizontalAlignment.Center;
    tbkTitle.TextAlignment = TextAlignment.Center;
    tbkTitle.FontSize = 80;
    FixedPage.SetLeft(tbkTitle, 96 * (dPageWidth_Cm / 2) - (MeasureTextBlock(tbkTitle).Width / 2));
    page.Children.Add((UIElement)tbkTitle);
    #endregion

    #region 2 Barcodes
    for (int iii = 0; iii < numBarcodes; iii++)
    {
        var img = new Image { Source = imgbarcode.Source };
        FixedPage.SetLeft(img, 96 * dPageWidth_Cm / 2 - dBarcodeImgWidth_Px / 2);//from the center of the page half the img size
        FixedPage.SetTop(img, 96 * (dVerticalSpacing_Cm * iii + dVerticalSpacingInitial_Cm));
        page.Children.Add((UIElement)img);

        var tbkBarcode = new TextBlock();
        tbkBarcode.Text = txtbarcodecontent.Text + "  " + iii;
        tbkBarcode.FontSize = 40;
        FixedPage.SetLeft(tbkBarcode, 96 * dPageWidth_Cm / 2 - (MeasureTextBlock(tbkBarcode).Width / 2));
        FixedPage.SetTop(tbkBarcode, 96 * (dVerticalSpacing_Cm * iii + dVerticalSpacing_Cm / 2) + dBarcodeImgHeight_Px);
        page.Children.Add((UIElement)tbkBarcode);
    }
    #endregion

    #region 3 Footer
    var tbkFooter = new TextBlock();
    tbkFooter.Text = "Footer for Barcodes";
    tbkFooter.HorizontalAlignment = HorizontalAlignment.Center;
    tbkFooter.TextAlignment = TextAlignment.Center;
    tbkFooter.FontSize = 40;
    FixedPage.SetLeft(tbkFooter, 96 * (dPageWidth_Cm / 2) - (MeasureTextBlock(tbkFooter).Width / 2));
    FixedPage.SetBottom(tbkFooter, 0);
    page.Children.Add(tbkFooter);
    #endregion

    return page;
}

private Size MeasureTextBlock(TextBlock tbk)
{
    var formattedText = new FormattedText(tbk.Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
        new Typeface(tbk.FontFamily, tbk.FontStyle, tbk.FontWeight, tbk.FontStretch),tbk.FontSize,
        Brushes.Black, new NumberSubstitution(), 1);
    return new Size(formattedText.Width, formattedText.Height);
}

我不得不使用MeasureTextBlock函数,因为我无法让正常的stackPanel中心逻辑工作。因此,结果如下:

如果需要更多页面,只需重复该逻辑并创建更多页面

相关问题