字符串应该在页面右侧拆分
本文关键字:拆分 字符串 | 更新日期: 2023-09-27 18:00:04
我有一个保存在字符串(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]+ "//";
}
}
现在我找到了一个解决方案:
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是保存路径的字符串。边缘。X和vergl。Y检查中是否有路径读取