Пример #1
0
        private bool AddFile(string ofd)
        {
            if (File.Exists(ofd))
            {
                if (chkFiles.FindStringExact(ofd) == -1)
                {
                    using (RasterCodecs codecs = new RasterCodecs())
                    {
                        int pageCount = codecs.GetTotalPages(ofd);

                        if (requiredPageCount != pageCount)
                        {
                            return(false);
                        }
                    }

                    chkFiles.Items.Add(ofd, true);
                    chkFiles.TopIndex = chkFiles.Items.Count - 1;
                }

                // it's already in the list
                return(true);
            }

            return(false);
        }
Пример #2
0
        public static void ExportPdfViaSvg(string filename)
        {
            var outputFile = Path.Combine(Leadtools.Demo.Support.Path.GetOutputPath(), Path.GetFileNameWithoutExtension(filename) + "_Svg.pdf");

            // Setup a new RasterCodecs object
            using (var codecs = new RasterCodecs())
            {
                // Check to see if we can use this method
                // https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~canloadsvg(string).html
                if (!codecs.CanLoadSvg(filename))
                {
                    Console.WriteLine("\nCannot load this file as SVG for conversion to PDF.\nSee: https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~canloadsvg(string).html#remarksSectionHeading");
                    return;
                }

                codecs.Options.RasterizeDocument.Load.Resolution = 300;

                // Get the number of pages in the input document
                var pageCount = codecs.GetTotalPages(filename);

                // Create a new instance of the LEADTOOLS Document Writer
                var docWriter = new DocumentWriter();

                // Set the PDF options
                // https://www.leadtools.com/help/leadtools/v19/dh/ft/leadtools.forms.documentwriters~leadtools.forms.documentwriters.pdfdocumentoptions.html
                var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
                if (pdfOptions != null)
                {
                    pdfOptions.DocumentType = PdfDocumentType.Pdf;
                    docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);
                }

                // Create a new PDF document
                docWriter.BeginDocument(outputFile, DocumentFormat.Pdf);

                var message = "";
                // Loop through all the pages
                for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++)
                {
                    // Get the page as SVG
                    // https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~loadsvg.html
                    var page           = new DocumentSvgPage();
                    var lastMessageLen = message.Length;

                    message = string.Format("\rConverting page {0} of {1}", pageNumber, pageCount);
                    var diff = lastMessageLen - message.Length;
                    Console.Write("{0}{1}", message, (diff > 0 ? new string( ' ', diff ) : ""));
                    using (page.SvgDocument = codecs.LoadSvg(filename, pageNumber, null))
                    {
                        // Add the page
                        docWriter.AddPage(page);
                    }
                }
                Console.Write("\r{0}\r", new String(' ', message.Length));
                // Finally, finish writing the PDF file on disk
                docWriter.EndDocument();
            }
        }
Пример #3
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();
            }
        }
Пример #4
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);
        }