public static void Convert(ConversionPostedData postedData, Common.Config.GlobalConfiguration globalConfiguration, List <string> supportedImageFormats)
        {
            string destinationType  = postedData.GetDestinationType();
            string documentGuid     = postedData.guid;
            string filesDirectory   = globalConfiguration.GetConversionConfiguration().GetResultDirectory();
            string outputFile       = Path.Combine(filesDirectory, Path.GetFileNameWithoutExtension(documentGuid) + "." + postedData.GetDestinationType());
            string destDocumentType = supportedImageFormats.Contains(Path.GetExtension("." + destinationType)) ? "image" : postedData.GetDestDocumentType();
            string fileNameWoExt    = Path.GetFileNameWithoutExtension(postedData.guid);

            using (Converter converter = new Converter(postedData.guid))
            {
                var convertOptions = GetConvertOptions(destDocumentType, destinationType);

                var documentInfo = converter.GetDocumentInfo();

                if (convertOptions is ImageConvertOptions)
                {
                    string outputFileTemplate = Path.Combine(filesDirectory, fileNameWoExt + "-{0}." + destinationType);

                    if ((documentInfo is SpreadsheetDocumentInfo && ((SpreadsheetDocumentInfo)documentInfo).WorksheetsCount == 1) ||
                        documentInfo.PagesCount == 1)
                    {
                        outputFileTemplate = Path.Combine(filesDirectory, fileNameWoExt + "." + destinationType);
                    }

                    SavePageStream getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);
                    converter.Convert(getPageStream, convertOptions);
                }
                else
                {
                    converter.Convert(outputFile, convertOptions);
                }
            }
        }
Example #2
0
        public static void Run()
        {
            string outputFolder       = Constants.GetOutputDirectoryPath();
            string outputFileTemplate = Path.Combine(outputFolder, "converted-page-{0}.png");

            SavePageStream getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);

            using (Converter converter = new Converter(Constants.SAMPLE_PDF))
            {
                ImageConvertOptions options = new ImageConvertOptions
                {
                    Format               = ImageFileType.Png,
                    FlipMode             = ImageFlipModes.FlipY,
                    Brightness           = 50,
                    Contrast             = 50,
                    Gamma                = 0.5F,
                    Grayscale            = true,
                    HorizontalResolution = 300,
                    VerticalResolution   = 100
                };

                converter.Convert(getPageStream, options);
            }

            Console.WriteLine("\nDocument converted successfully. \nCheck output in {0}", outputFolder);
        }
Example #3
0
        public async Task <HttpResponseMessage> GetThumbnail(string fileName, string folderName)
        {
            var    documentExtension = Path.GetExtension(fileName).TrimStart('.').ToLower();
            string inFilePath        = AppSettings.WorkingDirectory + "/" + folderName + "/" + fileName;

            Task <HttpResponseMessage> result = Task.FromResult(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK
            });


            try
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    SavePageStream getPageStream = page => memoryStream;

                    using (Converter converter = new Converter(inFilePath))
                    {
                        ImageConvertOptions options = new ImageConvertOptions
                        {
                            Format     = ImageFileType.Png,
                            PageNumber = 1,
                            PagesCount = 1,
                            Pages      = new List <int>()
                            {
                                1
                            }
                        };

                        converter.Convert(getPageStream, options);
                    }

                    result.Result.Content = new ByteArrayContent(memoryStream.ToArray());
                    result.Result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                }
            }
            catch (Exception)
            {
                string tmpFileName = "tmp.txt";
                string filePath    = AppSettings.WorkingDirectory + "/" + folderName + "/" + tmpFileName;
                if (!System.IO.File.Exists(filePath))
                {
                    System.IO.File.WriteAllText(filePath, " ");
                }

                result = GetThumbnail(tmpFileName, folderName);
            }

            return(await result);
        }
        public static void Run()
        {
            string outputFolder       = Constants.GetOutputDirectoryPath();
            string outputFileTemplate = Path.Combine(outputFolder, "converted-page-{0}.jpg");

            SavePageStream getPageStream = page => new FileStream(string.Format(outputFileTemplate, page), FileMode.Create);

            using (Converter converter = new Converter(Constants.SAMPLE_PDF))
            {
                ImageConvertOptions options = new ImageConvertOptions
                {
                    Format = ImageFileType.Jpg
                };

                converter.Convert(getPageStream, options);
            }

            Console.WriteLine("\nConversion to jpg completed successfully. \nCheck output in {0}", outputFolder);
        }