【问题标题】:String should get split on the right side of the page字符串应在页面右侧拆分
【发布时间】:2014-04-25 16:41:12
【问题描述】:

我有一个保存在字符串 (datn1, datn2) 中的路径,并且想在页面上打印该路径。所以我必须看看,如果字符串对于页面宽度来说太长,如果太长,它应该从一个新行开始。

首先我将字符串拆分并保存在字符串数组中:

String datn1 = dateiName1, datn2 = dateiName1;
char[] Trennzeichen = { '\\' };
String[] folders1 = datn1.Split(Trennzeichen);
String[] folders2 = datn2.Split(Trennzeichen); 

我在变量中也有页面的边距:

float leftMargin, rightMargin, topMargin, bottomMargin, width, height;

现在我想在部分字符串数组中添加一个新字符串并查看它是否在页面上。当它到达正确的站点时,它应该换一条新线……我该怎么做? 我的想法:

string path_new;
for (int i = 0; i <= folders1.Length; i++)
{
   If()//How can i say that he should look if the string is inside the margins?
   {
      path_new= folders1[i]+"//";
   }
   else
   {
      path_new= "\n" + folders1[i]+ "//";
   }
}

【问题讨论】:

  • 您的问题中的网站是指网站吗?您使用什么框架在 c# 中构建网站?如果是网站,在服务器上计算字符串的长度有什么用?如果有人的分辨率较低或调整了窗口大小怎么办?
  • 不,我建议了页面的右侧,我想打印... pagesize --> a4
  • 现在更清楚了,但我不明白你想要实现什么。您是在写入要打印的文件的路径,还是通过什么方式打印输出?附加问题:这不取决于字体及其大小吗?
  • 我想在页面上打印路径,因为它太长了,我必须拆分它。是的,这取决于字体和大小。因此,我想查询字符串是否在边距内,如果不是,则应以新行开头。现在看起来像这样:abload.de/img/problem2bkz6.png

标签: c# string split arrays


【解决方案1】:

现在我找到了解决方案:

 private void DrawGraphic(Graphics g) 
{                    
    g.PageUnit = GraphicsUnit.Millimeter;
    String datn1 = dateiName1, datn2 = dateiName2;
                char[] Trennzeichen = { '\\' };
                String[] folders1, folders2;
                if (vergl.X == 1)
                   folders1 = datn1.Split(Trennzeichen);
                else
                   folders1 = new String[0];
                if (vergl.Y == 1)
                   folders2 = datn2.Split(Trennzeichen);
                else
                   folders2 = new String[0];

                if (pageSetupDialog1.PageSettings.Landscape == false)
                {
                  string path_new = "";
                  string path_new2 = "";

                for (int i = 0; i < folders1.Length; i++)
                {
                 string path_temp = path_new + folders1[i] + "//";
                 System.Drawing.Size size_path_temp = TextRenderer.MeasureText(path_temp, new Font("Verdana", 8f));  // get size of string path_temp (in pixel)    
                 double size_path_temp_width = Convert.ToDouble(size_path_temp.Width); 
                 double variable = Convert.ToDouble(rightMargin.ToString())*96/25.4d; //96 = dpi Anzahl

                if (size_path_temp_width < variable)
                 {
                  path_new += folders1[i] + "//";
                 }
                 else
                 {
                  path_new += System.Environment.NewLine + folders1[i] + "//";
                 }
                }

            for (int i = 0; i < folders2.Length; i++)
            {
              string path_temp = path_new2 + folders2[i] + "//";
              System.Drawing.Size size_path_temp = TextRenderer.MeasureText(path_temp, new Font("Verdana", 8f));  // get size of string path_temp (in pixel)    
              double size_path_temp_width = Convert.ToDouble(size_path_temp.Width);
              double variable = Convert.ToDouble(rightMargin.ToString()) * 96 / 25.4d; //96 = dpi

         if (size_path_temp_width < variable)
         {
          path_new2 += folders2[i] + "//";
         }
         else
         {
          path_new2 += System.Environment.NewLine + folders2[i] + "//";
         }
        } 
 g.DrawString(path_new, new Font("Verdana", 8f), new SolidBrush(Color.Black), leftMargin, topMargin+10);
 g.DrawString(path_new2, new Font("Verdana", 8f), new SolidBrush(Color.Black), 20, topMargin + 10 + bmp1.Height / 6 + 25);
} 

dateiName1 和 dateiName2 是保存路径的字符串。 vergl.X 和 vergl.Y 检查是否有路径读入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多