Print a file saved in C #

0

I'm trying to print from windform, it turns out that when I print the document, I get the blank sheet. This is the code with which I try to print:

    private PrintDocument printDocument1 = new PrintDocument();
    private string stringToPrint;
    public Form1()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        LeerArchivo();
        printDocument1.Print();
    }

    private void LeerArchivo()
    {
        string docName = "1.pdf";
        string docPath = @"C:\Firmador\";
        printDocument1.DocumentName = docName;
        using (FileStream stream = new FileStream(docPath + docName, FileMode.Open, FileAccess.Read))
        using (StreamReader reader = new StreamReader(stream))
        {
            stringToPrint = reader.ReadToEnd();
        }
    }

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        int charactersOnPage = 0;
        int linesPerPage = 0;


        e.Graphics.MeasureString(stringToPrint, this.Font,
            e.MarginBounds.Size, StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);


        e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);


        stringToPrint = stringToPrint.Substring(charactersOnPage);

        e.HasMorePages = (stringToPrint.Length > 0);
    }

    private void printButton_Click(object sender, EventArgs e)
    {
        LeerArchivo();
        printDocument1.Print();
    }

This is what stringToPrint contains:

I would like to know if there is a way to correct it or some other way to print the file? Regards

    
asked by Danilo 06.12.2017 в 02:15
source

1 answer

0

There are several points where the error could be, let's see if we can clarify it:

Entry point

What is the entry point? button1_Click or printButton_Click ? I see that the two have the same code inside, which is a bit strange. The usual way to execute the same code from two event handlers is to extract that code to a private method that is called from both event handlers. In any case, have you put a break point at the entry point and have you checked if you actually go through there?

Reading file

Have you entered an entry point and checked that after the stringToPrint = reader.ReadToEnd(); assignment the variable stringToPrint contains the text of the file you want to print?

Printing

Have you put a breakpoint in the printDocument1_PrintPage method to check if you actually pass within that method? If so, does it contain at that moment the variable stringToPrint the text you expect to print? The rest of the code of that method seems correct to me, although I do not know the .NET printing API well

Workaround: Extract from here

You can open the file that you want to print with the program that is configured by default in the computer to print PDF's, send the PDF to the queue of the printer and close the program:

private void SendToPrinter()
{
   ProcessStartInfo info = new ProcessStartInfo();
   info.Verb = "print";                          // Seleccionar el programa para imprimir PDF por defecto
   info.FileName = @"C:\Firmador.pdf";         // Ruta hacia el fichero que quieres imprimir
   info.CreateNoWindow = true;                   // Hacerlo sin mostrar ventana
   info.WindowStyle = ProcessWindowStyle.Hidden; // Y de forma oculta

   Process p = new Process();
   p.StartInfo = info;
   p.Start();  // Lanza el proceso

   p.WaitForInputIdle();
   System.Threading.Thread.Sleep(3000);          // Espera 3 segundos
   if (false == p.CloseMainWindow())
      p.Kill();                                  // Y cierra el programa de imprimir PDF's
}
    
answered by 06.12.2017 / 09:00
source