Datacard Printer, using PrintDocument Failed to read default page settings of the printer

15 Views Asked by At

While using PrintDocument in c# web service, I am trying to read the printer default settings but always not success example code :

The default PageSettings.Landscape is set to true from "Printers in control Panel"

        public string print_card(string strServer, string strPort, string strFileName, string strFileNameBack, int iDoubleFace)
        {
            string iResult = "";
            try
            {
                PrinterSettings mySet = new PrinterSettings();  
                strFileToPrint_Front = "http://" + strServer + ":" + strPort + "/" + strFileName;
                strFileToPrint_Back = "http://" + strServer + ":" + strPort + "/" + strFileNameBack;
                iPrintIndex = 0;
                _iDoubleFace = iDoubleFace;
                PrintDocument pd = new PrintDocument();
        //pd.PrinterSettings = mySet;
                pd.DefaultPageSettings.PrinterSettings = mySet;
                //pd.DefaultPageSettings.PaperSize= new PaperSize("card",  1063, 650);
                //pd.DefaultPageSettings.Landscape= true;
            //pd.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Horizontal;
                pd.PrintPage += PrintPage;
                pd.Print();
                iResult = mySet.PrinterName;
            }catch (Exception ex)
            {
                iResult = ex.Message;
            }

            return iResult + strResult;
        }



        private void PrintPage(object o, PrintPageEventArgs e)
        {
            WebClient wc = new WebClient();

            string strToPrint;
            if (iPrintIndex == 0)
                strToPrint = strFileToPrint_Front;
            else
                strToPrint = strFileToPrint_Back;

            byte[] bytes = wc.DownloadData(strToPrint);
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

        Rectangle m = e.PageBounds;
        if ((double) img.Width / (double) img.Height > (double) img.Width / (double) img.Height)
        {
           m.Height = (int) ((double) img.Height / (double) img.Width * (double) m.Width);
        } else 
        {
        m.Width = (int) ((double) img.Width / (double) img.Height * (double) m.Height); 
        }
            var size = img.Size;
            var width = size.Width;   

            //System.Drawing.Image img = System.Drawing.Image.FromFile("http://localhost:8022/medical_card/311/medimgs/cards/card_8_246.jpg");
            Point loc = new Point(0, 0);
        e.Graphics.PageUnit = GraphicsUnit.Pixel;
            e.Graphics.DrawImage(img, 0, 0, 1063, 635);
            strResult = strResult + "," + width.ToString(); 
            if (_iDoubleFace == 2)
            {
                if (iPrintIndex == 0)
                    e.HasMorePages = true;
                else
                    e.HasMorePages = false;



                iPrintIndex++;
            }



        }

However, it always return false when I call PrinterSettings();

I need this to use other special features in the printer, not the regular known features that I can set as I did below, like (impress card feature).

P.S. when I tried to make a console application using Delphi TPrinter, it did work So, I tried to to call the EXE from the webService - it failed !!

0

There are 0 best solutions below