コード例 #1
0
ファイル: PdfToImage.cs プロジェクト: slledru/PdfReport
        void runJob(Action job)
        {
            if (!File.Exists(PdfFilePath))
            {
                throw new InvalidOperationException(BadFileErrorMessage);
            }

            var acrobatPdfDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");

            if (acrobatPdfDocType == null || !isAdobeSdkInstalled)
            {
                throw new InvalidOperationException(SdkError);
            }

            _pdfDoc = (CAcroPDDoc)Activator.CreateInstance(acrobatPdfDocType);
            if (_pdfDoc == null)
            {
                throw new InvalidOperationException(AdobeObjectsErrorMessage);
            }

            var acrobatPdfRectType = Type.GetTypeFromProgID("AcroExch.Rect");

            _pdfRect = (CAcroRect)Activator.CreateInstance(acrobatPdfRectType);

            var result = _pdfDoc.Open(PdfFilePath);

            if (!result)
            {
                throw new InvalidOperationException(BadFileErrorMessage);
            }

            job();

            releaseComObjects();
        }
コード例 #2
0
        private void ScootRect(CAcroRect cAcroRect, int verticalShift, int horizontalShift)
        {
            cAcroRect.Top    += (short)verticalShift;
            cAcroRect.bottom += (short)verticalShift;

            cAcroRect.Left  += (short)horizontalShift;
            cAcroRect.right += (short)horizontalShift;
        }
コード例 #3
0
 public Pdf(string fileName)
 {
     _fileName = fileName;
     _document = (CAcroPDDoc)Interaction.CreateObject("AcroExch.PDDoc", "");
     if (!_document.Open(fileName))
     {
         throw new FileNotFoundException(fileName);
     }
     _rect = (CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
 }
コード例 #4
0
        private string SearchAround(int top, int bottom, int left, int right, int page, string expected, double threshold)
        {
            CAcroRect cAcroRect = Factory.TypeLoader.GetRect();

            cAcroRect.Top    = (short)top;
            cAcroRect.bottom = (short)bottom;
            cAcroRect.Left   = (short)left;
            cAcroRect.right  = (short)right;


            string found = "";

            for (int instruction = 0; instruction < 5; instruction++)
            {
                switch (instruction)
                {
                case 0:
                    found = ReadRect(cAcroRect, page);
                    break;

                case 1:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                case 2:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                case 3:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                case 4:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                default:
                    found = "";
                    break;
                }

                found = TrimAtLineBreak(found);
                if (Levenshtein.EditDistance.IsCloseTo(found, expected, threshold))
                {
                    break;
                }
            }

            return(found);
        }
コード例 #5
0
ファイル: Form1.cs プロジェクト: kahn520/PdfScreenShot
        private void ExportAsImage(string strFile, string strExportPath)
        {
            if (!Directory.Exists(strExportPath))
            {
                Directory.CreateDirectory(strExportPath);
            }
            Acrobat.AcroPDDoc pdf = new AcroPDDoc();
            pdf.Open(strFile);

            int pageCount = Convert.ToInt32(txtPage.Text);
            int count     = pdf.GetNumPages();

            for (int i = 0; i < count; i++)
            {
                if (i == pageCount)
                {
                    break;
                }
                listMsg.Items.Add($"正在导出第{i + 1}页(共{count}页)");
                listMsg.TopIndex = listMsg.Items.Count - 1;
                int zoom = 1;
                while (true)
                {
                    try
                    {
                        CAcroPDPage page      = (CAcroPDPage)pdf.AcquirePage(i);
                        CAcroRect   pdfRect   = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
                        CAcroPoint  pdfPoint  = (Acrobat.CAcroPoint)page.GetSize();
                        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;


                        page.CopyToClipboard(pdfRect, 0, 0, (short)(100 * zoom));
                        Object obj = Clipboard.GetData(DataFormats.Bitmap);
                        using (Bitmap bitmap = (Bitmap)obj)
                        {
                            SavePicture(bitmap, strExportPath, Path.GetFileNameWithoutExtension(strFile), (i + 1).ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        continue;
                    }

                    break;
                }
            }
        }
コード例 #6
0
        private string SearchAroundRegex(int top, int bottom, int left, int right, int page, Regex exp)
        {
            CAcroRect cAcroRect = Factory.TypeLoader.GetRect();

            cAcroRect.Top    = (short)top;
            cAcroRect.bottom = (short)bottom;
            cAcroRect.Left   = (short)left;
            cAcroRect.right  = (short)right;


            string found = "";

            for (int instruction = 0; instruction < 5; instruction++)
            {
                switch (instruction)
                {
                case 0:
                    found = ReadRect(cAcroRect, page);
                    break;

                case 1:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                case 2:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                case 3:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                case 4:
                    ScootRect(cAcroRect, 5, 0);
                    found = ReadRect(cAcroRect, page);
                    break;

                default:
                    found = "";
                    break;
                }

                if (exp.IsMatch(found))
                {
                    break;
                }
            }

            return(found);
        }
コード例 #7
0
        private string ReadRect(CAcroRect cAcroRect, int page)
        {
            CAcroPDTextSelect textSelect = (CAcroPDTextSelect)_pdDoc.CreateTextSelect(page, cAcroRect);

            if (textSelect == null)
            {
                return("");
            }
            else
            {
                string text    = "";
                int    endText = textSelect.GetNumText();
                for (int vector = 0; vector < endText; vector++)
                {
                    text += textSelect.GetText(vector);
                }

                return(text);
            }
        }
コード例 #8
0
        //http://www.myexception.cn/c-sharp/1506415.html
        //http://www.aspose.com/docs/display/pdfnet/Generate+Thumbnail+Images+from+PDF+Documents
        public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
                                            string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution)
        {
            CAcroPDDoc  pdfDoc   = null;
            CAcroPDPage pdfPage  = null;
            CAcroRect   pdfRect  = null;
            CAcroPoint  pdfPoint = null;

            // 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 = (CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");

            // 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  = (CAcroPDPage)pdfDoc.AcquirePage(i - 1);
                pdfPoint = (CAcroPoint)pdfPage.GetSize();
                pdfRect  = (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;

                // Render to clipboard, scaled by 100 percent (ie. original size)
                // Even though we want a smaller image, better for us to scale in .NET
                // than Acrobat as it would greek out small text
                pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution));

                IDataObject clipboardData = Clipboard.GetDataObject();

                if (clipboardData != null && clipboardData.GetDataPresent(DataFormats.Bitmap))
                {
                    string _dir  = Path.Combine(imageOutputPath, imageName);
                    string _path = _dir + "_" + i.ToString() + ".jpg";
                    if (File.Exists(_path))
                    {
                        File.Delete(_path);
                    }

                    Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
                    pdfBitmap.Save(_path, imageFormat);
                    pdfBitmap.Dispose();
                }
            }

            pdfDoc.Close();
            Marshal.ReleaseComObject(pdfPage);
            Marshal.ReleaseComObject(pdfRect);
            Marshal.ReleaseComObject(pdfDoc);
            Marshal.ReleaseComObject(pdfPoint);
        }
コード例 #9
0
ファイル: PdfToImage.cs プロジェクト: VahidN/PdfReport
        void runJob(Action job)
        {
            if (!File.Exists(PdfFilePath))
                throw new InvalidOperationException(BadFileErrorMessage);

            var acrobatPdfDocType = Type.GetTypeFromProgID("AcroExch.PDDoc");
            if (acrobatPdfDocType == null || !isAdobeSdkInstalled)
                throw new InvalidOperationException(SdkError);

            _pdfDoc = (CAcroPDDoc)Activator.CreateInstance(acrobatPdfDocType);
            if (_pdfDoc == null)
                throw new InvalidOperationException(AdobeObjectsErrorMessage);

            var acrobatPdfRectType = Type.GetTypeFromProgID("AcroExch.Rect");
            _pdfRect = (CAcroRect)Activator.CreateInstance(acrobatPdfRectType);

            var result = _pdfDoc.Open(PdfFilePath);
            if (!result)
                throw new InvalidOperationException(BadFileErrorMessage);

            job();

            releaseComObjects();
        }