예제 #1
0
        protected override object LoadItem(ImageViewerItem item)
        {
            // This method is called when an item comes into view
            // and is not cached in memory

            // For this example, all we need is to load the image
            // from the original file. But we can also load other
            // state and data from a database or using deserilization.

            // In our demo, the item index is the page index
            // However, we can use the item .Tag property or derive our
            // own class to hold the data needed to load the page

            // Index is 0-based, so add 1 to get the page number
            var pageNumber = this.ImageViewer.Items.IndexOf(item) + 1;

            // Load the page and return it
            if (_useSVG)
            {
                var svgDocument = _rasterCodecs.LoadSvg(_imageFileName, pageNumber, null) as SvgDocument;

                // Ensure the SVG optimized for fast viewing
                MainForm.OptimizeForView(svgDocument);

                return(svgDocument);
            }
            else
            {
                var rasterImage = _rasterCodecs.Load(_imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, pageNumber, pageNumber);
                return(rasterImage);
            }
        }
예제 #2
0
파일: ToPdf.cs 프로젝트: vjezba/cs-samples
        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 void LoadPagesDirect(string imageFileName, int pageCount, bool useSVG)
        {
            // Loads all the pages into the viewer
            for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++)
            {
                if (useSVG)
                {
                    // Load it as an SVG and add it
                    var svgDocument = _rasterCodecs.LoadSvg(imageFileName, pageNumber, null) as SvgDocument;

                    // Ensure the SVG optimized for fast viewing
                    OptimizeForView(svgDocument);
                    this._imageViewer.Items.AddFromSvgDocument(svgDocument);
                }
                else
                {
                    // Load it as a raster image and add it
                    var rasterImage = _rasterCodecs.Load(imageFileName, pageNumber);
                    this._imageViewer.Items.AddFromImage(rasterImage, 1);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Method to ensure that a files that is sent to the service is valid
        /// </summary>
        internal void ValidateFile(Stream s, ref int lastPage)
        {
            try
            {
                using (var codecs = new RasterCodecs())
                    using (var info = codecs.GetInformation(s, true))
                    {
                        //Now that we have the actual file information, we can verify the properties of the file itself.  We will sanitize the lastPage parameter of the request to make sure that it pointing to a valid location within the file.
                        if (lastPage == -1 || lastPage > info.TotalPages)
                        {
                            lastPage = info.TotalPages;
                        }

                        //Check to determine whether or not a file is raster-based, or if it is a document/vector file.  If it is a document/vector file, we can check and see if it is a format that is supposed by calling codecs.LoadSvg
                        if (info.Document.IsDocumentFile || info.Vector.IsVectorFile)
                        {
                            var options = new CodecsLoadSvgOptions {
                                MaximumElements = DemoConfiguration.MaxSvgElements
                            };
                            using (var svg = codecs.LoadSvg(s, 1, options))
                                if (svg == null)
                                {
                                    throw new RejectedFileException();
                                }
                        }
                    }
            }
            //If the file format is not supported by the LEADTOOLS SDK, or if it isn't possible to pull info from the file (such as the case with txt files), the RasterCodecs.GetInformation call will fail.  This will cause the SDK to throw a RasterException -- which we can catch, and then reject the file.
            catch (RasterException)
            {
                throw new RejectedFileException();
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #5
0
        private void LoadDocument(string fileName, bool loadDefault)
        {
            try
            {
                using (RasterCodecs codecs = new RasterCodecs())
                {
                    // Set load resolution
                    codecs.Options.RasterizeDocument.Load.XResolution = 300;
                    codecs.Options.RasterizeDocument.Load.YResolution = 300;

                    int firstPage = 1;
                    int lastPage  = 1;
                    List <SvgDocument> documents = new List <SvgDocument>();

                    if (!loadDefault)
                    {
                        // Check if the file can be loaded as svg
                        bool canLoadSvg = codecs.CanLoadSvg(fileName);
                        using (CodecsImageInfo info = codecs.GetInformation(fileName, true))
                        {
                            if (!canLoadSvg)
                            {
                                // Check if the file type is not PDF
                                if (info.Format != RasterImageFormat.PdfLeadMrc &&
                                    info.Format != RasterImageFormat.RasPdf &&
                                    info.Format != RasterImageFormat.RasPdfCmyk &&
                                    info.Format != RasterImageFormat.RasPdfG31Dim &&
                                    info.Format != RasterImageFormat.RasPdfG32Dim &&
                                    info.Format != RasterImageFormat.RasPdfG4 &&
                                    info.Format != RasterImageFormat.RasPdfJbig2 &&
                                    info.Format != RasterImageFormat.RasPdfJpeg &&
                                    info.Format != RasterImageFormat.RasPdfJpeg411 &&
                                    info.Format != RasterImageFormat.RasPdfJpeg422 &&
                                    info.Format != RasterImageFormat.RasPdfJpx &&
                                    info.Format != RasterImageFormat.RasPdfLzw &&
                                    info.Format != RasterImageFormat.RasPdfLzwCmyk)
                                {
                                    MessageBox.Show("The selected file can't be loaded as an SVG file", "Invalid File Format", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                            }

                            if (info.TotalPages > 1)
                            {
                                using (ImageFileLoaderPagesDialog dlg = new ImageFileLoaderPagesDialog(info.TotalPages, false))
                                {
                                    if (dlg.ShowDialog(this) == DialogResult.Cancel)
                                    {
                                        return;
                                    }

                                    firstPage = dlg.FirstPage;
                                    lastPage  = dlg.LastPage;
                                }
                            }
                        }
                    }

                    using (WaitCursor wait = new WaitCursor())
                    {
                        for (int page = firstPage; page <= lastPage; page++)
                        {
                            SvgDocument svgDoc = codecs.LoadSvg(fileName, page, _loadSvgOptions) as SvgDocument;
                            documents.Add(svgDoc);
                        }

                        SetDocument(fileName, documents, firstPage);
                    }

                    UpdateControls();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, string.Format("Error {0}{1}{2}", ex.GetType().FullName, Environment.NewLine, ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }