コード例 #1
0
        private OutputDevice GetCorrectOutputDevice(Job job)
        {
            OutputDevice device;

            switch (job.Profile.OutputFormat)
            {
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfA3B:
            case OutputFormat.PdfX:
            case OutputFormat.Pdf:
                device = new PdfDevice(job);
                break;

            case OutputFormat.Png:
                device = new PngDevice(job);
                break;

            case OutputFormat.Jpeg:
                device = new JpegDevice(job);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(job);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(job);
                break;

            default:
                throw new Exception("Illegal OutputFormat specified");
            }
            return(device);
        }
コード例 #2
0
        public static void Run()
        {
            // ExStart:ExtractTextUsingTextDevice
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Text();

            // Open document
            Document pdfDocument = new Document(dataDir + "input.pdf");

            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            // String to hold extracted text
            string extractedText = "";

            foreach (Page pdfPage in pdfDocument.Pages)
            {
                using (MemoryStream textStream = new MemoryStream())
                {
                    // Create text device
                    TextDevice textDevice = new TextDevice();

                    // Set text extraction options - set text extraction mode (Raw or Pure)
                    TextExtractionOptions textExtOptions = new
                                                           TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Pure);
                    textDevice.ExtractionOptions = textExtOptions;

                    // Convert a particular page and save text to the stream
                    textDevice.Process(pdfPage, textStream);
                    // ExStart:SaveTextToStream
                    // Convert a particular page and save text to the stream
                    textDevice.Process(pdfDocument.Pages[1], textStream);
                    // ExEnd:SaveTextToStream

                    // Close memory stream
                    textStream.Close();

                    // Get text from memory stream
                    extractedText = Encoding.Unicode.GetString(textStream.ToArray());
                }
                builder.Append(extractedText);
            }

            dataDir = dataDir + "input_Text_Extracted_out_.txt";
            // Save the extracted text in text file
            File.WriteAllText(dataDir, builder.ToString());
            // ExEnd:ExtractTextUsingTextDevice
            Console.WriteLine("\nText extracted successfully using text device from page of PDF Document.\nFile saved at " + dataDir);
        }
コード例 #3
0
        public static void Run()
        {
            // ExStart:ExtractTextUsingTextDevice
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_AsposePdf_Text();

            // Open document
            Document pdfDocument = new Document( dataDir + "input.pdf");
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            // String to hold extracted text
            string extractedText = "";

            foreach (Page pdfPage in pdfDocument.Pages)
            {
                using (MemoryStream textStream = new MemoryStream())
                {
                    // Create text device
                    TextDevice textDevice = new TextDevice();

                    // Set text extraction options - set text extraction mode (Raw or Pure)
                    TextExtractionOptions textExtOptions = new
                    TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Pure);
                    textDevice.ExtractionOptions = textExtOptions;

                    // Convert a particular page and save text to the stream
                    textDevice.Process(pdfPage, textStream);
                    // ExStart:SaveTextToStream
                    // Convert a particular page and save text to the stream
                    textDevice.Process(pdfDocument.Pages[1], textStream);
                    // ExEnd:SaveTextToStream

                    // Close memory stream
                    textStream.Close();

                    // Get text from memory stream
                    extractedText = Encoding.Unicode.GetString(textStream.ToArray());
                }
                builder.Append(extractedText);
            }

            dataDir = dataDir + "input_Text_Extracted_out.txt";
            // Save the extracted text in text file
            File.WriteAllText(dataDir, builder.ToString());
            // ExEnd:ExtractTextUsingTextDevice            
            Console.WriteLine("\nText extracted successfully using text device from page of PDF Document.\nFile saved at " + dataDir);
        }
コード例 #4
0
        public static OutputDevice GenerateDevice(OutputFormat outputFormat, IFile fileWrap = null)
        {
            var jobStub = GenerateJobStub(outputFormat);

            var fileStub = fileWrap ?? Substitute.For <IFile>();

            var osHelperStub = Substitute.For <IOsHelper>();

            osHelperStub.WindowsFontsFolder.Returns(WindowsFontsFolderDummie);

            var commandLineUtilStub = Substitute.For <ICommandLineUtil>();

            OutputDevice device = null;

            switch (outputFormat)
            {
            case OutputFormat.Jpeg:
                device = new JpegDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Pdf:
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfX:
                device = new PdfDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Png:
                device = new PngDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;
            }

            return(device);
        }
コード例 #5
0
        public static OutputDevice GenerateDevice(OutputFormat outputFormat)
        {
            var jobStub = GenerateJobStub(outputFormat);

            var fileStub = MockRepository.GenerateStub <IFile>();

            var osHelperStub = MockRepository.GenerateStub <IOsHelper>();

            osHelperStub.Stub(x => x.WindowsFontsFolder).Return(WindowsFontsFolderDummie);

            var commandLineUtilStub = MockRepository.GenerateStub <ICommandLineUtil>();

            OutputDevice device = null;

            switch (outputFormat)
            {
            case OutputFormat.Jpeg:
                device = new JpegDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Pdf:
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfX:
                device = new PdfDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Png:
                device = new PngDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;
            }

            return(device);
        }
コード例 #6
0
        private OutputDevice GetOutputDevice(Job job, ConversionMode conversionMode)
        {
            OutputDevice device;

            if (conversionMode == ConversionMode.IntermediateConversion)
            {
                return(new PdfIntermediateDevice(job));
            }

            switch (job.Profile.OutputFormat)
            {
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfA3B:
            case OutputFormat.PdfX:
            case OutputFormat.Pdf:
                device = new PdfDevice(job, conversionMode);
                break;

            case OutputFormat.Png:
                device = new PngDevice(job, conversionMode);
                break;

            case OutputFormat.Jpeg:
                device = new JpegDevice(job, conversionMode);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(job, conversionMode);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(job, conversionMode);
                break;

            default:
                throw new Exception("Illegal OutputFormat specified");
            }
            return(device);
        }
コード例 #7
0
ファイル: GhostscriptJob.cs プロジェクト: u001tag/clawPDF
        /// <summary>
        ///     Run the Job
        /// </summary>
        protected override JobState RunJobWork()
        {
            try
            {
                if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
                {
                    Thread.CurrentThread.Name = "JobWorker";
                }
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                _ghostScript.Output += Ghostscript_Output;
                OnJobCompleted      += (sender, args) => _ghostScript.Output -= Ghostscript_Output;

                OutputFiles.Clear();

                Logger.Trace("Setting up actions");
                SetUpActions();

                Logger.Debug("Starting Ghostscript Job");

                OutputDevice device;
                switch (Profile.OutputFormat)
                {
                case OutputFormat.PdfA1B:
                case OutputFormat.PdfA2B:
                case OutputFormat.PdfX:
                case OutputFormat.Pdf:
                    device = new PdfDevice(this);
                    break;

                case OutputFormat.Png:
                    device = new PngDevice(this);
                    break;

                case OutputFormat.Jpeg:
                    device = new JpegDevice(this);
                    break;

                case OutputFormat.Tif:
                    device = new TiffDevice(this);
                    break;

                case OutputFormat.Txt:
                    device = new TextDevice(this);
                    break;

                default:
                    throw new Exception("Illegal OutputFormat specified");
                }

                Logger.Trace("Output format is: {0}", Profile.OutputFormat.ToString());

                _ghostScript.Output += Ghostscript_Logging;
                var success = _ghostScript.Run(device, JobTempFolder);
                _ghostScript.Output -= Ghostscript_Logging;

                Logger.Trace("Finished Ghostscript execution");

                if (!success)
                {
                    var errorMessage = ExtractGhostscriptErrors(GhostscriptOutput);
                    Logger.Error("Ghostscript execution failed: " + errorMessage);
                    ErrorMessage = errorMessage;

                    JobState = JobState.Failed;
                    return(JobState);
                }

                ProcessOutput();

                Logger.Trace("Moving output files to final location");
                MoveOutputFiles();

                Logger.Trace("Finished Ghostscript Job");
                JobState = JobState.Succeeded;
                return(JobState);
            }
            catch (ProcessingException)
            {
                JobState = JobState.Failed;
                throw;
            }
            catch (DeviceException)
            {
                JobState = JobState.Failed;
                throw;
            }
            catch (Exception ex)
            {
                JobState = JobState.Failed;
                Logger.Error("There was an error while converting the Job {0}: {1}", JobInfo.InfFile, ex);
                throw;
            }
        }