private static DC_PAPER_SIZE[] ReadDC_PAPER_SIZEArray(HGlobalBuffer buffer, int itemByteSize)
        {
            int nItems = buffer.Length / itemByteSize;

            DC_PAPER_SIZE[] result = new DC_PAPER_SIZE[nItems];

            bool shouldRelease = false;

            buffer.Handle.DangerousAddRef(ref shouldRelease);
            try
            {
                IntPtr baseAddr = buffer.Handle.DangerousGetHandle();
                for (int i = 0, offset = 0; i < nItems; i++, offset += itemByteSize)
                {
                    int w = Marshal.ReadInt32(baseAddr, offset);
                    int h = Marshal.ReadInt32(baseAddr, offset + (itemByteSize / 2));
                    result[i] = new DC_PAPER_SIZE(w, h);
                }

                return(result);
            }
            finally
            {
                if (shouldRelease)
                {
                    buffer.Handle.DangerousRelease();
                }
            }
        }
        /// <summary>
        /// Get the default paper width and height
        /// </summary>
        /// <param name="defaultDevMode">devMode that contains the code for the default paper size</param>
        /// <param name="mediaSizeCodes">List of supported paperSizeCodes</param>
        /// <param name="mediaSizes">List of supported paperSize widths and heights</param>
        /// <param name="defaultPaperSize">Out parameter that recieves the default paper width and height</param>
        /// <returns>True if the call succeded</returns>
        public bool GetDefaultPaperSize(DevMode defaultDevMode, IList <short> paperSizeCodes, IList <DC_PAPER_SIZE> paperSizes, out DC_PAPER_SIZE defaultPaperSize)
        {
            defaultPaperSize = new DC_PAPER_SIZE();

            if (defaultDevMode == null)
            {
                if (paperSizes.Count > 0)
                {
                    defaultPaperSize = paperSizes[0];
                    return(true);
                }

                return(false);
            }
            else
            {
                bool hasWidth = false;
                hasWidth = defaultDevMode.IsFieldSet(DevModeFields.DM_PAPERWIDTH);
                if (hasWidth)
                {
                    defaultPaperSize.Width = defaultDevMode.PaperWidth;
                }

                bool hasHeight = false;
                hasHeight = defaultDevMode.IsFieldSet(DevModeFields.DM_PAPERWIDTH);
                if (hasHeight)
                {
                    defaultPaperSize.Height = defaultDevMode.PaperLength;
                }

                if (!hasWidth || !hasHeight)
                {
                    if (defaultDevMode.IsFieldSet(DevModeFields.DM_PAPERSIZE))
                    {
                        int defaultPaperSizeIndex = paperSizeCodes.IndexOf(defaultDevMode.PaperSize);
                        if (0 <= defaultPaperSizeIndex && defaultPaperSizeIndex < paperSizes.Count)
                        {
                            if (!hasWidth)
                            {
                                defaultPaperSize.Width = paperSizes[defaultPaperSizeIndex].Width;
                            }

                            if (!hasHeight)
                            {
                                defaultPaperSize.Height = paperSizes[defaultPaperSizeIndex].Height;
                            }

                            hasWidth = hasHeight = true;
                        }
                    }
                }

                return(hasWidth && hasHeight);
            }
        }