• I am attempting to print a PDF using the control in C#. I am getting an error while the printing dialog is open.

    This is my printing code.

    Code
    private void PrintButton_Click(object sender, System.EventArgs e) {            if (PDFTB.Text.Length == 0){                return;            }            PageNo = 1;            viewer.LoadFromFile(PDFTB.Text);            PrintDocument pd = new PrintDocument();            pd.PrinterSettings.PrinterName = "PDFCreator";            pd.PrintPage += new PrintPageEventHandler(this.PrintPage);            pd.Print();        }public void PrintPage(object sender, PrintPageEventArgs e) {            viewer.PrintTo(PageNo, e.Graphics, 300, 300);            PageNo++;            if (PageNo > viewer.Command(commands.COMPDF_GetPageCount)){                e.HasMorePages = false;            }        }

    The error message is

    Zitat

    Additional information: The object is currently in use elsewhere.

    I was looking through the wrapper code and the printTo method looks like this:

    Code
    public bool PrintTo(uint PageNO, Graphics Canvas, uint ResX, uint ResY)
            {
                bool res;
                Command(commands.COMPDF_PrintHDCSetXRes, ResX);
                Command(commands.COMPDF_PrintHDCSetYRes, ResY);
                Command(commands.COMPDF_PrintHDCSetHDC, Canvas.GetHdc().ToInt32());
                res = (Command(commands.COMPDF_PrintHDC, PageNO) >= 0);
                GC.KeepAlive(Canvas);
                return res;
            }

    There doesn't seem to be a releaseDC call here. Is the DC released in the DLL?[/code]

    • Offizieller Beitrag

    Thanks for this hint - no the DC was forgotten. The code should read like:

    Code
    public bool PrintTo(uint PageNO, Graphics Canvas, uint ResX, uint ResY)		{			bool res;			IntPtr DC = Canvas.GetHdc();			Command(commands.COMPDF_PrintHDCSetXRes, ResX);			Command(commands.COMPDF_PrintHDCSetYRes, ResY);			Command(commands.COMPDF_PrintHDCSetHDC, DC.ToInt32());			res = (Command(commands.COMPDF_PrintHDC, PageNO) >= 0);			Canvas.ReleaseHdc(DC);			GC.KeepAlive(Canvas);			return res;		}

    Please also update: