Class to write/read formatted TXT files

Asked

Viewed 372 times

0

I’m using the Classe Textwriter to read/write a card receipt on .txt however it does not have the property of formatting letter size, spacing, etc. This makes the proof printed on the thermal printer come out without symmetry.

My question is, is there another Class in C# that does this process, but with the formatting options?

public void LerConfirmacao()
    {
        if (File.Exists(ConfigurationManager.AppSettings["Comprovante"]))
        {
            File.Create(ConfigurationManager.AppSettings["Comprovante"]).Close();
        }

        TextWriter textWriter = File.AppendText(ConfigurationManager.AppSettings["Comprovante"]);
        string[] linhas = File.ReadAllLines(ConfigurationManager.AppSettings["001Resp"]);
        foreach (string linha in linhas)
        {
            if (linha.Substring(0, 3) == "029")
            {
                textWriter.WriteLine(linha.Substring(10));
            }
        }

        textWriter.Close();

        if (Directory.Exists(ConfigurationManager.AppSettings["Pagamentos"]))
        {
            Directory.CreateDirectory(ConfigurationManager.AppSettings["Pagamentos"]);
        }
        Directory.CreateDirectory(ConfigurationManager.AppSettings["Pagamentos"]);
        //File.Create(ConfigurationManager.AppSettings["Pagamentos"]).Close();
        string nomeArquivo = ConfigurationManager.AppSettings["Pagamentos"] + "Comprovante_" + PedNumero + "_" + PedFrmPgto;
        TextWriter writer = File.AppendText(nomeArquivo);

        foreach (string item in File.ReadAllLines(ConfigurationManager.AppSettings["Comprovante"]))
        {
            writer.WriteLine(item);
        }
        writer.Close();
    }
  • No formation in plain text files.

  • @Is there a way out? Procedure change ... (?)

  • Use a single-spaced font for printing (?)

1 answer

1


Files . txt is not formatted. You must use another extension (Example: . rtf).
What you can do is use a RichTextBox to format the text. Example:

RichTextBox formatar = new RichTextBox();
formatar.Text = "texto1 ABC";
formatar.Select(2,4);
formatar.SelectionBackColor = Color.Red;
formatar.SaveFile(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/teste.rtf");

Browser other questions tagged

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