0

On 1 Form I have two buttons (one to print to PDF printer, second to normal printer). I print some bitmap image. However behaviour of the program is strange. Sometimes both buttons work as expected. Sometimes normal printer prints blank, while pdf prints proper, sometimes normal printer do not print anything while pdf printer prints some "code".

Below my code

    private void printBtn_Click(object sender, EventArgs e)
            { ... BmpToPrint = new Bitmap(tmp_bmp);
              tmp_bmp.Dispose();
              string file = "sometext";

              pdoc.PrinterSettings.PrinterName = System.Configuration.ConfigurationManager.AppSettings["Printer"];
              pdoc.DocumentName = file;
              pdoc.DefaultPageSettings.Landscape = false;

              ppv.Document = pdoc; //printpreviewdialog ppv, printdocument pdoc
              printDialog1.Document = pdoc;

                if (printDialog1.PrinterSettings.IsValid) { pdoc.Print(); } }


    private void printPdfBtn_Click(object sender, EventArgs e)
            {... BmpToPrint = new Bitmap(tmp_bmp);
              tmp_bmp.Dispose();

                string file = "sometext" + ".pdf";
                if (!Directory.Exists(directory)) { directory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); }

                pdoc.PrinterSettings.PrinterName = System.Configuration.ConfigurationManager.AppSettings["PdfPrinter"];
                pdoc.PrinterSettings.PrintToFile = true;
                pdoc.DocumentName = file;
                pdoc.PrinterSettings.PrintFileName = Path.Combine(directory, file);
                pdoc.DefaultPageSettings.Landscape = false;

                ppv.Document = pdoc;
                printDialog1.Document = pdoc;
                if (printDialog1.PrinterSettings.IsValid) { pdoc.Print(); } }


private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.Clear(Color.White);
            Point PktWst = new Point(5, 0);
            e.Graphics.DrawImage(BmpToPrint, PktWst);
            e.HasMorePages = false;
        }

sometimes in pdf instead of image appears this

Sometimes it works perfectly printing image both to pdf and printer while sometimes the same image as before is either not printed/blank printed/ or pdf has this artifacts instead.

Shall I reset some settings while using second printer or clean some data somehow?

Mmaa Bbaa
  • 1
  • 1
  • If you keep only printing to the normal printer (or the PDF printer) does it always work? Does the problem only occur when you swap from one printer to the other? Do you get different problems when you swap from PDF to normal to when you swap from normal to PDF? Have you tried using a separate PrintDocument for each printer (e.g. pdocPDF & pdocNormal)? – PaulF Jan 04 '18 at 13:44
  • Problems seem to appear after swap. First print is always ok, nevermind which printer I pick, second (it means on second printer) sometimes too, but also sometimes not.. I see no rule to be honest except the fact that the first is always ok. No, by now I've used only 1 PrintDocument. – Mmaa Bbaa Jan 04 '18 at 14:54
  • I suggest trying 2 PrintDocuments or possibly create a new instance every time you click on the buttons. The data appearing in your PDF file is the PostScript data that would be sent to the normal printer. – PaulF Jan 04 '18 at 15:02
  • Thank you! 2 PrintDocuments Solved it. – Mmaa Bbaa Jan 04 '18 at 15:07

0 Answers0