Exemple #1
0
        public Tuple <byte[], string> CreateSearchablePdf(byte[] fileData, PdfMeta metaData)
        {
            try
            {
                int    PageCountStart = GetPages(fileData);
                string sessionName    = TempData.Instance.CreateNewSession();
                OnCompressorEvent?.Invoke("Created Session:" + sessionName);
                string inputDataFilePath  = TempData.Instance.CreateTempFile(sessionName, ".pdf");
                string outputDataFilePath = TempData.Instance.CreateTempFile(sessionName, ".pdf");
                if (fileData == null || fileData.Length == 0)
                {
                    throw new Exception("No Data in fileData");
                }
                using (FileStream writer = new FileStream(inputDataFilePath, FileMode.Create, FileAccess.Write))
                {
                    writer.Write(fileData, 0, fileData.Length);
                    writer.Flush(true);
                }

                OnCompressorEvent?.Invoke(sessionName + " Wrote binary to file");
                OnCompressorEvent?.Invoke(sessionName + " Begin Compress and Ocr");
                string pageBody       = CompressAndOcr(sessionName, inputDataFilePath, outputDataFilePath, metaData);
                string outputFileName = outputDataFilePath;
                if (PdfSettings.CompressFinalPdf)
                {
                    OnCompressorEvent?.Invoke(sessionName + " Compressing output");
                    GhostScript gs = new GhostScript(GhostScriptPath, PdfSettings.Dpi);
                    outputFileName = gs.CompressPdf(outputDataFilePath, sessionName, PdfSettings.PdfCompatibilityLevel, PdfSettings.DistillerMode,
                                                    PdfSettings.DistillerOptions);
                }

                byte[] outFile      = File.ReadAllBytes(outputFileName);
                int    PageCountEnd = GetPages(outFile);
                OnCompressorEvent?.Invoke(sessionName + " Destroying session");
                TempData.Instance.DestroySession(sessionName);

                if (PageCountEnd != PageCountStart)
                {
                    throw new PageCountMismatchException("Page count is different", PageCountStart, PageCountEnd);
                }

                return(new Tuple <byte[], string>(outFile, pageBody));
            }
            catch (Exception e)
            {
                OnExceptionOccurred?.Invoke(this, e);
                throw new FailedToGenerateException("Error in: CreateSearchablePdf", e);
            }
        }