日期:2014-05-18  浏览次数:20919 次

未将对象引用到实例
自己新建了一个类库,程序调用的时候提示未将对象引用到实例,如果不用类库,直接用类库的函数代码,则不会提示错误,这是为什么呢
C# code
   public static void ConvertPdf2Image(string pdfFilePath, string imageDirectoryPath,
            int beginPageNum, int endPageNum, ImageFormat format, double zoom = 1)
        {
            Acrobat.CAcroPDDoc pdfDoc = null;
            Acrobat.CAcroPDPage pdfPage = null;
            Acrobat.CAcroRect pdfRect = null;
            Acrobat.CAcroPoint pdfPoint = null;

            //生成操作Pdf文件的Com对象
            pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

            //检查输入参数
            if (!pdfDoc.Open(pdfFilePath))
            {
                throw new FileNotFoundException(string.Format("源文件{0}不存在!", pdfFilePath));
            }

            if (!Directory.Exists(imageDirectoryPath))
            {
                Directory.CreateDirectory(imageDirectoryPath);
            }

            if (beginPageNum <= 0)
            {
                beginPageNum = 1;
            }

            if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0)
            {
                endPageNum = pdfDoc.GetNumPages();
            }

            if (beginPageNum > endPageNum)
            {
                throw new ArgumentException("参数\"beginPageNum\"必须小于\"endPageNum\"!");
            }

            if (format == null)
            {
                format = ImageFormat.Jpeg;

            }

            if (zoom <= 0)
            {
                zoom = 1;
            }
            //


            //转换
            for (int i = beginPageNum; i <= endPageNum; i++)
            {




                //取出当前页
                pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
                //得到当前页的大小
                pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                //生成一个页的裁剪区矩形对象
                pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                //计算当前页经缩放后的实际宽度和高度,zoom==1时,保持原比例大小
                int imgWidth = (int)((double)pdfPoint.x * zoom);
                int imgHeight = (int)((double)pdfPoint.y * zoom);

                //设置裁剪矩形的大小为当前页的大小
                pdfRect.Left = 0;
                pdfRect.right = (short)imgWidth;
                pdfRect.Top = 0;
                pdfRect.bottom = (short)imgHeight;

                //将当前页的裁剪区的内容编成图片后复制到剪贴板中
                pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * zoom));

               [color=#FF0000] IDataObject clipboardData = Clipboard.GetDataObject();[/color]

                //检查剪贴板中的对象是否是图片,如果是图片则将其保存为指定格式的图片文件
                if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);

                    pdfBitmap.Save(
                        Path.Combine(imageDirectoryPath, i.ToString("0000") + "." + format.ToString()), format);

                    pdfBitmap.Dispose();
                }
            }

            //关闭和释放相关COM对象
            pdfDoc.Close();
            Marshal.ReleaseComObject(pdfRect);
            Marshal.ReleaseComObject(pdfPoint);
            Marshal.ReleaseComObject(pdfPage);
            Marshal.ReleaseComObject(pdfDoc);


        }

红色区域是有问题的,如果直接把代码粘贴到程序中不会出现问题,调用类库出现问题,clipboardData=null

------解决方案--------------------
那是不是GetDataObject为空啊,你看你给他赋值了没!
------解决方案--------------------
你先单步调试找出哪个地方引用为null了,然后找问题原因,再想解决办法。
------解决方案--------------------
1. 程序是否使用了多线程?
2. 类库的定义贴出来看看
3. Clipboard 类只能用于设置为单线程单元 (STA) 模式的线程中。若要使用此类,请确保已使用 STAThreadAttribute 属性标记 Mai