ntrghf 2024-12-13 18:05 采纳率: 0%
浏览 82
已结题

C#编程中使用printDocument类实现文字排版打印问题

#C#编程中使用printDocument类实现文字排版打印问题
排版打印功能基本实现,目前的问题是如果文字中插入空格,需要将空格的占用高度可以进行调节,其他文字字符不变

 private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
 {
     string text_new = this.textBox5.Text;
     System.Drawing.Font font = new System.Drawing.Font(this.comboBox2.Text, (float)((float)this.numericUpDown33.Value), GraphicsUnit.Millimeter);
     System.Drawing.Font font1 = new System.Drawing.Font(this.comboBox2.Text, (float)((float)this.numericUpDown33.Value * (float)this.numericUpDown22.Value), GraphicsUnit.Millimeter);
     SolidBrush solidBrush = new SolidBrush(Color.Black);
     StringFormat stringFormat = new StringFormat()
     {
         FormatFlags = StringFormatFlags.DirectionVertical,
         LineAlignment = StringAlignment.Center,
         Alignment = StringAlignment.Center
     };
     // 定义字体样式
     FontStyle fontStyle = isBold ? FontStyle.Bold : FontStyle.Regular;
     
     // 定义一个方法来绘制图像
     void DrawImage(Graphics graphics, string iconPath, int x, int y, int width, int height)
     {
         using (Bitmap bitmap = new Bitmap(iconPath))
         {
             graphics.DrawImage(bitmap, x, y, width, height);
         }
     }
     

     int textLength = this.textBox5.TextLength;
     int num = 0;
     int num1 = 0;
     int num2 = 0;
     int num3 = 0; // 新增变量,用于记录第三列字符的位置
     int sectionStart = 0; // 记录每个段落的起始位置
     List<int> starCounts = new List<int>(); // 用于记录每个段落的'*'数量 
     // 第一段解析逻辑
     for (int i = 0; i < text_new.Length; i++)
     {
         if (num == 0 && text_new[i].ToString() == "/")
         {
             num = 1;
             sectionStart = i; // 记录段落起始位置
             num2 = i;
         }
         else if (num == 1 && text_new[i].ToString() == "*")
         {
             num = 2;
             num1 = i;
         }
         else if (num == 2 && text_new[i].ToString() == "*")
         {
             num = 3;
             num3 = i;
         }
         else if ((num == 3 || num == 2) && text_new[i].ToString() == "/")
         {
             num = 0;
             num2 = num1 - num2 - 1;
             num1 = num3 > 0 ? num3 - num1 - 1 : i - num1 - 1;
             int minLength = num3 > 0 ? Math.Min(Math.Min(num2, num1), i - num3 - 1) : Math.Min(num2, num1);
             textLength = num3 > 0 ? textLength - 4 - minLength : textLength - 3 - minLength;
             num3 = 0; // 重置num3以便下一个段落使用 
             // 统计段落内'*'的数量
             int starCount = 0;
             for (int j = sectionStart + 1; j < i; j++)
             {
                 if (text_new[j] == '*')
                 {
                     starCount++;
                 }
             }
             starCounts.Add(starCount); // 记录'*'数量
         }
     }
     float value = (float)((float)this.numericUpDown36.Value);//横偏
     float single = (float)((float)this.numericUpDown37.Value);//竖偏
     // 翻转
     e.Graphics.RotateTransform(180f);
     e.Graphics.TranslateTransform(-4f * (printwidthx - (float)((float)this.numericUpDown3.Value)), 0); // 负数向右移动,正数向左移动 
     value = (float)((float)this.numericUpDown36.Value) - (float)((float)this.numericUpDown3.Value);
     single = (float)((float)this.numericUpDown37.Value) - (float)((float)this.numericUpDown2.Value);
     if (this.checkBox6.Checked && this.insertbmp1 != null)
     {
         PictureBox pictureBox2 = new PictureBox()
         {
             Image = Image.FromFile(this.insertbmp1)
         };
         e.Graphics.DrawImage(pictureBox2.Image, 4f * ((float)((float)this.numericUpDown15.Value) - (float)((float)this.numericUpDown3.Value)),
                              4f * ((float)((float)this.numericUpDown14.Value) - (float)((float)this.numericUpDown2.Value)),
                              4f * (float)((float)this.numericUpDown13.Value),
                              4f * (float)((float)this.numericUpDown12.Value));
     }
     if (this.checkBox7.Checked && this.insertbmp2 != null)
     {
         PictureBox pictureBox3 = new PictureBox()
         {
             Image = Image.FromFile(this.insertbmp2)
         };
         e.Graphics.DrawImage(pictureBox3.Image, 4f * ((float)((float)this.numericUpDown16.Value) - (float)((float)this.numericUpDown3.Value)),
                              4f * ((float)((float)this.numericUpDown17.Value) - (float)((float)this.numericUpDown2.Value)),
                              4f * (float)((float)this.numericUpDown18.Value),
                              4f * (float)((float)this.numericUpDown19.Value));
     }
     if (!this.checkBox4.Checked)
     {
         this.saveset[16] = 0;
     }
     else
     {
         // 燕尾
         Bitmap bitmap10 = new Bitmap(".\\ICON\\d3dx9_307");
         e.Graphics.DrawImage(bitmap10, -2 * ((int)printwidthx + (int)this.numericUpDown3.Value) + 30 + 4 * (printwidthx - (float)((float)this.numericUpDown3.Value)),
                              (float)(20 - 20 * (int)this.numericUpDown3.Value / 13),
                              (float)(4 * (int)this.numericUpDown3.Value) - 60,
                              (float)(20 * (int)this.numericUpDown3.Value / 13 - 31));
         this.saveset[16] = 1;
     }
     if (!this.checkBox2.Checked)
     {
         this.saveset[17] = 0;
     }
     else
     {
         Bitmap bitmap11 = new Bitmap(".\\ICON\\d3dx9_306");
         e.Graphics.DrawImage(bitmap11, 2 * ((int)printwidthx - (int)this.numericUpDown3.Value) - 4f * (float)((float)this.numericUpDown3.Value),
                              (float)(10 - 4 * (int)this.numericUpDown2.Value),
                              (float)(4 * (int)this.numericUpDown3.Value), 3f);
         e.Graphics.DrawImage(bitmap11, 2 * ((int)printwidthx - (int)this.numericUpDown3.Value) - 4f * (float)((float)this.numericUpDown3.Value), -10f,
                              (float)(4 * (int)this.numericUpDown3.Value), 3f);
         this.saveset[17] = 1;
     }
     if (!this.checkBox1.Checked)
     {
         this.saveset[15] = 0;
     }
     else
     {
         StringFormat stringFormat2 = new StringFormat()
         {
             Alignment = StringAlignment.Center
         };
         this.saveset[15] = 1;
         if (this.checkBox3.Checked)
         {
             Bitmap bitmap12 = new Bitmap(".\\ICON\\LOGO.bmp");
             e.Graphics.DrawImage(bitmap12, 2 * ((int)printwidthx - (int)this.numericUpDown3.Value) + 40 - 4f * (float)((float)this.numericUpDown3.Value),
                                  4f * (float)((float)this.numericUpDown10.Value) - 6f * (float)((float)this.numericUpDown3.Value) - 140f,
                                  4f * (float)((float)this.numericUpDown3.Value) - 80f,
                                  4f * (float)((float)this.numericUpDown3.Value) - 80f);
             this.saveset[15] = 2;
         }
         e.Graphics.DrawString(this.textBox2.Text, new System.Drawing.Font("黑体", (float)((float)this.numericUpDown3.Value) / (float)luokuany, FontStyle.Bold, GraphicsUnit.Millimeter), solidBrush,
                              -2f * printwidthx + 4 * (printwidthx - (float)((float)this.numericUpDown3.Value)),
                              4f * (float)((float)this.numericUpDown10.Value) - 3f * (float)((float)this.numericUpDown3.Value) / 2f - 250f, stringFormat2);
         e.Graphics.DrawString(this.textBox3.Text, new System.Drawing.Font("黑体", (float)((float)this.numericUpDown3.Value) / (float)luokuany, FontStyle.Bold, GraphicsUnit.Millimeter), solidBrush,
                              -2f * printwidthx + 4 * (printwidthx - (float)((float)this.numericUpDown3.Value)),
                              4f * (float)((float)this.numericUpDown10.Value) - (float)((float)this.numericUpDown3.Value) - 250f + 4 * (float)((float)this.numericUpDown20.Value), stringFormat2);
         e.Graphics.DrawString(this.textBox4.Text, new System.Drawing.Font("黑体", (float)((float)this.numericUpDown3.Value) / (float)luokuany, FontStyle.Bold, GraphicsUnit.Millimeter), solidBrush,
                              -2f * printwidthx + 4 * (printwidthx - (float)((float)this.numericUpDown3.Value)),
                              4f * (float)((float)this.numericUpDown10.Value) - (float)((float)this.numericUpDown3.Value) / 2f - 250f + 8 * (float)((float)this.numericUpDown20.Value), stringFormat2);
         e.Graphics.DrawString(this.textBox7.Text, new System.Drawing.Font("黑体", (float)((float)this.numericUpDown3.Value) / (float)luokuany, FontStyle.Bold, GraphicsUnit.Millimeter), solidBrush,
                              -2f * printwidthx + 4 * (printwidthx - (float)((float)this.numericUpDown3.Value)),
                              4f * (float)((float)this.numericUpDown10.Value) - 250f + 12 * (float)((float)this.numericUpDown20.Value), stringFormat2);
     }
     float value1 = (float)((float)this.numericUpDown34.Value) / (float)((float)this.numericUpDown33.Value);
     float single1 = 2f * printwidthx + 4f * (float)value;
     float value2 = (2f * (float)((float)this.numericUpDown2.Value) + 4f * (float)single) / value1 - (float)(2 * textLength) * (float)((float)(this.numericUpDown33.Value + this.numericUpDown35.Value));
     float single2 = 0f;
     e.Graphics.ScaleTransform(1f, value1);
     int currentSection = 0; // 用于追踪当前段落
     int maxLength = 0; // 声明最大长度变量
     for (int j = 0; j < text_new.Length; j++)
     {
         if (num == 0 && text_new[j].ToString() == "/")
         {
             num = 1;
             value2 = value2 + (float)((float)this.numericUpDown33.Value);
             single2 = value2;
             num2 = j;
         }
         else if (num == 1 && text_new[j].ToString() == "*")
         {
             num = 2;
             value2 = single2;
             num1 = j;
         }
         else if (num == 2 && text_new[j].ToString() == "*")
         {
             num = 3;
             value2 = single2;
             num3 = j;
         }
         else if ((num == 3 || num == 2) && text_new[j].ToString() == "/")
         {
             num = 0;
             num2 = num1 - num2 - 1;
             num1 = num3 > 0 ? num3 - num1 - 1 : j - num1 - 1;
             maxLength = num3 > 0 ? Math.Max(Math.Max(num2, num1), j - num3 - 1) : Math.Max(num2, num1);
             value2 = single2 + (float)(5 * maxLength) * ((float)this.numericUpDown33.Value + (float)this.numericUpDown35.Value) / 2f + (3f * (float)this.numericUpDown35.Value - (float)this.numericUpDown33.Value) / 2f;
             single2 = 0f;
             num3 = 0; // 重置num3以便下一个段落使用
             currentSection++; // 进入下一个段落
         }
         else if (num == 0) // 正常文字
         {
             single1 = 2f * printwidthx + 4f * (float)value;
             value2 = value2 + 4f * (float)((float)this.numericUpDown33.Value);
             Graphics graphics = e.Graphics;
             char chr = text_new[j];
             graphics.DrawString(chr.ToString(), font, solidBrush, single1, value2, stringFormat);
             value2 = value2 + 4f * (float)((float)this.numericUpDown35.Value);
         }
         else if (num == 1 || num == 2 || num == 3) // 检查'*'的数量
         {
             int starCount = starCounts[currentSection]; // 获取当前段落的'*'数量
             //float extraSpace = 0;
             if (num == 1) // 第一个“/”到“*”之间的文字
             {
                 if (starCount == 1) // 两排
                 {
                     single1 = 2f * printwidthx + 4f * (float)value - (float)((float)this.numericUpDown3.Value) + 15f;
                 }
                 else if (starCount == 2) // 三排
                 {
                     single1 = 2f * printwidthx + 4f * (float)value - (4f * (float)((float)this.numericUpDown3.Value) - 60f) / 3f;
                     System.Drawing.Font font2 = new System.Drawing.Font(this.comboBox2.Text, (float)((float)this.numericUpDown33.Value * (float)this.numericUpDown32.Value), GraphicsUnit.Millimeter);
                     //240808第三列比例控制
                     font1 = font2;//240808第三列比例控制
                 }
                 // 根据最大行长度动态调整间距
                 value2 = value2 + 5f * (float)((float)this.numericUpDown33.Value) / 2f;//字的纵坐标
                 Graphics graphic = e.Graphics;
                 char chr1 = text_new[j];
                 graphic.DrawString(chr1.ToString(), font1, solidBrush, single1, value2, stringFormat);
                 //extraSpace = 4f*(maxLength-num2 ) ;
                 value2 = value2 + 5f * (float)((float)this.numericUpDown35.Value) / 2f + ((currentSection == 0) ? 8 * (float)this.numericUpDown23.Value : (currentSection == 1) ? 8 * (float)this.numericUpDown26.Value : 8 * (float)this.numericUpDown29.Value);//字距
             }
             else if (num == 2) // “*”到“*”之间的文字
             {
                 if (starCount == 1) // 两排
                 {
                     single1 = 2f * printwidthx + 4f * (float)value + (float)((float)this.numericUpDown3.Value) - 15f;
                 }
                 else if (starCount == 2) // 三排
                 {
                     single1 = 2f * printwidthx + 4f * (float)value;
                     System.Drawing.Font font2 = new System.Drawing.Font(this.comboBox2.Text, (float)((float)this.numericUpDown33.Value * (float)this.numericUpDown32.Value), GraphicsUnit.Millimeter);
                     //240808第三列比例控制
                     font1 = font2;//240808第三列比例控制
                 }
                 // 根据最大行长度动态调整间距
                 value2 = value2 + 5f * (float)((float)this.numericUpDown33.Value) / 2f;//字的纵坐标
                 Graphics graphic = e.Graphics;
                 char chr1 = text_new[j];
                 graphic.DrawString(chr1.ToString(), font1, solidBrush, single1, value2, stringFormat);
                 //extraSpace = 4f*(maxLength-num1  ) ;
                 value2 = value2 + 5f * (float)((float)this.numericUpDown35.Value) / 2f + ((currentSection == 0) ? 8 * (float)this.numericUpDown24.Value : (currentSection == 1) ? 8 * (float)this.numericUpDown27.Value : 8 * (float)this.numericUpDown30.Value);//字距
             }
             else if (num == 3) // “*”到第二个“/”之间的文字
             {
                 single1 = 2f * printwidthx + 4f * (float)value + (4f * (float)((float)this.numericUpDown3.Value) - 60f) / 3f;
                 //System.Drawing.Font font2 = new System.Drawing.Font(this.comboBox2.Text, (float)((float)this.numericUpDown33.Value * (float)this.numericUpDown32.Value), GraphicsUnit.Millimeter);
                 //font1 = font2;
                 // 根据最大行长度动态调整间距
                 value2 = value2 + 5f * (float)((float)this.numericUpDown33.Value) / 2f;//字的纵坐标
                 Graphics graphic = e.Graphics;
                 char chr1 = text_new[j];
                 graphic.DrawString(chr1.ToString(), font1, solidBrush, single1, value2, stringFormat);
                 int col3Length = j - num3 - 1;
                 //extraSpace = 4f*(maxLength-col3Length);
                 value2 = value2 + 5f * (float)((float)this.numericUpDown35.Value) / 2f + ((currentSection == 0) ? 8 * (float)this.numericUpDown25.Value : (currentSection == 1) ? 8 * (float)this.numericUpDown28.Value : 8 * (float)this.numericUpDown31.Value);//字距
             }
         }
     }
    ...
     font.Dispose();
     solidBrush.Dispose();
     stringFormat.Dispose();
     e.Graphics.Dispose();
     e.HasMorePages = false; 
 }

img


目标是要实现图片中红框内空格的高度可以自己定义,比如0.8,0.6,0.3之类自主调节。

  • 写回答

37条回答 默认 最新

  • 阿里嘎多学长 2024-12-13 18:10
    关注
    获得0.50元问题酬金

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解决方案

    你可以使用 Graphics.MeasureString 方法来测量字符串的宽度和高度,然后根据需要调整空格的占用高度。

    以下是一个简单的示例代码:

    private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
        string text = "Hello World";
        Font font = new Font("Arial", 12);
        SizeF textSize = e.Graphics.MeasureString(text, font);
        e.Graphics.DrawString(text, font, Brushes.Black, 0, 0);
    }
    

    在上面的代码中,我们使用 MeasureString 方法来测量字符串的宽度和高度,然后使用 DrawString 方法将字符串绘制到打印页面上。

    如果你想调整空格的占用高度,可以使用 StringFormat 对象来指定字符串的排版格式。例如:

    StringFormat format = new StringFormat();
    format.Trimming = StringTrimming.EllipsisCharacter;
    format.FormatFlags = StringFormatFlags.MeasureTrimming;
    SizeF textSize = e.Graphics.MeasureString(text, font, textSize.Width, format);
    

    在上面的代码中,我们创建了一个 StringFormat 对象,并设置了 Trimming 属性为 EllipsisCharacter,以便将字符串的宽度限制在指定的宽度内。然后,我们使用 MeasureString 方法来测量字符串的宽度和高度,并将结果存储在 textSize 变量中。

    最后,我们可以使用 DrawString 方法将字符串绘制到打印页面上,使用 textSize 变量来指定字符串的宽度和高度。

    e.Graphics.DrawString(text, font, Brushes.Black, 0, 0, textSize);
    

    在上面的代码中,我们使用 DrawString 方法将字符串绘制到打印页面上,并使用 textSize 变量来指定字符串的宽度和高度。

    评论

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 创建了问题 12月13日

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题