Format line with itextsharp

Asked

Viewed 1,121 times

1

How can I build a line with different itextsharp formatting?

I tried that way:

Paragraph paragrafo = new Paragraph(Texto, new Font(Font.FontFamily.COURIER, tamanho, estilo));
doc.Add(paragrafo);

But every "add" he plays on a new line and not ahead of the previous one.

If anyone can help me, since thank you.

  • 1

    What do you mean "ahead of the previous" ?

  • @Leandroangelo I believe he is talking about continuing in the same line as the previous paragraph.

  • This is a standard behavior, the right would be to make a Stringbuilder and generate its text property before passing the paragraph and adding it.

  • @Magnocosta Include whole method code.

  • @Leandroangelo , that’s right, stay on the same line. The code basically this, I can include the whole routine, but will only add the "public void and }" at the end.

  • @Peace, then, at the moment is done just like that, I create a stringbuilder, I line up and send it to the paragraph, but this does not allow me to work the formats, for example: "CLIENT: ABC" being "CLIENT" in bold and ABC not.. understands?

  • Where is your Builder string? you are presenting the wrong part of the code is not where is your problem

  • @Leandroangelo below is the most complete code.

Show 3 more comments

3 answers

1

Phrase texto_1= new Phrase("cliente: ", new Font(Font.FontFamily.TIMES_ROMAN, 12f, Font.BOLD, BaseColor.GRAY)); 

Phrase texto_2 = new Phrase(ViewBag.Nome + " "+ ViewBag.Sobrenome, new Font(Font.FontFamily.TIMES_ROMAN, 12f, Font.NORMAL));


Paragraph texto_2_formatacoes= new Paragraph{texto_1, texto_2 };

doc.add(texto_2_formatacoes)

the 2 texts must be Phrase. If it is Paragraph doesn’t work.

1

Like this:

public void impF(ref StringBuilder ret, int pos, string Texto, bool pulaLinha, float fs = 7.0F, int estilo = 0)
{
    if (pulaLinha)
    {
        string x = ret + l(Texto, " ", pos - linha.Length) + "\r\n";
        ret.Clear();
        linha = "";
        Paragraph paragrafo = new Paragraph(x, new Font(Font.FontFamily.COURIER, fs, estilo));
        doc.Add(paragrafo);
    }
    else
    {
        //paragrafo.Add(l(Texto, " ", pos - linha.Length) + "\r\n");
        ret.Append(l(Texto, " ", pos - linha.Length));
        linha += l(Texto, " ", pos - linha.Length);
    }
}

This way he mounts the line in the stringbuilder and when requested he adds to the paragraph.

I would like to add to the paragraph each independent text with its own style (bold, Italico, size etc)

It would be something like that:

public void impF(ref StringBuilder ret, int pos, string Texto, bool pulaLinha, float fs = 7.0F, int estilo = 0)
{
    Paragraph paragrafo = new Paragraph(Texto, new Font(Font.FontFamily.COURIER, fs, estilo));
    doc.Add(paragrafo);
    if (pulaLinha)
    {
        doc.Add(new Paragraph("\r\n"));
    }
}

This routine is called:

 GeraRelatorio.impF(ref texto, 01, "Operação: " + row["cdescropera"].ToString(), true);

This works perfectly, but the style can only be applied to the entire line.

0

What you can do is create several Phrase with different settings and add to paragraph

Paragraph paragraph = new Paragrah();

Phrase phrase1 = new Phrase("Texto negrito",
           BaseFont.CreateFont(BaseFont.HELVEDICA_BOLD, BaseFont.CP1252, 
           BaseFont.NOT_EMBEDDED));

Phrase phrase2 = new Phrase("Texto itálico",
           BaseFont.CreateFont(BaseFont.HELVEDICA_ITALIC, BaseFont.CP1252, 
           BaseFont.NOT_EMBEDDED));

 paragraph.Add(phrase1);
 paragraph.Add(phrase2);

Another thing you can do is use multiple Contentbyte on the same line instead of using Paragraph. The disadvantage is that Contentbyte does not break line, the challenge would be to identify where they should start (at the end of the previous one). If the previous text does not vary, as for example a label, will work well, if they are varied texts will not work well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.