/// <summary>
        /// PDF转Image
        /// </summary>
        /// <param name="pdfInputPath">PDF文件</param>
        /// <param name="imageOutputPath">图片文件路径</param>
        /// <param name="imageName">图片名称</param>
        /// <param name="startPageNum">从第几页开始</param>
        /// <param name="endPageNum">到第几页结束</param>
        /// <param name="imageFormat">生成图片类型</param>
        /// <param name="resolution">显示倍数</param>
        public bool ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
                                     string imageName, string Hospsampleid, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
        {
            bool          falg   = true;
            List <string> Sqllst = new List <string>();
            //执行的SQL集合
            string     errormsg = "";
            SortedList SQLlist  = new SortedList(new MySort());
            int        errcount = 0; //标记当前pdf每页是否都已经转图片成功

            Acrobat.CAcroPDDoc  pdfDoc   = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
            Acrobat.CAcroPDPage pdfPage  = null;
            Acrobat.CAcroRect   pdfRect  = null;
            Acrobat.CAcroPoint  pdfPoint = null;
            try
            {
                //yhl 2017.3.22pdf转图片之前先清空粘贴板信息,防止图片信息没清空,下个条码存了上个条码的信息
                //IDataObject clipboardData1 = Clipboard.GetDataObject();
                //if (clipboardData1.GetDataPresent(DataFormats.Bitmap))
                //{
                Clipboard.Clear();
                // }
                // Create the document (Can only create the AcroExch.PDDoc object using late-binding)
                // Note using VisualBasic helper functions, have to add reference to DLL
                // pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");//.CreateObject("AcroExch.PDDoc", "");
                //var CreatePdf = Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
                // pdfDoc = (Acrobat.CAcroPDDoc)CreatePdf;

                // pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);
                // validate parameter
                if (!pdfDoc.Open(pdfInputPath))
                {
                    throw new FileNotFoundException();
                }
                if (!Directory.Exists(imageOutputPath))
                {
                    Directory.CreateDirectory(imageOutputPath);
                }
                if (startPageNum <= 0)
                {
                    startPageNum = 1;
                }
                if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0)
                {
                    endPageNum = pdfDoc.GetNumPages();
                }
                if (startPageNum > endPageNum)
                {
                    int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
                }
                if (imageFormat == null)
                {
                    imageFormat = ImageFormat.Jpeg;
                }
                if (resolution <= 0)
                {
                    resolution = 1;
                }

                // start to convert each page
                for (int i = startPageNum; i <= endPageNum; i++)
                {
                    pdfPage  = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i - 1);
                    pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                    pdfRect  = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");

                    int imgWidth  = (int)((double)pdfPoint.x * resolution);
                    int imgHeight = (int)((double)pdfPoint.y * resolution);

                    pdfRect.Left   = 0;
                    pdfRect.right  = (short)imgWidth;
                    pdfRect.Top    = 0;
                    pdfRect.bottom = (short)imgHeight;

                    pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));

                    IDataObject clipboardData = Clipboard.GetDataObject();
                    if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                    {
                        Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
                        pdfBitmap.Save(Path.Combine(imageOutputPath, imageName) + "_" + i + ".jpg", imageFormat);
                        pdfBitmap.Dispose();
                    }

                    string imgpath = Path.Combine(imageOutputPath, imageName) + "_" + i + ".jpg";
                    if (!File.Exists(imgpath) || File.ReadAllBytes(imgpath).Count() <= 0)
                    {
                        errcount++;
                    }

                    string          imageAddress = getImageString(Path.Combine(imageOutputPath, imageName) + "_" + i + ".jpg");
                    int             maxid        = new TableIDDAO().GetNextId("DAAN_LIS_REPORT");
                    DAAN_LIS_REPORT dalisReport  = new DAAN_LIS_REPORT();
                    dalisReport.DAAN_ID       = maxid.ToString();
                    dalisReport.DAAN_JPG      = Convert.FromBase64String(imageAddress);
                    dalisReport.DAAN_BAR_CODE = Hospsampleid;
                    SQLlist.Add(new Hashtable()
                    {
                        { "INSERT", "InsertDAAN_LIS_REPORT" }
                    }, dalisReport);
                }

                pdfDoc.Close();
                Marshal.ReleaseComObject(pdfPage);
                Marshal.ReleaseComObject(pdfRect);
                Marshal.ReleaseComObject(pdfDoc);
                Marshal.ReleaseComObject(pdfPoint);
                falg = service.ExecuteSqlTran(SQLlist, ref errormsg);
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                SetTB(imageName + "条码转换失败!" + ex.Message);
                falg = false;
                errcount++;

                pdfDoc.Close();
                Marshal.ReleaseComObject(pdfPage);
                Marshal.ReleaseComObject(pdfRect);
                Marshal.ReleaseComObject(pdfDoc);
                Marshal.ReleaseComObject(pdfPoint);
            }

            if (errcount > 0)
            {
                falg = false;
            }
            return(falg);
        }
示例#2
0
        /// <summary>
        /// 获取序列 主表序列 ,SQl中表名为Ora中序列名
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int GetMicorgResultID()
        {
            TableIDDAO tableid = new TableIDDAO();

            return(tableid.GetNextId("SEQ_DA_MICORGRESULT"));
        }