public FileItemDTO ConvertFileToPDF(FileItemEntity fileItem)
        {
            fileItem.FileFullPath = FileUtils.GetDefaultInputPath() + fileItem.FileName;

            File.WriteAllBytes(fileItem.FileFullPath, fileItem.Bytes);

            var fileItemDTO = new FileItemDTO()
            {
                Id           = fileItem.Id,
                FileFullPath = FileUtils.GetNewFileName(fileItem.FileName, FileNameOptionEnum.Convert)
            };

            fileItemDTO.FileName = FileUtils.GetSafeFileName(fileItemDTO.FileFullPath);

            using (PDFDoc doc = new PDFDoc())
            {
                try
                {
                    switch (FileUtils.GetFileType(fileItem.FileFullPath))
                    {
                    case FileType.MSOffice:
                        pdftron.PDF.Convert.OfficeToPDF(doc, fileItem.FileFullPath, null);
                        break;

                    case FileType.HTML:
                        HTML2PDF converter = new HTML2PDF();

                        var htmlString = File.ReadAllText(fileItem.FileFullPath);

                        converter.InsertFromHtmlString(htmlString);

                        converter.Convert(doc);
                        break;

                    case FileType.Other:
                        pdftron.PDF.Convert.ToPdf(doc, fileItem.FileFullPath);
                        break;

                    case FileType.NotMapped:
                        throw new ArgumentException($"Extensão {FileUtils.GetFileExtension(fileItem.FileFullPath)} não mapeada");

                    default:
                        break;
                    }

                    doc.Save(fileItemDTO.FileFullPath, SDFDoc.SaveOptions.e_linearized);

                    return(fileItemDTO);
                }
                catch (PDFNetException ex)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public PdfTronForm()
        {
            InitializeComponent();

            PDFNetLoader loader = PDFNetLoader.Instance();

            PDFNet.SetTempPath("C:\\ProgramData\\ActivePDF\\DocConverter\\Watch Folders\\Default\\Temp");
            PDFNet.Initialize();
            HTML2PDF.SetModulePath(Environment.CurrentDirectory + "\\html2pdf.dll");
        }
예제 #3
0
        // POST: api/PDF
        //public void Post([FromBody]string value)
        //{

        //}

        // POST: api/PDF

        public string Post([FromBody] PDFRequest objPDFRequest)
        {
            if (objPDFRequest == null)
            {
                return("Web API Request Exception:- Bad Request");
            }

            string strResult = null;

            try
            {
                if (!objPDFRequest.ValidRequest())
                {
                    return("Web API Request Exception:- Bad Request(Invalid)");
                }

                HTML2PDF obj        = new HTML2PDF();
                string   strHTMLURL = objPDFRequest.HTMLURL;
                string   fileName   = obj.CreateHTML2PDF(strHTMLURL, objPDFRequest.HTMLContents, objPDFRequest.PageSize);

                if (!string.IsNullOrEmpty(fileName))
                {
                    string file = fileName;
                    if (File.Exists(file))
                    {
                        strResult = fileName;

                        var openFile = File.OpenRead(file);
                        // copy the stream (thanks to http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c)
                        byte[] buffer = new byte[32768];
                        while (true)
                        {
                            int read = openFile.Read(buffer, 0, buffer.Length);
                            if (read <= 0)
                            {
                                break;
                            }
                            //Response.OutputStream.Write(buffer, 0, read);
                        }
                        openFile.Close();
                        openFile.Dispose();

                        // File.Delete(file);
                    }
                }

                return(strResult);
            }
            catch (Exception ex)
            {
                return("Web API Request Exception:- " + ex.Message);
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            string output_path = "../../TestFiles/Output/html2pdf_example";
            string host        = "http://www.gutenberg.org/";
            string page0       = "wiki/Main_Page";
            string page1       = "catalog/";
            string page2       = "browse/recent/last1";
            string page3       = "wiki/Gutenberg:The_Sheet_Music_Project";

            // The first step in every application using PDFNet is to initialize the
            // library and set the path to common PDF resources. The library is usually
            // initialized only once, but calling Initialize() multiple times is also fine.
            PDFNet.Initialize();

            // For HTML2PDF we need to locate the html2pdf module. If placed with the
            // PDFNet library, or in the current working directory, it will be loaded
            // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
            HTML2PDF.SetModulePath("../../../../../Lib");

            //--------------------------------------------------------------------------------
            // Example 1) Simple conversion of a web page to a PDF doc.

            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    // now convert a web page, sending generated PDF pages to doc
                    if (HTML2PDF.Convert(doc, host + page0))
                    {
                        doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized);
                    }
                    else
                    {
                        Console.WriteLine("Conversion failed.");
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            //--------------------------------------------------------------------------------
            // Example 2) Modify the settings of the generated PDF pages and attach to an
            // existing PDF document.

            try
            {
                // open the existing PDF, and initialize the security handler
                using (PDFDoc doc = new PDFDoc("../../TestFiles/numbered.pdf"))
                {
                    doc.InitSecurityHandler();

                    // create the HTML2PDF converter object and modify the output of the PDF pages
                    HTML2PDF converter = new HTML2PDF();
                    converter.SetImageQuality(25);
                    converter.SetPaperSize(PrinterMode.PaperSize.e_11x17);

                    // insert the web page to convert
                    converter.InsertFromURL(host + page0);

                    // convert the web page, appending generated PDF pages to doc
                    if (converter.Convert(doc))
                    {
                        doc.Save(output_path + "_02.pdf", SDFDoc.SaveOptions.e_linearized);
                    }
                    else
                    {
                        Console.WriteLine("Conversion failed. HTTP Code: {0}\n{1}", converter.GetHTTPErrorCode(), converter.GetLog());
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            //--------------------------------------------------------------------------------
            // Example 3) Convert multiple web pages, adding a table of contents, and setting
            // the first page as a cover page, not to be included with the table of contents outline.

            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    HTML2PDF converter = new HTML2PDF();

                    // Add a cover page, which is excluded from the outline, and ignore any errors
                    HTML2PDF.WebPageSettings cover = new HTML2PDF.WebPageSettings();
                    cover.SetLoadErrorHandling(HTML2PDF.WebPageSettings.ErrorHandling.e_ignore);
                    cover.SetIncludeInOutline(false);
                    converter.InsertFromURL(host + page3, cover);

                    // Add a table of contents settings (modifying the settings is optional)
                    HTML2PDF.TOCSettings toc = new HTML2PDF.TOCSettings();
                    toc.SetDottedLines(false);
                    converter.InsertTOC(toc);

                    // Now add the rest of the web pages, disabling external links and
                    // skipping any web pages that fail to load.
                    //
                    // Note that the order of insertion matters, so these will appear
                    // after the cover and table of contents, in the order below.
                    HTML2PDF.WebPageSettings settings = new HTML2PDF.WebPageSettings();
                    settings.SetLoadErrorHandling(HTML2PDF.WebPageSettings.ErrorHandling.e_skip);
                    settings.SetExternalLinks(false);
                    converter.InsertFromURL(host + page0, settings);
                    converter.InsertFromURL(host + page1, settings);
                    converter.InsertFromURL(host + page2, settings);

                    if (converter.Convert(doc))
                    {
                        doc.Save(output_path + "_03.pdf", SDFDoc.SaveOptions.e_linearized);
                    }
                    else
                    {
                        Console.WriteLine("Conversion failed. HTTP Code: {0}\n{1}", converter.GetHTTPErrorCode(), converter.GetLog());
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }

            //--------------------------------------------------------------------------------
            // Example 4) Convert HTML string to PDF.

            try
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    HTML2PDF converter = new HTML2PDF();

                    // Our HTML data
                    string html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";

                    // Add html data
                    converter.InsertFromHtmlString(html);
                    // Note, InsertFromHtmlString can be mixed with the other Insert methods.

                    if (converter.Convert(doc))
                    {
                        doc.Save(output_path + "_04.pdf", SDFDoc.SaveOptions.e_linearized);
                    }
                    else
                    {
                        Console.WriteLine("Conversion failed. HTTP Code: {0}\n{1}", converter.GetHTTPErrorCode(), converter.GetLog());
                    }
                }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private void btnConvertToPdf_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbConvertPdf.Text))
            {
                ofdAbrirArquivo.Multiselect = false;
                ofdAbrirArquivo.Filter      = string.Empty;

                if (ofdAbrirArquivo.ShowDialog() == DialogResult.OK)
                {
                    using (PDFDoc doc = new PDFDoc())
                    {
                        try
                        {
                            switch (GetFileType(ofdAbrirArquivo.FileName))
                            {
                            case FileType.MSOffice:
                                pdftron.PDF.Convert.OfficeToPDF(doc, ofdAbrirArquivo.FileName, null);
                                break;

                            case FileType.HTML:
                                HTML2PDF converter = new HTML2PDF();

                                var htmlString = System.IO.File.ReadAllText(ofdAbrirArquivo.FileName);

                                converter.InsertFromHtmlString(htmlString);

                                converter.Convert(doc);
                                break;

                            case FileType.Other:
                                pdftron.PDF.Convert.ToPdf(doc, ofdAbrirArquivo.FileName);
                                break;

                            case FileType.NotMapped:
                                MessageBox.Show($"Extensão {GetFileExtension(ofdAbrirArquivo.FileName)} não mapeada");
                                return;

                            default:
                                break;
                            }

                            doc.Save(GetNewFileName(ofdAbrirArquivo.FileName, FileNameOptionEnum.Convert), SDFDoc.SaveOptions.e_linearized);
                        }
                        catch (PDFNetException ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }
            else
            {
                using (PDFDoc doc = new PDFDoc())
                {
                    //HTML2PDF.Convert(doc, tbConvertPdf.Text);

                    HTML2PDF converter = new HTML2PDF();

                    converter.InsertFromURL(tbConvertPdf.Text);

                    converter.Convert(doc);

                    MessageBox.Show(converter.GetLog());

                    doc.Save(GetNewFileName("teste_convert-from-html", FileNameOptionEnum.Convert), SDFDoc.SaveOptions.e_linearized);
                }
            }
        }