Question

I have a path saved in string (datn1, datn2) and want to print the path on a page. So I have to look, if the string is too long for the width of the page and if it is too long, it should begin with a new line.

First of all I split the string and saved it in a string array:

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

I also have the margins of the page in variables:

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

Now I want to add a new string to parts of the string array and look, if it's on the page. When it reaches the right site it should make a new line... how can i do that? my idea:

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]+ "//";
   }
}
Était-ce utile?

La solution

now i found a solution:

 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 and dateiName2 is a string in which the paths are saved. vergl.X and vergl.Y checks if there is a path read in

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top