Exemplo n.º 1
0
        private void LoadImage(string imageFileName)
        {
            try
            {
                using (var wait = new WaitCursor())
                {
                    // Get the image number of pages
                    int pageCount;
                    using (var imageInfo = _rasterCodecs.GetInformation(imageFileName, true))
                    {
                        pageCount = imageInfo.TotalPages;
                    }

                    // At this point, we can probably load this image, so save it
                    _imageFileName = imageFileName;

                    // See if we can load this image as SVG
                    // If the user selected that and if the format supports it
                    var useSVG = (_demoOptions.UseSVG && _rasterCodecs.CanLoadSvg(imageFileName));

                    // See if we need the virtualizer
                    // If the user selected that and if we have more than one page
                    var useVirtualizer = (_demoOptions.UseVirtiualizer && pageCount > 1);

                    // We are ready
                    // Ensure that the image viewer will not perform any unnecessary calculations in the middle of adding
                    // and removing items
                    _imageViewer.BeginUpdate();

                    try
                    {
                        // Remove the previous pages
                        _imageViewer.Items.Clear();

                        // Set the image info label
                        this.Text            = string.Format("{0} - {1}", imageFileName, Messager.Caption);
                        _imageInfoLabel.Text = string.Format("Pages:{0} - Use SVG:{1} - Use Virtualizer:{2}",
                                                             pageCount, useSVG ? "Yes" : "No", useVirtualizer ? "Yes" : "No");

                        if (useVirtualizer)
                        {
                            LoadPagesWithVirtualizer(imageFileName, pageCount, useSVG);
                        }
                        else
                        {
                            LoadPagesDirect(imageFileName, pageCount, useSVG);
                        }
                    }
                    finally
                    {
                        _imageViewer.EndUpdate();
                    }
                }
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex);
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 3
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);
            }
        }