Exemplo n.º 1
0
        private static void ConvertDocumentToImage(
            string inputFile,
            string outputFile,
            RasterImageFormat outputFormat,
            int bitsPerPixel)
        {
            if (!File.Exists(inputFile))
            {
                throw new ArgumentException($"{inputFile} not found.", nameof(inputFile));
            }

            if (bitsPerPixel != 0 && bitsPerPixel != 1 && bitsPerPixel != 2 && bitsPerPixel != 4 &&
                bitsPerPixel != 8 && bitsPerPixel != 16 && bitsPerPixel != 24 && bitsPerPixel != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(bitsPerPixel), bitsPerPixel,
                                                      $"Invalid {nameof(bitsPerPixel)} value");
            }

            using (var codecs = new RasterCodecs())
            {
                codecs.Options.RasterizeDocument.Load.XResolution = 300;
                codecs.Options.RasterizeDocument.Load.YResolution = 300;

                // indicates the start of a loop from the same source file
                codecs.StartOptimizedLoad();

                var totalPages = codecs.GetTotalPages(inputFile);
                if (totalPages > 1 && !RasterCodecs.FormatSupportsMultipageSave(outputFormat))
                {
                    Console.WriteLine($"The {outputFormat} format does not support multiple pages.\n"
                                      + "The resulting file will only contain the only last page of the document.");
                }

                for (var pageNumber = 1; pageNumber <= totalPages; pageNumber++)
                {
                    Console.WriteLine($"Loading and saving page {pageNumber}");
                    using (var rasterImage = codecs.Load(inputFile, bitsPerPixel, CodecsLoadByteOrder.Bgr, pageNumber, pageNumber))
                        codecs.Save(rasterImage, outputFile, outputFormat, bitsPerPixel, 1, -1, 1, CodecsSavePageMode.Append);
                }

                // indicates the end of the load for the source file
                codecs.StopOptimizedLoad();
            }
        }
Exemplo n.º 2
0
        public static List <ImageInfo> ConvertDocumentToImage(
            string inputFile,
            string outputFileTemplate,
            RasterImageFormat outputFormat,
            int bitsPerPixel,
            HashSet <string> justThese)
        {
            if (justThese == null)
            {
                justThese = new HashSet <string>();
            }
            if (!File.Exists(inputFile))
            {
                throw new ArgumentException($"{inputFile} not found.", nameof(inputFile));
            }

            if (bitsPerPixel != 0 && bitsPerPixel != 1 && bitsPerPixel != 2 && bitsPerPixel != 4 &&
                bitsPerPixel != 8 && bitsPerPixel != 16 && bitsPerPixel != 24 && bitsPerPixel != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(bitsPerPixel), bitsPerPixel,
                                                      $"Invalid {nameof(bitsPerPixel)} value");
            }

            var retFiles = new List <ImageInfo>();

            using (var codecs = new RasterCodecs())
            {
                codecs.Options.RasterizeDocument.Load.XResolution = 300;
                codecs.Options.RasterizeDocument.Load.YResolution = 300;

                // indicates the start of a loop from the same source file
                codecs.StartOptimizedLoad();

                var totalPages = codecs.GetTotalPages(inputFile);

                /*
                 * if (totalPages > 1 && !RasterCodecs.FormatSupportsMultipageSave(outputFormat))
                 * throw new NotSupportedException(
                 *    $"The {outputFormat} format does not support multiple pages.");
                 */
                for (var pageNumber = 1; pageNumber <= totalPages; pageNumber++)
                {
                    string newOutfile = outputFileTemplate.Replace("{page}", pageNumber.ToString("D2"));
                    string stem       = Path.GetFileNameWithoutExtension(newOutfile);
                    if (justThese.Count > 0 && !justThese.Contains(stem))
                    {
                        continue;
                    }
                    if (File.Exists(newOutfile))
                    {
                        logger.Info($"File already exists {newOutfile}");
                        retFiles.Add(new ImageInfo()
                        {
                            ImageFileInfo = new FileInfo(newOutfile)
                        });
                        continue;
                    }

                    logger.Info($"Loading and saving page {newOutfile}");
                    var rasterImage =
                        codecs.Load(inputFile, bitsPerPixel, CodecsLoadByteOrder.Bgr, pageNumber, pageNumber);
                    codecs.Save(rasterImage, newOutfile, outputFormat, bitsPerPixel, 1, -1, 1, CodecsSavePageMode.Replace);
                    retFiles.Add(new ImageInfo()
                    {
                        Image = rasterImage, ImageFileInfo = new FileInfo(newOutfile)
                    });
                }

                // indicates the end of the load for the source file
                codecs.StopOptimizedLoad();
            }
            return(retFiles);
        }