You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF打印多页XPS文档内容重复问题求助

多页打印时QR码与内容重复,复用同一Grid导致覆盖问题

我的应用根据用户输入生成唯一编号并转换为QR码,用于打印输出。ListBox中存储打印条目,每页最多显示6条,当收集满6条或处理到最后一条时,调用FillPrintGridTemplate方法填充模板Grid,再将其加入打印列表。

单页(条目数≤6)打印正常,QR码显示正确;但条目数超过6时,生成的XPS文档页数正确,但每页的QR码和信息完全重复。排查发现第二次调用FillPrintGridTemplate时,filledTemplates列表中的第一个元素会被覆盖,推测是因为始终复用同一个Grid实例导致的。

按钮点击事件代码

private void Button_Click(object sender, RoutedEventArgs e)
{
     listBox10.Visibility = Visibility.Collapsed;
     PrintGrid.Visibility = Visibility.Visible;

     GenerateUniqueNumber_v2.PrintTemplate printTemplate = new GenerateUniqueNumber_v2.PrintTemplate();

     // 创建临时ListBox存放待打印条目
     ListBox tempListBox = new ListBox();

     // 存放填充好的模板列表
     List<FrameworkElement> filledTemplates = new List<FrameworkElement>();

     // 遍历ListBox中的条目
     for (int i = 0; i < listBox10.Items.Count; i++)
     {
         // 将当前条目加入临时ListBox
         tempListBox.Items.Add(listBox10.Items[i]);

         // 检查是否收集满6条或已到最后一条
         if ((i + 1) % 6 == 0 || i == listBox10.Items.Count - 1)         
         {
             // 调用打印方法处理当前批次条目
             var filledTemplate = printTemplate.FillPrintGridTemplate(tempListBox);                    
             filledTemplates.Add(filledTemplate); // 将填充好的模板加入列表
             filledTemplate = null;
             // 清空临时ListBox准备下一批次
             tempListBox.Items.Clear();
             tempListBox = new ListBox();
         }
     }

     Print_WPF_Preview(filledTemplates);
}

FillPrintGridTemplate方法代码

public Grid FillPrintGridTemplate(ListBox listBox)
{
    Grid virtualGrid = new Grid();

    Label[] wnrLabels = { lbl_WNR1, lbl_WNR2, lbl_WNR3, lbl_WNR4, lbl_WNR5, lbl_WNR6 };
    Label[] UniqueNumberLabels = { lbl_UniqueNumber1, lbl_UniqueNumber2, lbl_UniqueNumber3, lbl_UniqueNumber4, lbl_UniqueNumber5, lbl_UniqueNumber6 };
    Label[] MATNRLabels = { lbl_MATNR1, lbl_MATNR2, lbl_MATNR3, lbl_MATNR4, lbl_MATNR5, lbl_MATNR6 };
    Image[] QRCodeImage = { qrCodeImage1, qrCodeImage2, qrCodeImage3, qrCodeImage4, qrCodeImage5, qrCodeImage6 };

    ResetElements(wnrLabels, UniqueNumberLabels, MATNRLabels, QRCodeImage);

    // 遍历ListView中的每个条目
    int i = 0;

    foreach (var obj in listBox.Items)
    {
        // 将条目转换为UniqueNumberItem类型
        UniqueNumberItem item = obj as UniqueNumberItem;

        if (item != null)
        {
            // 访问item的属性填充对应控件
            wnrLabels[i].Content = item.WNR;
            UniqueNumberLabels[i].Content = item.UniqueNumber;
            MATNRLabels[i].Content = item.Matnr;

            // 为唯一编号生成QR码
            BitmapImage qrCodeImage = GenerateQRCode(item.UniqueNumber);
            // 设置Image控件的Source为QR码
            QRCodeImage[i].Source = qrCodeImage;

            i++; // 移动到下一个索引处理下一条目
        }
    }

    return MainGrid;
}

打印预览代码

public static void Print_WPF_Preview(List<FrameworkElement> wpf_Elements)
{
    string sPrintFileName = "print_preview.xps";

    if (File.Exists(sPrintFileName))
    {
        File.Delete(sPrintFileName);
    }

    // 创建XPS文档
    using (XpsDocument doc = new XpsDocument(sPrintFileName, FileAccess.ReadWrite))
    {
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
        SerializerWriterCollator output_Document = writer.CreateVisualsCollator();
        output_Document.BeginBatchWrite();

        // 遍历每个FrameworkElement并写入文档
        foreach (var element in wpf_Elements)
        {
            output_Document.Write(element);
            // 为每个元素添加新页面
            output_Document.Write(new PageContent());
        }

        output_Document.EndBatchWrite();

        // 打开文档预览
        FixedDocumentSequence preview = doc.GetFixedDocumentSequence();
        GenerateUniqueNumber_v2.PrintWindow printWindow = new GenerateUniqueNumber_v2.PrintWindow(preview);
        printWindow.Show();
    }
}

疑问

  1. 如何创建包含子元素的Grid副本,避免每次复用同一Grid导致内容覆盖?或者有没有更优的解决方案?
  2. 次要问题:打印时随机出现空白页。

内容的提问来源于stack exchange,提问作者user25434284

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 17:53:14