0

I have a label that contains comments and I am using iTextsharp to insert those lines into a pdf now the point where comments come in here is the box for comments int he pdf:

This is the PDF comment box

I will use this code to put in a line:

     string comment = lblComment.text;
// lets say the lblComment.text = "This is a Document where User Added new Address"
         cb.BeginText();
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 11);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Comment, 215, 96, 0); // Insert the Cooments
            cb.EndText();

Now the problem is Comment can be long and I want some way of doing this so if the Line for the Comment is more than these many charechters insert next line and take half of the comment strign to the next line. I tried looking at splitting string on google all i could find:

    string data = "THExxQUICKxxBROWNxxFOX";

return data.Split(new string[] { "xx" }, StringSplitOptions.None);

This is where the string has xx you split it but didnt work.

Francis
  • 56
  • 1
  • 12

3 Answers3

0

From your code I think you're just looking for

return Regex.Split(data, "xx");

but from your question I think maybe you want to recursively return substrings to get numerous lines <= max line length, not actually split on a specific character.

user8675309
  • 591
  • 1
  • 6
  • 24
  • This won't compile. You can't use a string as a parameter to split. It only takes characters, an array of characters or an array of strings. You would have to do `return data.Split(new string[] { "xx" }, StringSplitOptions.None)` https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx – Icemanind Nov 17 '17 at 17:12
  • Yeah, I was thinking of Regex.Split, I'm always mixing those two up. Thanks for catching that, answer updated. – user8675309 Nov 17 '17 at 17:26
0

I'll try to answer this part: "... if the Line for the Comment is more than these many characters insert next line and take half of the comment string to the next line."

public string Summarize(string input, int length)
{
    if (input.Length <= length) return input;
    string result = input.Substring(0, length) + System.Environment.NewLine + input.Substring(length, length/2);
    //You may want to add more logic like spliting the string at the previous whitespace
    return result;
}

edit- There are more efficient ways of performing this task, such as using a StringBuilder, but I am going for readability here.

zambonee
  • 1,599
  • 11
  • 17
  • This kind of worked but a question the string it returns it put's in some random words such as this I gave it a text Haseeb and the length was 2 and this is what i got "Ha\r\ns" – Francis Nov 17 '17 at 17:14
  • `\r\n` are carriage return and newline characters. So, you got "Ha[new line]s". – zambonee Nov 17 '17 at 17:20
  • When I said New Line i didn't mean that line as you see i am using strings to put in 2 lines i meant take the letters to another string and put them in those 2 lines. – Francis Nov 17 '17 at 18:51
  • @Haseeb Could you put an example of a comment string, and the output you need in your original post? – zambonee Nov 17 '17 at 18:54
  • Sure Let me add that to my question – Francis Nov 17 '17 at 19:15
0

Here is something I found for you I believe this is what you are looking for you could put this code in an if statement if string Comments.length>##

  string YourComment = lblComment.text;
    string[] Peaces= a.Split(' ');

    int counter = 0;
    string first = "";
    int Center= a.Length / 2; // Devied it into 2 peaces 
    while (first.Length < Center)
    {
        firstHalf += parts[Peaces] + " ";
        counter++;
    }
    string secondHalf = string.Join(" ", Peaces.Skip(counter)); // Join the string
    string Comment1 = firstHalf ;
    string Comment2 = secondHalf;

Now you could use those 2 Strings for 2 Equal length lines like this

 cb.BeginText();
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 11);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Comment1, 215, 96, 0); // Insert the Cooments
            cb.EndText();
 cb.BeginText();
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 11);
            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Comment2, 215, 96, 0); // Insert the Cooments
            cb.EndText();

I read this one Stack over flow one of the question unfortunatily couldnt find that question link again so cant put a link to credit it

Simple Code
  • 558
  • 9
  • 24