Пример #1
0
        private static int Run(string[] arguments)
        {
            RedrawConfig config = ArgumentsProcessor.Process(arguments);

            if (config == null)
            {
                return(1);
            }

            FileInfo inputFile = new FileInfo(config.InputPath);

            if (!inputFile.Exists)
            {
                Console.WriteLine("Specified input file does not exist");
                return(1);
            }

            DirectoryInfo outputDir = new DirectoryInfo(Path.GetDirectoryName(config.OutputPath));

            if (!outputDir.Exists)
            {
                outputDir.Create();
            }

            GhostscriptWrapper.GeneratePageThumbs(inputFile.FullName, config.OutputPath, config.FirstPage, config.LastPage, config.Width, config.Height);

            return(0);
        }
Пример #2
0
 public void GenerateMultiplePageThumbnails()
 {
     GhostscriptWrapper.GeneratePageThumbs(TEST_FILE_LOCATION, MULTIPLE_FILE_LOCATION, 1, MULTIPLE_FILE_PAGE_COUNT, 100, 100);
     for (var i = 1; i <= MULTIPLE_FILE_PAGE_COUNT; i++)
     {
         Assert.IsTrue(File.Exists(String.Format("output{0}.jpg", i)));
     }
 }
Пример #3
0
 public void pdf2ImageByPage(string pdfFile, int pagenumber)
 //static void pdf2Image(string pptfile, string prefix)
 {
     // Do not forget the %d in the output file name  @"Example%d.jpg"
     GhostscriptWrapper.GeneratePageThumbs(exeBase + @"\" + @"d5000.pdf", exeBase + @"\" + @"Example%d.jpg", 1, 15, 100, 100);
     // for a single page [you have to know the page number -- no function in ghostscript]
     // GhostscriptWrapper.GeneratePageThumb(exeBase + @"\" + @"d5000.pdf", exeBase + @"\" + @"Example1.jpg", 1, 100, 100);
 }
Пример #4
0
        private IEnumerable <string> ParsePdfDocumentToImages()
        {
            FileHelper.CheckFilePathExisting(_pathToPdf);
            int    pdfPageCount      = GetPdfPageCount();
            string pdfName           = Path.GetFileNameWithoutExtension(_pathToPdf);
            string imagePathTemplate = new FileInfo(_pathToPdf).DirectoryName + @"\" + pdfName + "_0%d.tiff";

            GhostscriptWrapper.GeneratePageThumbs(_pathToPdf, imagePathTemplate, 1, pdfPageCount, 200, 200);
            return(Directory.GetFiles(new FileInfo(_pathToPdf).DirectoryName, pdfName + "_*"));
        }
        private void SavePdfToImage(string folder, string filepath)
        {
            Logger logger = LogManager.GetCurrentClassLogger();

            logger.Info(folder);

            PdfReader pdfReader     = new PdfReader(filepath);
            int       numberOfPages = pdfReader.NumberOfPages;

            var finalPath = string.Join("/", folder, MULTIPLE_FILE_LOCATION);

            logger.Info(finalPath);
            GhostscriptWrapper.GeneratePageThumbs(filepath, finalPath, 1, numberOfPages, 100, 100);
        }
Пример #6
0
        // Rotina para gerar Thumbnail
        string gerarThumb(string filename)
        {
            string inputPdf     = filename;
            string outputPng    = thumbDir + Path.GetFileNameWithoutExtension(filename) + ".png";
            string outputPngBig = thumbDir + Path.GetFileNameWithoutExtension(filename) + "_big.png";

            //Criar diretório de thumbnails, caso não exista
            if (!Directory.Exists(thumbDir))
            {
                Directory.CreateDirectory(thumbDir);
            }

            if (File.Exists(outputPng) && File.Exists(outputPngBig))
            {
                return(outputPng);
            }
            else
            {
                if (!File.Exists(outputPng))
                {
                    try { GhostscriptWrapper.GeneratePageThumbs(inputPdf, outputPng, 1, 1, 10, 10); }
                    catch { userInfoMsg("!Error: Could not generate thumbnail for the file: " + Path.GetFileName(filename)); }
                }
                if (!File.Exists(outputPngBig))
                {
                    try { GhostscriptWrapper.GeneratePageThumbs(inputPdf, outputPngBig, 1, 1, 100, 100); }
                    catch { userInfoMsg("!Error: Could not generate preview of the file: " + Path.GetFileName(filename)); }
                }

                if (File.Exists(outputPng))
                {
                    return(outputPng);
                }
                else
                {
                    return(string.Empty);
                }
            }
        }
Пример #7
0
 static void Main(string[] args)
 {
     GhostscriptWrapper.GeneratePageThumbs(@"C:\Users\User\Downloads\English_Medium_Extra_for_WEB-2.pdf",
                                           "Example.png", 1, 3, 130, 130);
 }
Пример #8
0
        public async Task ProcessFileAsync(long id, CloudBlockBlob blob, IBinder binder, ILogger log, CancellationToken token)
        {
            var tempDirectory = Path.Combine(Path.GetTempPath(), id.ToString());

            try
            {
                Directory.CreateDirectory(tempDirectory);

                await using var sr = await blob.OpenReadAsync();

                var directory = blob.Parent;
                var textBlob  = directory.GetBlockBlobReference("text.txt");

                textBlob.Properties.ContentType = "text/plain";
                var text2 = await _convertDocumentApi.ConvertDocumentPptxToTxtAsync(sr);

                var text = text2.TextResult;
                text = StripUnwantedChars(text);

                await textBlob.UploadTextAsync(text ?? string.Empty);

                sr.Seek(0, SeekOrigin.Begin);
                var bytes = await _convertDocumentApi.ConvertDocumentAutodetectToPdfAsync(sr);

                var inputFileNamePath = Path.Combine(tempDirectory, "in.pdf");

                await File.WriteAllBytesAsync(inputFileNamePath, bytes, token);

                log.LogInformation($"location of file is {inputFileNamePath}");
                var outputPath = Path.Combine(tempDirectory, "output");
                Directory.CreateDirectory(outputPath);
                log.LogInformation($"location of output is {outputPath}");
                GhostscriptWrapper.GeneratePageThumbs(inputFileNamePath, Path.Combine(outputPath, "%d.jpg"), 1, 1000,
                                                      150, 150);

                var files = Directory.GetFiles(outputPath);
                textBlob.Metadata["PageCount"] = files.Length.ToString();
                await textBlob.SetMetadataAsync();

                var tasks = new List <Task>();
                foreach (var file in files)
                {
                    var fileName     = int.Parse(Path.GetFileNameWithoutExtension(file));
                    var blobToUpload = directory.GetBlockBlobReference($"preview-{--fileName}.jpg");
                    blob.Properties.ContentType = "image/jpeg";
                    var t = blobToUpload.UploadFromFileAsync(file);
                    tasks.Add(t);
                    //File.Delete(file);
                }

                await Task.WhenAll(tasks);
            }
            finally
            {
                Directory.Delete(tempDirectory, true);
            }
            //var text = text2.TextResult;
            //if (result.Successful == false)
            //{
            //    throw new ArgumentException($"ConvertDocumentAutodetectToPngArrayAsync return false in id {id}");
            //}

            //var imagesResult = result.PngResultPages;
            //var pageCount = imagesResult.Count;


            //textBlob.Metadata["PageCount"] = pageCount.ToString();
            //await textBlob.SetMetadataAsync();

            //var starter = await binder.BindAsync<IDurableOrchestrationClient>(new DurableClientAttribute(), token);


            //await starter.PurgeInstanceHistoryAsync($"ProcessPowerPoint-{id}");
            //await starter.StartNewAsync("ProcessPowerPoint", $"ProcessPowerPoint-{id}", new PowerPointOrchestrationInput
            //{
            //    Id = id,
            //    Images = imagesResult
            //});

            //var httpClient = await binder.BindAsync<HttpClient>(new HttpClientFactoryAttribute(), token);
            //var listOfTasks = imagesResult.Select(async imageResult =>
            //{
            //    var previewBlob = directory.GetBlockBlobReference($"preview-{imageResult.PageNumber - 1}.jpg");
            //    previewBlob.Properties.ContentType = "image/jpeg";
            //    await using var imageStream = await httpClient.GetStreamAsync(imageResult.URL);
            //    using var input = Image.Load<Rgba32>(imageStream);
            //    await using var blobWriteStream = await previewBlob.OpenWriteAsync();
            //    input.SaveAsJpeg(blobWriteStream);
            //});
            //await Task.WhenAll(listOfTasks);
        }
Пример #9
0
        void ApriSingoloPDFClick(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFile;
            openFile        = new System.Windows.Forms.OpenFileDialog();
            openFile.Filter = "PDF files *.pdf|*.pdf";
            openFile.Title  = "Select a file";
            if (openFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            inputBox.Text = openFile.FileName;
            if (pagePreviewPanel.BackgroundImage != null)
            {
                pagePreviewPanel.BackgroundImage.Dispose();
            }

            try
            {
                reader = new PdfReader(inputBox.Text);
            }
            catch
            {
                PwdDialog dlg = new PwdDialog();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    string pwd = (dlg.Controls["pwdTextBox"] as TextBox).Text;
                    reader = new PdfReader(inputBox.Text, Tools.StrToByteArray(pwd));
                }
                else
                {
                    inputBox.Text = "";
                    return;
                }
            }
            MetaData md = new MetaData();

            md.Info = reader.Info;

            authorBox.Text  = md.Author;
            titleBox.Text   = md.Title;
            subjectBox.Text = md.Subject;
            kwBox.Text      = md.Keywords;
            creatorBox.Text = md.Creator;
            prodBox.Text    = md.Producer;
            string MULTIPLE_FILE_LOCATION = TmpLocation + "\\output%d.jpg";

            if (!Directory.Exists(TmpLocation))
            {
                Directory.CreateDirectory(TmpLocation);
            }
            else
            {
                int i = 1;
                while (File.Exists(TmpLocation + "\\output" + i + ".jpg"))
                {
                    try {
                        File.Delete(TmpLocation + "\\output" + i + ".jpg");
                    } catch (Exception ex) {
                        MessageBox.Show(ex.Message, "Errore nel cancellare i files temporanei");
                    }
                    i++;
                }
            }
            try {
                GhostscriptWrapper.GeneratePageThumbs(openFile.FileName, MULTIPLE_FILE_LOCATION, 1, reader.NumberOfPages, 20, 20);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Errore nella generazione dell'anteprima");
            }

            numberOfPagesUpDown.Maximum = reader.NumberOfPages;
            numberOfPagesUpDown.Minimum = 1;
            numberOfPagesUpDown.Value   = 1;
            numberOfPagesUpDown_ValueChanged(numberOfPagesUpDown, null);

            sigPicture.Left = 0;
            sigPicture.Top  = sigPicture.Parent.Height - sigPicture.Height;
            sigPictureMove(sigPicture, null);

            sigPicture.Width  = 50;
            sigPicture.Height = 20;
            sigPictureResize(sigPicture, null);
        }