public void ConvertZIPtoPDFTest()
        {
            // Prepare path to a source zip file
            string documentPath = Path.Combine(DataDir, "test.zip");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "zip-to-pdf.pdf");

            // Create an instance of ZipArchiveMessageHandler
            using var zip = new ZipArchiveMessageHandler(documentPath);

            // Create an instance of the Configuration class
            using var configuration = new Configuration();

            // Add ZipArchiveMessageHandler to the chain of existing message handlers
            configuration
            .GetService <INetworkService>()
            .MessageHandlers.Add(zip);

            // Initialize an HTML document with specified configuration
            using var document = new HTMLDocument("zip:///test.html", configuration);

            // Create the PDF Device
            using var device = new PdfDevice(savePath);

            // Render ZIP to PDF
            document.RenderTo(device);

            Assert.True(File.Exists(savePath));
        }
Beispiel #2
0
        private static void MakePDF(object sender, Aspose.Html.Dom.Events.Event e)

        {
            Console.WriteLine("<MakePDF>");
            if (sender is HTMLDocument doc)
            {
                if (doc.ReadyState.Equals("complete"))
                {
                    Console.WriteLine(doc.ReadyState);
                    var pdfOptions = new PdfRenderingOptions();
                    pdfOptions.PageSetup.AnyPage.Margin = new Aspose.Html.Drawing.Margin(
                        Unit.FromMillimeters(15),
                        Unit.FromMillimeters(10),
                        Unit.FromMillimeters(5),
                        Unit.FromMillimeters(10));
                    pdfOptions.PageSetup.AnyPage.Size = new Aspose.Html.Drawing.Size(Unit.FromCentimeters(14.8), Unit.FromCentimeters(21));
                    using (var pdfDevice = new PdfDevice(pdfOptions, "Adventures.pdf"))
                        using (var renderer = new HtmlRenderer())
                        {
                            // Render the output using HtmlRenderer
                            renderer.Render(pdfDevice, doc);
                        }
                    IsFinished.Set();
                }
                else
                {
                    Console.WriteLine(doc.ReadyState);
                }
            }
            Console.WriteLine("</MakePDF>");
        }
Beispiel #3
0
        public void ZIPtoPDFTimeLoggingTest()
        {
            // Prepare path to a source zip file
            string documentPath = Path.Combine(DataDir, "test.zip");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "zip-to-pdf-duration.pdf");

            // Create an instance of the Configuration class
            using var configuration = new Configuration();
            var service  = configuration.GetService <INetworkService>();
            var handlers = service.MessageHandlers;

            // Custom Schema: ZIP. Add ZipFileSchemaMessageHandler to the end of the pipeline
            handlers.Add(new ZipFileSchemaMessageHandler(new Archive(documentPath)));

            // Duration Logging. Add the StartRequestDurationLoggingMessageHandler at the first place in the pipeline
            handlers.Insert(0, new StartRequestDurationLoggingMessageHandler());

            // Add the StopRequestDurationLoggingMessageHandler to the end of the pipeline
            handlers.Add(new StopRequestDurationLoggingMessageHandler());

            // Initialize an HTML document with specified configuration
            using var document = new HTMLDocument("zip-file:///test.html", configuration);

            // Create the PDF Device
            using var device = new PdfDevice(savePath);

            // Render ZIP to PDF
            document.RenderTo(device);

            Assert.True(File.Exists(savePath));
        }
        private OutputDevice GetCorrectOutputDevice(Job job)
        {
            OutputDevice device;

            switch (job.Profile.OutputFormat)
            {
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfA3B:
            case OutputFormat.PdfX:
            case OutputFormat.Pdf:
                device = new PdfDevice(job);
                break;

            case OutputFormat.Png:
                device = new PngDevice(job);
                break;

            case OutputFormat.Jpeg:
                device = new JpegDevice(job);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(job);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(job);
                break;

            default:
                throw new Exception("Illegal OutputFormat specified");
            }
            return(device);
        }
        public void ExternalCSSTest()
        {
            // Create an instance of HTML document with specified content
            var htmlContent = "<link rel=\"stylesheet\" href=\"https://docs.aspose.com/html/net/editing-a-document/external.css\" type=\"text/css\" />\r\n" +
                              "<div class=\"rect1\" ></div>\r\n" +
                              "<div class=\"rect2\" ></div>\r\n" +
                              "<div class=\"frame\">\r\n" +
                              "<p style=\"font-size:2.5em; color:#ae4566;\"> External CSS </p>\r\n" +
                              "<p class=\"rect3\"> An external CSS can be created once and applied to multiple web pages</p></div>\r\n";

            using (var document = new HTMLDocument(htmlContent, "."))
            {
                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "external-css.html"));

                // Create the instance of the PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "external-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(document.QuerySelectorAll("link").Length > 0);
            }

            Assert.True(File.Exists(Path.Combine(OutputDir, "external-css.html")));
        }
        public void EditInlineCSSTest()
        {
            // Create an instance of an HTML document with specified content
            var content = "<p>InlineCSS </p>";

            using (var document = new HTMLDocument(content, "."))
            {
                // Find the paragraph element to set a style
                var paragraph = (HTMLElement)document.GetElementsByTagName("p").First();

                // Set the style attribute
                paragraph.SetAttribute("style", "font-size:250%; font-family:verdana; color:#cd66aa");

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "edit-inline-css.html"));

                // Create an instance of PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "edit-inline-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-inline-css.pdf")));
            }
        }
        public void EditInternalCSSTest()
        {
            // Create an instance of an HTML document with specified content
            var content = "<div><p>Internal CSS</p><p>An internal CSS is used to define a style for a single HTML page</p></div>";

            using (var document = new HTMLDocument(content, "."))
            {
                var style = document.CreateElement("style");
                style.TextContent = ".frame1 { margin-top:50px; margin-left:50px; padding:20px; width:360px; height:90px; background-color:#a52a2a; font-family:verdana; color:#FFF5EE;} \r\n" +
                                    ".frame2 { margin-top:-90px; margin-left:160px; text-align:center; padding:20px; width:360px; height:100px; background-color:#ADD8E6;}";

                // Find the document header element and append the style element to the header
                var head = document.GetElementsByTagName("head").First();
                head.AppendChild(style);

                // Find the first paragraph element to inspect the styles
                var paragraph = (HTMLElement)document.GetElementsByTagName("p").First();
                paragraph.ClassName = "frame1";

                // Find the last paragraph element to inspect the styles
                var lastParagraph = (HTMLElement)document.GetElementsByTagName("p").Last();
                lastParagraph.ClassName = "frame2";

                // Set a color to the first paragraph
                paragraph.Style.FontSize  = "250%";
                paragraph.Style.TextAlign = "center";

                // Set a font-size to the last paragraph
                lastParagraph.Style.Color      = "#434343";
                lastParagraph.Style.FontSize   = "150%";
                lastParagraph.Style.FontFamily = "verdana";

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "edit-internal-css.html"));

                // Create the instance of the PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "edit-internal-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(document.QuerySelectorAll("style").Length > 0);

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-internal-css.pdf")));

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-internal-css.html")));
            }
        }
 static void ConvertSVGtoPDF()
 {
     using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
     {
         var options = new PdfRenderingOptions()
         {
             PageSetup =
             {
                 Sizing = SizingType.FitContent
             }
         };
         using (var device = new PdfDevice(options, dataDir + "smiley_out.pdf"))
         {
             document.RenderTo(device);
         }
     }
 }
Beispiel #9
0
        public static OutputDevice GenerateDevice(OutputFormat outputFormat, IFile fileWrap = null)
        {
            var jobStub = GenerateJobStub(outputFormat);

            var fileStub = fileWrap ?? Substitute.For <IFile>();

            var osHelperStub = Substitute.For <IOsHelper>();

            osHelperStub.WindowsFontsFolder.Returns(WindowsFontsFolderDummie);

            var commandLineUtilStub = Substitute.For <ICommandLineUtil>();

            OutputDevice device = null;

            switch (outputFormat)
            {
            case OutputFormat.Jpeg:
                device = new JpegDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Pdf:
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfX:
                device = new PdfDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Png:
                device = new PngDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;
            }

            return(device);
        }
        public static OutputDevice GenerateDevice(OutputFormat outputFormat)
        {
            var jobStub = GenerateJobStub(outputFormat);

            var fileStub = MockRepository.GenerateStub <IFile>();

            var osHelperStub = MockRepository.GenerateStub <IOsHelper>();

            osHelperStub.Stub(x => x.WindowsFontsFolder).Return(WindowsFontsFolderDummie);

            var commandLineUtilStub = MockRepository.GenerateStub <ICommandLineUtil>();

            OutputDevice device = null;

            switch (outputFormat)
            {
            case OutputFormat.Jpeg:
                device = new JpegDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Pdf:
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfX:
                device = new PdfDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Png:
                device = new PngDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(jobStub, fileStub, osHelperStub, commandLineUtilStub);
                break;
            }

            return(device);
        }
Beispiel #11
0
        public static void Run()
        {
            //ExStart: ConvertSVGToPDF
            string dataDir = RunExamples.GetDataDir_Data();

            using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
            {
                var options = new PdfRenderingOptions()
                {
                    PageSetup =
                    {
                        AnyPage = new Page(new Size(500, 500))
                    }
                };
                using (var device = new PdfDevice(options, dataDir + "smiley_out.pdf"))
                {
                    document.RenderTo(device);
                }
            }
            //ExEnd: ConvertSVGToPDF
        }
Beispiel #12
0
        private OutputDevice GetOutputDevice(Job job, ConversionMode conversionMode)
        {
            OutputDevice device;

            if (conversionMode == ConversionMode.IntermediateConversion)
            {
                return(new PdfIntermediateDevice(job));
            }

            switch (job.Profile.OutputFormat)
            {
            case OutputFormat.PdfA1B:
            case OutputFormat.PdfA2B:
            case OutputFormat.PdfA3B:
            case OutputFormat.PdfX:
            case OutputFormat.Pdf:
                device = new PdfDevice(job, conversionMode);
                break;

            case OutputFormat.Png:
                device = new PngDevice(job, conversionMode);
                break;

            case OutputFormat.Jpeg:
                device = new JpegDevice(job, conversionMode);
                break;

            case OutputFormat.Tif:
                device = new TiffDevice(job, conversionMode);
                break;

            case OutputFormat.Txt:
                device = new TextDevice(job, conversionMode);
                break;

            default:
                throw new Exception("Illegal OutputFormat specified");
            }
            return(device);
        }
Beispiel #13
0
        ///<Summary>
        /// ConvertEpsToPdf method to convert eps file to pdf
        ///</Summary>
        public Response ConvertEpsToPdf(string fileName, string folderName)
        {
            return(ProcessTask(fileName, folderName, ".pdf", false, false, delegate(string inFilePath, string outPath, string zipOutFolder)
            {
                FileStream psStream = new FileStream(inFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                FileStream pdfStream = new FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);

                PsDocument document = new PsDocument(psStream);
                PdfSaveOptions options = new PdfSaveOptions(true);
                PdfDevice device = new PdfDevice(pdfStream);

                try
                {
                    document.Save(device, options);
                }
                finally
                {
                    psStream.Close();
                    pdfStream.Close();
                }
            }));
        }
        public static void Run()
        {
            // ExStart:1
            string dataDir = RunExamples.GetDataDir_Data();

            using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
            {
                var options = new PdfRenderingOptions()
                {
                    PageSetup =
                    {
                        AnyPage = new Page(new Size(500, 500), new Margin(50, 50, 50, 50))
                    },
                    Encryption = new PdfEncryptionInfo("user", "p@wd", PdfPermissions.PrintDocument, PdfEncryptionAlgorithm.RC4_128)
                };
                using (PdfDevice device = new PdfDevice(options, dataDir + @"document_out.pdf"))
                {
                    document.RenderTo(device);
                }
            }
            // ExEnd:1
        }
        public void UsingDOMTest()
        {
            // Create an instance of an HTML document
            using (var document = new HTMLDocument())
            {
                // Create a style element and assign the green color for all elements with class-name equals 'gr'.
                var style = document.CreateElement("style");
                style.TextContent = ".gr { color: green }";

                // Find the document header element and append style element to the header
                var head = document.GetElementsByTagName("head").First();
                head.AppendChild(style);

                // Create a paragraph element with class-name 'gr'.
                var p = (HTMLParagraphElement)document.CreateElement("p");
                p.ClassName = "gr";

                // Create a text node
                var text = document.CreateTextNode("Hello World!!");

                // Append the text node to the paragraph
                p.AppendChild(text);

                // Append the paragraph to the document body element
                document.Body.AppendChild(p);

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "using-dom.html"));

                // Create an instance of the PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "using-dom.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(File.Exists(Path.Combine(OutputDir, "using-dom.pdf")));
            }
        }
        public void ConvertHTMLtoPDFTest()
        {
            // Prepare path to a source HTML file
            string documentPath = Path.Combine(DataDir, "spring.html");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "spring-output.pdf");

            // Initialize an HTML document from the file
            using var document = new HTMLDocument(documentPath);

            // Create an instance of the PdfRenderingOptions class
            var pdfOptions = new PdfRenderingOptions();

            // Create the PDF Device and specify the output file to render
            using var device = new PdfDevice(pdfOptions, savePath);

            // Render HTML to PDF
            document.RenderTo(device);


            Assert.True(File.Exists(Path.Combine(OutputDir, "spring-output.pdf")));
        }
Beispiel #17
0
        /// <summary>
        ///     Run the Job
        /// </summary>
        protected override JobState RunJobWork()
        {
            try
            {
                if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
                {
                    Thread.CurrentThread.Name = "JobWorker";
                }
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                _ghostScript.Output += Ghostscript_Output;
                OnJobCompleted      += (sender, args) => _ghostScript.Output -= Ghostscript_Output;

                OutputFiles.Clear();

                Logger.Trace("Setting up actions");
                SetUpActions();

                Logger.Debug("Starting Ghostscript Job");

                OutputDevice device;
                switch (Profile.OutputFormat)
                {
                case OutputFormat.PdfA1B:
                case OutputFormat.PdfA2B:
                case OutputFormat.PdfX:
                case OutputFormat.Pdf:
                    device = new PdfDevice(this);
                    break;

                case OutputFormat.Png:
                    device = new PngDevice(this);
                    break;

                case OutputFormat.Jpeg:
                    device = new JpegDevice(this);
                    break;

                case OutputFormat.Tif:
                    device = new TiffDevice(this);
                    break;

                case OutputFormat.Txt:
                    device = new TextDevice(this);
                    break;

                default:
                    throw new Exception("Illegal OutputFormat specified");
                }

                Logger.Trace("Output format is: {0}", Profile.OutputFormat.ToString());

                _ghostScript.Output += Ghostscript_Logging;
                var success = _ghostScript.Run(device, JobTempFolder);
                _ghostScript.Output -= Ghostscript_Logging;

                Logger.Trace("Finished Ghostscript execution");

                if (!success)
                {
                    var errorMessage = ExtractGhostscriptErrors(GhostscriptOutput);
                    Logger.Error("Ghostscript execution failed: " + errorMessage);
                    ErrorMessage = errorMessage;

                    JobState = JobState.Failed;
                    return(JobState);
                }

                ProcessOutput();

                Logger.Trace("Moving output files to final location");
                MoveOutputFiles();

                Logger.Trace("Finished Ghostscript Job");
                JobState = JobState.Succeeded;
                return(JobState);
            }
            catch (ProcessingException)
            {
                JobState = JobState.Failed;
                throw;
            }
            catch (DeviceException)
            {
                JobState = JobState.Failed;
                throw;
            }
            catch (Exception ex)
            {
                JobState = JobState.Failed;
                Logger.Error("There was an error while converting the Job {0}: {1}", JobInfo.InfFile, ex);
                throw;
            }
        }
Beispiel #18
0
        public virtual void Export(string sourcePath, string outPath, RenderingOptions options = null)
        {
            switch (dstFormat_)
            {
            case ExportFormat.PDF:
                PdfRenderingOptions pdf_opts = options as PdfRenderingOptions;
                if (pdf_opts == null)
                {
                    pdf_opts = new PdfRenderingOptions();
                }

                using (PdfDevice device = new PdfDevice(pdf_opts, outPath))
                {
                    Render(sourcePath, device);
                }
                break;

            case ExportFormat.XPS:
                XpsRenderingOptions xps_opts = options as XpsRenderingOptions
                ;
                if (xps_opts == null)
                {
                    xps_opts = new XpsRenderingOptions();
                }

                using (XpsDevice device = new XpsDevice(xps_opts, outPath))
                {
                    Render(sourcePath, device);
                }
                break;

            case ExportFormat.MD:
                IDevice nullDev = null;
                Render(sourcePath, nullDev);
                break;

            case ExportFormat.MHTML:
                throw new NotImplementedException("Conversion to 'MHTML' isn't implemented yet.");

            //break;
            case ExportFormat.JPEG:
            case ExportFormat.PNG:
            case ExportFormat.BMP:
            case ExportFormat.TIFF:
            case ExportFormat.GIF:
                ImageRenderingOptions img_opts = options as ImageRenderingOptions;
                if (img_opts == null)
                {
                    img_opts = new ImageRenderingOptions();
                }
                switch (dstFormat_)
                {
                case ExportFormat.JPEG:
                    img_opts.Format = ImageFormat.Jpeg; break;

                case ExportFormat.PNG:
                    img_opts.Format = ImageFormat.Png; break;

                case ExportFormat.BMP:
                    img_opts.Format = ImageFormat.Bmp; break;

                case ExportFormat.TIFF:
                    img_opts.Format = ImageFormat.Tiff; break;

                case ExportFormat.GIF:
                    img_opts.Format = ImageFormat.Gif; break;
                }
                using (ImageDevice device = new ImageDevice(img_opts, outPath))
                {
                    Render(sourcePath, device);
                }

                break;
            }
        }