首页 > 解决方案 > 是否可以在 C# 中双面打印,例如打印身份证或驾驶执照?

问题描述

我有两张图片 front.jpg 和 back.jpg。我想将它们正面和背面打印在同一页上以制作一张“卡片”。我遇到了“双工”属性,但不确定它在这方面如何帮助我。

我尝试使用双工。我正在尝试在 printDocument1_PrintPage 事件中为正面和背面打印两个图像

private void button1_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += printDocument1_PrintPage;
    //here to select the printer attached to user PC
    PrintDialog printDialog1 = new PrintDialog();
    printDialog1.Document = pd;
    DialogResult result = printDialog1.ShowDialog();
    pd.PrinterSettings.Duplex = Duplex.Horizontal;

    if (result == DialogResult.OK)
    {    
        pd.Print();//this will trigger the Print Event handeler PrintPage
    }
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    try
    {
        #region Front
        //Load the image from the file
        System.Drawing.Image img = System.Drawing.Image.FromFile(@"E:\front.jpg");

         //Adjust the size of the image to the page to print the full image without loosing any part of it
         Rectangle m = e.MarginBounds;

         if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
         {
              m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
         }
         else
         {
             m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
         }
         e.Graphics.DrawImage(img, m);
         #endregion

         //Load the image from the file
         System.Drawing.Image img1 = System.Drawing.Image.FromFile(@"E:\back.jpg");

         //Adjust the size of the image to the page to print the full image without loosing any part of it
         Rectangle m1 = e.MarginBounds;

         if ((double)img1.Width / (double)img1.Height > (double)m.Width / (double)m.Height) // image is wider
         {
                m.Height = (int)((double)img1.Height / (double)img1.Width * (double)m.Width);
         }
         else
         {
             m.Width = (int)((double)img1.Width / (double)img1.Height * (double)m.Height);
         }
         e.Graphics.DrawImage(img1, m1);

        }
        catch (Exception)
        {

        }
    }

标签: c#webforms

解决方案


我相信您的问题是您不是在打印一个双面页面,而是在打印两页,一张纸的每一面。双工魔法发生在打印机/驱动程序中,而不是 GDI。

我只看到这里打印了一页。不要尝试一次打印两个图像。

  1. 完成打印第一张图像的工作。
  2. 设置 (PrintPageEventArgs) e.HasMorePages = true;
  3. 从此函数返回,您将再次被调用到第二页。
  4. 现在,打印第二张图片并设置 e.HasMorePages = false;

那应该行得通。祝你好运!


推荐阅读