public static bool FindTextInPdfFile(string fileFullPath, string text, ref CancellationTokenSource cts)
        {
            var pdfFocus = new PdfFocus();

            try
            {
                pdfFocus.OpenPdf(fileFullPath);
                if (pdfFocus.PageCount > 0)
                {
                    for (var i = 1; i < pdfFocus.PageCount + 1; i++)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            break;
                        }
                        if (pdfFocus.ToText(i, i).IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            pdfFocus.ClosePdf();
                            return(true);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                pdfFocus.ClosePdf();
            }

            return(false);
        }
示例#2
0
    /// <summary>
    /// 将PDF文档转换为图片的方法
    /// </summary>
    /// <param name="pdfInputPath">PDF文件路径</param>
    /// <param name="imageOutputPath">图片输出路径</param>
    /// <param name="imageName">生成图片的名字</param>
    /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
    /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
    /// <param name="imageFormat">设置所需图片格式</param>
    /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
    public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
                                        string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
    {
        PdfFocus pdfFocus = new PdfFocus();

        pdfFocus.OpenPdf(pdfInputPath);

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

        // validate pageNum
        if (startPageNum <= 0)
        {
            startPageNum = 1;
        }

        if (endPageNum > pdfFocus.PageCount)
        {
            endPageNum = pdfFocus.PageCount;
        }

        if (startPageNum > endPageNum)
        {
            int tempPageNum = startPageNum;
            startPageNum = endPageNum;
            endPageNum   = startPageNum;
        }

        // start to convert each page
        for (int i = startPageNum; i <= endPageNum; i++)
        {
            byte[] img = pdfFocus.ToImage(i);
            using (FileStream fs1 = File.Create(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString()))
            {
                fs1.Write(img, 0, img.Length);
            }
        }

        pdfFocus.ClosePdf();
    }
示例#3
0
        private static long TimeCutPage(string fileName)
        {
            SautinSoft.PdfFocus f = new PdfFocus();
            long timeFor1         = 0;
            long timeFor2         = 0;

            f.OpenPdf(fileName);
            f.ImageOptions.Dpi = 320;
            pages = f.PageCount;
            sw.Start();
            f.ToImage(prefixFilePath, 1);
            sw.Stop();
            timeFor1 = sw.ElapsedMilliseconds;
            sw.Start();
            f.ToImage(prefixFilePath, 2);
            sw.Stop();
            timeFor2 = sw.ElapsedMilliseconds;
            long allTime = timeFor1 + ((timeFor2 - timeFor1) * (f.PageCount - 1));

            f.ClosePdf();
            timeForJpeg = allTime;
            sw.Reset();
            return(timeForJpeg);
        }
示例#4
0
        public static void ConvertToImg(string path, ref ProgressBar progressBar, ref double time, ref Label label)
        {
            //Stopwatch sw = new Stopwatch();
            // sw.Start();
            SautinSoft.PdfFocus f = new PdfFocus();
            f.OpenPdf(path);
            int    pageCount      = f.PageCount;
            string prefixFilePath = "PDF\\";

            f.ImageOptions.Dpi = 320;
            Stopwatch sw = new Stopwatch();

            Cutter.setMaximumAndStep(progressBar, f.PageCount * 2);

            for (int i = 1; i <= f.PageCount; i++)
            {
                sw.Start();
                f.ToImage(prefixFilePath + i.ToString() + ".jpeg", i);
                sw.Stop();
                time -= sw.ElapsedMilliseconds;
                TimeCalc.MinuteSeconds(time, label);
                sw.Reset();
            }

            f.ClosePdf();

            for (int i = 1; i <= pageCount; i++)
            {
                sw.Start();
                Cutter.Cut(prefixFilePath + i.ToString() + ".jpeg", i, progressBar);
                sw.Stop();
                time -= sw.ElapsedMilliseconds;
                TimeCalc.MinuteSeconds(time, label);
                sw.Reset();
            }
        }
        public ActionResult <bool> EvaluateStudents([FromBody] List <Evaluation> evaluations)
        {
            var dao = new EvaluationsDao();

            Directory.CreateDirectory(STUDENT_ANS_SHEET_UPLOAD_PATH);
            foreach (var e in evaluations)
            {
                Console.WriteLine("-------------------------------------------------------");
                Console.WriteLine($"Student Id {e.Student.Id}");
                Console.WriteLine($"DateTime {e.DateTime?.ToString("yyyy-MM-dd")}");
                var date     = e.DateTime?.ToString("yyyy_MM_dd");
                var fileName = $"AnswerKey_Student_{e.Student.Id}_{e.Examination.Course.CourseCode}_{date}.pdf";
                var filePath = Path.Combine(STUDENT_ANS_SHEET_UPLOAD_PATH, fileName);
                var bytes    = Convert.FromBase64String(e.AnswerSheet);
                System.IO.File.WriteAllBytes(filePath, bytes);
                Console.WriteLine("File Written");

                var refSheetName =
                    e.Examination.ReferenceAnswerSheet.Substring(
                        e.Examination.ReferenceAnswerSheet.LastIndexOf("/") + 1);

                var refSheetPath = Path.Combine(REFERENCE_ANS_SHEET_UPLOAD_PATH, refSheetName);
                Console.WriteLine($"Ref Sheet Path = {refSheetPath}");

                var studentPdf = new PdfFocus();
                Console.WriteLine($"Opening Student Pdf...");
                studentPdf.OpenPdf(filePath);
                var studentAnsText = studentPdf.ToText();
                Console.WriteLine("Student pdf read successfully!");
                studentPdf.ClosePdf();
                Console.WriteLine("Closing student pdf");

                var refPdf = new PdfFocus();
                Console.WriteLine("Opening reference pdf");
                refPdf.OpenPdf(refSheetPath);
                var refAnsText = refPdf.ToText();
                Console.WriteLine("Reference pdf read successfully!");
                refPdf.ClosePdf();
                Console.WriteLine("Closing reference pdf");

                paralleldots pd = new paralleldots("AliC73YnPPScR8dJJEMD8qxinhFTTUjFPmJGs5yknY0");
                Console.WriteLine("Calculating score");
                var similarity = pd.similarity(studentAnsText, refAnsText);

                var json  = JsonValue.Parse(similarity);
                var score = double.Parse(json["normalized_score"].ToString());
                Console.WriteLine($"Score = {score}");
                var percent = score / 5.0;
                Console.WriteLine($"Percentage = {percent}");

                e.MarksObtained = (int?)(e.Examination.TotalMarks * percent);

                e.AnswerSheet = $"/api/examinations/student-ans-sheet/get/{fileName}";

                Console.WriteLine("Writing result to database");
                dao.CreateEvaluation(e);
                Console.WriteLine("Written Successfully!");
            }

            return(true);
        }