protected override void TransformHtml2Pdf()
        {
            Document  doc       = new Document(PageSize.A4.Rotate());
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));

            doc.SetMargins(doc.LeftMargin - 10, doc.RightMargin - 10, doc.TopMargin, doc.BottomMargin);
            doc.Open();

            CssFilesImpl cssFiles = new CssFilesImpl();

            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + @"\tool\xml\examples\" + "sampleTest.css")));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext  hpc         =
                new HtmlPipelineContext(
                    new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));

            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            hpc.SetImageProvider(new SampleTest.SampleTestImageProvider());
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline    pipeline     = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker    worker       = new XMLWorker(pipeline, true);
            XMLParser    p            = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));

            p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
            doc.Close();
        }
Example #2
0
        protected override void MakePdf(string outPdf)
        {
            Document  doc       = new Document(PageSize.A4_LANDSCAPE);
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPath + outPdf, FileMode.Create));

            doc.SetMargins(0, 0, 0, 0);
            doc.Open();


            CssFilesImpl cssFiles = new CssFilesImpl();

            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + testName +
                                  Path.DirectorySeparatorChar + "sampleTest.css")));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext  hpc         =
                new HtmlPipelineContext(
                    new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));

            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            hpc.SetImageProvider(new SampleTestImageProvider());
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline    pipeline     = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker    worker       = new XMLWorker(pipeline, true);
            XMLParser    p            = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));

            p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
            doc.Close();
        }
Example #3
0
        private static bool GeneratePdf(string htmlText, string fileFullName, string watermarkText, PdfFont font)
        {
            if (string.IsNullOrEmpty(htmlText))
            {
                return(false);
            }

            htmlText = "<p>" + htmlText + "</p>";

            var document = new Document();
            var writer   = PdfWriter.GetInstance(document, new FileStream(fileFullName, FileMode.Create));

            if (!string.IsNullOrEmpty(watermarkText))
            {
                writer.PageEvent = new PdfWatermarkPageEvent(watermarkText);
            }

            document.Open();

            //pipeline
            var htmlContext = new HtmlPipelineContext(null);

            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            htmlContext.SetImageProvider(new ChannelImageProvider());

            htmlContext.SetCssAppliers(new CssAppliersImpl(GetFontProviderBy(font)));
            var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
            var pipeline    = new CssResolverPipeline(cssResolver,
                                                      new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

            //parse
            byte[] data    = Encoding.UTF8.GetBytes(htmlText);
            var    msInput = new MemoryStream(data);
            var    worker  = new XMLWorker(pipeline, true);
            var    parser  = new XMLParser(worker);

            parser.Parse(msInput); //XMLWorkerHelper.GetInstance().ParseXHtml(..)
            var pdfDest = new PdfDestination(PdfDestination.XYZ, 0, document.PageSize.Height, 1f);
            var action  = PdfAction.GotoLocalPage(1, pdfDest, writer);

            writer.SetOpenAction(action);

            //close
            document.Close();
            msInput.Close();

            return(true);
        }
Example #4
0
        private void processHtml(IElementHandler elementsHandler)
        {
            var cssResolver = new StyleAttrCSSResolver();

            if (CssFilesPath != null && CssFilesPath.Any())
            {
                foreach (var cssFile in CssFilesPath)
                {
                    cssResolver.AddCss(XmlWorkerUtils.GetCssFile(cssFile));
                }
            }

            if (!string.IsNullOrEmpty(InlineCss))
            {
                cssResolver.AddCss(InlineCss, "utf-8", true);
            }

            var htmlContext = new HtmlPipelineContext(new CssAppliersImpl(new UnicodeFontProvider(DefaultFont)));

            if (!string.IsNullOrEmpty(ImagesPath))
            {
                htmlContext.SetImageProvider(new ImageProvider {
                    ImagesPath = ImagesPath
                });
            }
            htmlContext.CharSet(Encoding.UTF8);

            var tagsProcessorFactory = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();

            if (PdfElement != null)
            {
                tagsProcessorFactory.AddProcessor("totalpagesnumber", new TotalPagesNumberXmlWorkerProcessor(PdfElement));
            }

            htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagsProcessorFactory);
            var pipeline = new CssResolverPipeline(cssResolver,
                                                   new HtmlPipeline(htmlContext, new ElementHandlerPipeline(elementsHandler, null)));
            var worker = new XMLWorker(pipeline, parseHtml: true);
            var parser = new XMLParser();

            parser.AddListener(worker);
            parser.Parse(new StringReader(Html));
        }
        virtual public void AddingAnImageRoot()
        {
            Document  doc    = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(doc,
                                                     new FileStream(TARGET + "columbus3.pdf", FileMode.Create));

            doc.Open();
            HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

            htmlContext.SetImageProvider(new CustomImageProvider()).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
            IPipeline    pipeline    = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext,
                                                                                             new PdfWriterPipeline(doc, writer)));
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser p      = new XMLParser(worker);

            p.Parse(File.OpenRead(RESOURCES + @"\examples\columbus.html"));
            doc.Close();
        }
Example #6
0
        private XMLWorker CreateXmlWorker(Document doc, PdfWriter writer)
        {
            var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);

            cssResolver.AddCss(content: css, isPersistent: true);

            var htmlContext = new HtmlPipelineContext(null);

            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            htmlContext.SetImageProvider(imageProvider);

            var pdfPipeline  = new PdfWriterPipeline(doc, writer);
            var htmlPipeline = new HtmlPipeline(htmlContext, pdfPipeline);
            var cssPipeline  = new CssResolverPipeline(cssResolver, htmlPipeline);

            var worker = new XMLWorker(cssPipeline, true);

            return(worker);
        }
        protected override void MakePdf(string outPdf)
        {
            Document doc = new Document(PageSize.A3.Rotate());

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));

            pdfWriter.CreateXmpMetadata();

            doc.SetMargins(200, 200, 0, 0);
            doc.Open();


            CssFilesImpl cssFiles = new CssFilesImpl();

            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
                                  Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
                                  "main.css")));
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
                                  Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
                                  "widget082.css")));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext  hpc         =
                new HtmlPipelineContext(
                    new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));

            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            hpc.SetImageProvider(new SampleTestImageProvider());
            hpc.SetPageSize(doc.PageSize);
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline    pipeline     = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker    worker       = new XMLWorker(pipeline, true);
            XMLParser    p            = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));

            p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
            //ICC_Profile icc = ICC_Profile.getInstance(ComplexDiv01Test.class.getResourceAsStream("sRGB Color Space Profile.icm"));
            //pdfWriter.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            doc.Close();
        }
Example #8
0
        private static void _add_to_table(Guid applicationId, HttpContext context, PdfPTable mainTable,
                                          StyleAttrCSSResolver cssResolver, WikiPDFText text, Font font, PdfPCell cell = null)
        {
            if (cell == null)
            {
                cell = new PdfPCell();
            }

            cell.Border = 0;
            cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
            cell.RunDirection        = text.IsRTL ? PdfWriter.RUN_DIRECTION_RTL : PdfWriter.RUN_DIRECTION_LTR;
            cell.SetLeading(0, 2);

            itsXmlHtml.DefaultTagProcessorFactory tagProcessors =
                (itsXmlHtml.DefaultTagProcessorFactory)itsXmlHtml.Tags.GetHtmlTagProcessorFactory();

            tagProcessors.RemoveProcessor(itsXmlHtml.HTML.Tag.IMG);                                          // remove the default processor
            tagProcessors.AddProcessor(itsXmlHtml.HTML.Tag.IMG, new CustomImageTagProcessor(applicationId)); // use our new processor

            //tagProcessors.AddProcessor(itsXmlHtml.HTML.Tag.TABLE, new TableTagProcessor());

            HtmlPipelineContext htmlContext =
                new HtmlPipelineContext(new itsXmlHtml.CssAppliersImpl(new IranSansFontProvider(font)));

            htmlContext.SetImageProvider(new ImageProvider());
            htmlContext.CharSet(Encoding.UTF8);
            htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);

            ElementsCollector   elementsHandler = new ElementsCollector();
            CssResolverPipeline pipeline        = new CssResolverPipeline(cssResolver,
                                                                          new HtmlPipeline(htmlContext, new ElementHandlerPipeline(elementsHandler, null)));

            itsXml.XMLWorker worker = new itsXml.XMLWorker(pipeline, parseHtml: true);
            XMLParser        parser = new XMLParser(true, worker, Encoding.UTF8);

            parser.Parse(new StringReader(text.Text));

            cell.AddElement(elementsHandler.Paragraph);
            mainTable.AddCell(cell);
        }
        private static void AddHTMLText(PdfWriter writer, Document document, string htmlString, List <string> stylesheetFilePath)
        {
            var cssResolver = new StyleAttrCSSResolver();

            Stream cssStream = null;

            if (stylesheetFilePath != null)
            {
                foreach (var stylesheetPath in stylesheetFilePath)
                {
                    cssStream = new FileStream(stylesheetPath, FileMode.Open, FileAccess.Read);
                    var cssFile = XMLWorkerHelper.GetCSS(cssStream);
                    cssResolver.AddCss(cssFile);
                }
            }

            var cssAppliersImpl     = new CssAppliersImpl();
            var htmlPipelineContext = new HtmlPipelineContext(cssAppliersImpl);

            htmlPipelineContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            htmlPipelineContext.SetImageProvider(new DownloadImageProvider());

            var pdfWriterPipeline   = new PdfWriterPipeline(document, writer);
            var htmlPipe            = new HtmlPipeline(htmlPipelineContext, pdfWriterPipeline);
            var cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipe);

            var worker       = new XMLWorker(cssResolverPipeline, true);
            var parser       = new XMLParser(worker);
            var stringReader = new StringReader(htmlString);

            parser.Parse(stringReader);

            if (cssStream != null)
            {
                cssStream.Dispose();
            }
        }
Example #10
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Invalid number of arguments.");
                Console.WriteLine("Usage: html2Pdf.exe [input html_file/directory] [default css file]");
                return;
            }

            List <FileStream> fileList = new List <FileStream>();

            if (File.Exists(args[0]))
            {
                fileList.Add(new FileStream(args[0], FileMode.Open));
            }
            else if (Directory.Exists(args[0]))
            {
                CollectHtmlFiles(fileList, args[0]);
            }

            if (fileList.Count == 0)
            {
                Console.WriteLine("Invalid html_file/directory");
                Console.WriteLine("Usage: html2Pdf.exe [input html_file/directory] [default css file]");
                return;
            }

            foreach (FileStream fileStream in fileList)
            {
                Document doc = new Document(PageSize.LETTER);
                doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0);
                String path = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar +
                              Path.GetFileNameWithoutExtension(fileStream.Name) + ".pdf";
                PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));

                doc.Open();
                Dictionary <String, String> substFonts = new Dictionary <String, String>();
                substFonts["Arial Unicode MS"] = "Helvetica";
                CssFilesImpl cssFiles = new CssFilesImpl();
                cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(args[1], FileMode.Open)));
                StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
                HtmlPipelineContext  hpc         = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts)));
                hpc.SetImageProvider(new ImageProvider(args[0]));
                hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
                IPipeline    pipeline     = new CssResolverPipeline(cssResolver, htmlPipeline);
                XMLWorker    worker       = new XMLWorker(pipeline, true);
                XMLParser    xmlParse     = new XMLParser(true, worker, null);
                xmlParse.Parse(fileStream);
                doc.Close();

                String cmpPath = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar +
                                 "cmp_" + Path.GetFileNameWithoutExtension(fileStream.Name) + ".pdf";
                if (File.Exists(cmpPath))
                {
                    CompareTool ct       = new CompareTool(path, cmpPath);
                    String      outImage = "<testName>-%03d.png".Replace("<testName>", Path.GetFileNameWithoutExtension(fileStream.Name));
                    String      cmpImage = "cmp_<testName>-%03d.png".Replace("<testName>", Path.GetFileNameWithoutExtension(fileStream.Name));
                    String      diffPath = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) +
                                           Path.DirectorySeparatorChar + "diff_" + Path.GetFileNameWithoutExtension(fileStream.Name) + ".png";
                    String errorMessage = ct.Compare(Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar + "compare" + Path.DirectorySeparatorChar, diffPath);
                    if (errorMessage != null)
                    {
                        Console.WriteLine(errorMessage);
                    }
                }
            }
        }
 virtual public void SetUp()
 {
     ctx = new HtmlPipelineContext(null);
     ctx.SetImageProvider(new CustomAbstractImageProvider());
     clone = (HtmlPipelineContext)ctx.Clone();
 }
Example #12
0
        public bool ConvertToPDF(string inputFile, string outputFile)
        {
            bool converted = false;

            try
            {
                HtmlDocument doc = new HtmlDocument();
                doc.OptionFixNestedTags   = true;
                doc.OptionWriteEmptyNodes = true;
                doc.OptionAutoCloseOnEnd  = true;

                doc.Load(inputFile);

                string rootInner = doc.DocumentNode.InnerHtml;

                if (!rootInner.Contains("<html"))
                {
                    doc.DocumentNode.InnerHtml = "<!DOCTYPE html>"
                                                 + "\r\n<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"
                                                 + "\r\n\t<head>"
                                                 + "\r\n\t\t<title>HTML to PDF</title>"
                                                 + "\r\n\t</head>"
                                                 + "\r\n\t<body>\r\n"
                                                 + rootInner
                                                 + "\r\n\t</body>"
                                                 + "\r\n</html>";
                }

                rootInner = doc.DocumentNode.InnerHtml;

                // Remove <meta> tag.
                string metaTag = @"<\s*(meta)(\s[^>]*)?>\s*";
                while (Regex.IsMatch(rootInner, metaTag))
                {
                    string metaMatch = Regex.Match(rootInner, metaTag).Value;
                    rootInner = rootInner.Replace(metaMatch, string.Empty);
                }
                rootInner = rootInner.Replace("</meta>", string.Empty);

                // Remove <form> tag.
                string formTag = @"<\s*(form)(\s[^>]*)?>\s*";
                while (Regex.IsMatch(rootInner, formTag))
                {
                    string formMatch = Regex.Match(rootInner, formTag).Value;
                    rootInner = rootInner.Replace(formMatch, string.Empty);
                }
                rootInner = rootInner.Replace("</form>", string.Empty);

                // Close br tag.
                string          brTag     = @"<\s*(br)(\s[^>]*)?>";
                MatchCollection brMatches = Regex.Matches(rootInner, brTag);
                if (brMatches != null)
                {
                    foreach (Match match in brMatches)
                    {
                        rootInner = rootInner.Replace(match.Value, "<br />");
                    }
                }

                // Replace <font> tag with div.
                string fontTag = @"<\s*(font)(\s[^>]*)?>";
                while (Regex.IsMatch(rootInner, fontTag))
                {
                    string fontMatch = Regex.Match(rootInner, fontTag).Value;
                    string toSpan    = fontMatch.Replace("font", "div");
                    rootInner = rootInner.Replace(fontMatch, toSpan);
                }
                rootInner = rootInner.Replace("</font>", "</div>");

                doc.DocumentNode.InnerHtml = rootInner;

                // Table elements
                var tableNodes = doc.DocumentNode.SelectNodes("//table");
                if (tableNodes != null)
                {
                    foreach (HtmlNode node in tableNodes)
                    {
                        bool isValidTable = false;
                        if (node.HasChildNodes)
                        {
                            if ((node.SelectSingleNode("thead/tr/th") != null) ||
                                (node.SelectSingleNode("tbody/tr/td") != null) ||
                                (node.SelectSingleNode("tr/td") != null))
                            {
                                isValidTable = true;
                            }
                        }

                        // Remove invalid table (no tr, td tags).
                        if (!isValidTable)
                        {
                            HtmlNode parent = node.ParentNode;
                            parent.InnerHtml = " ";
                        }

                        bool hasStyle = node.Attributes.Contains("style");
                        if (!hasStyle)
                        {
                            node.Attributes.Add("style", string.Empty);
                        }

                        StringBuilder styleValue = new StringBuilder(node.Attributes["style"].Value.Trim());
                        if (!string.IsNullOrEmpty(styleValue.ToString()))
                        {
                            if (!styleValue.ToString().EndsWith(";"))
                            {
                                styleValue.Append(";");
                            }
                        }

                        if (node.Attributes.Contains("cellspacing"))
                        {
                            if (!styleValue.ToString().Contains("border-collapse"))
                            {
                                styleValue.Append("border-collapse: collapse; ");
                            }
                            node.Attributes.Remove("cellspacing");
                        }

                        if (node.Attributes.Contains("cellpadding"))
                        {
                            if (!styleValue.ToString().Contains("cellpadding"))
                            {
                                styleValue.Append("padding: " + node.Attributes["cellpadding"].Value + "px; ");
                            }
                            node.Attributes.Remove("cellpadding");
                        }

                        if (node.Attributes.Contains("border"))
                        {
                            if (!styleValue.ToString().Contains("border"))
                            {
                                styleValue.Append("border: " + node.Attributes["border"].Value + "; ");
                            }
                            node.Attributes.Remove("border");
                        }

                        if (node.Attributes.Contains("width"))
                        {
                            string width = node.Attributes["width"].Value;
                            if (node.Attributes["width"].Value.EndsWith("%"))
                            {
                                width = node.Attributes["width"].Value;
                            }
                            else
                            {
                                if (node.Attributes["width"].Value.EndsWith("px"))
                                {
                                    width = node.Attributes["width"].Value;
                                }
                                else
                                {
                                    width = node.Attributes["width"].Value + "px";
                                }
                            }

                            styleValue.Append("width: " + width + "; ");
                            node.Attributes.Remove("width");
                        }

                        if (node.Attributes.Contains("height"))
                        {
                            string height = node.Attributes["height"].Value.EndsWith("px") ? node.Attributes["height"].Value : node.Attributes["height"].Value + "px; ";
                            styleValue.Append("height: " + height);
                            node.Attributes.Remove("height");
                        }

                        if (node.Attributes.Contains("align"))
                        {
                            styleValue.Append("text-align: " + node.Attributes["align"].Value + "; ");
                            node.Attributes.Remove("align");
                        }

                        node.Attributes["style"].Value = styleValue.ToString();
                    }
                }

                // Remove div from /div/img path.
                var imgNodes = doc.DocumentNode.SelectNodes("//div/img");
                while (imgNodes != null)
                {
                    foreach (HtmlNode node in imgNodes)
                    {
                        node.InnerHtml = " ";
                        HtmlNode td = node.ParentNode.ParentNode;
                        td.RemoveChild(node.ParentNode, true);
                    }
                    imgNodes = doc.DocumentNode.SelectNodes("//div/img");
                }

                // Remove div with class="Top_Hidden".
                var divHiddenNodes = doc.DocumentNode.SelectNodes("//div[@class='Top_Hidden']");
                while (divHiddenNodes != null)
                {
                    foreach (HtmlNode node in divHiddenNodes)
                    {
                        HtmlNode tatay = node.ParentNode;
                        tatay.RemoveChild(node, false);
                    }
                    divHiddenNodes = doc.DocumentNode.SelectNodes("//div[@class='Top_Hidden']");
                }

                // Remove children for div with class="blank".
                var divBlankNodes = doc.DocumentNode.SelectNodes("//div[@class='blank']");
                if (divBlankNodes != null)
                {
                    foreach (HtmlNode node in divBlankNodes)
                    {
                        node.RemoveAllChildren();
                    }
                }

                // Close img tag from /td/img path.
                var tdImgNodes = doc.DocumentNode.SelectNodes("//td/img");
                if (tdImgNodes != null)
                {
                    foreach (HtmlNode node in tdImgNodes)
                    {
                        node.InnerHtml = " ";
                    }
                }

                // Add style to div tag.
                var divNodes = doc.DocumentNode.SelectNodes("//div");
                if (divNodes != null)
                {
                    foreach (HtmlNode node in divNodes)
                    {
                        if (node.HasAttributes)
                        {
                            bool hasStyle = node.Attributes.Contains("style");
                            if (!hasStyle)
                            {
                                node.Attributes.Add("style", string.Empty);
                            }

                            StringBuilder styleValue = new StringBuilder(node.Attributes["style"].Value.Trim());
                            if (!string.IsNullOrEmpty(styleValue.ToString()) && !styleValue.ToString().EndsWith(";"))
                            {
                                styleValue.Append(";");
                            }

                            if (node.Attributes.Contains("face"))
                            {
                                string fontFamily = node.Attributes["face"].Value;
                                styleValue.Append("font-family: " + fontFamily.ToLower() + ";");
                                node.Attributes.Remove("face");
                            }

                            if (node.Attributes.Contains("size"))
                            {
                                string fontSize = node.Attributes["size"].Value;
                                string size     = "9pt";
                                switch (fontSize)
                                {
                                case "1":
                                {
                                    size = "7pt";
                                    break;
                                }

                                case "2":
                                {
                                    size = "9pt";
                                    break;
                                }

                                case "3":
                                {
                                    size = "10pt";
                                    break;
                                }

                                case "4":
                                {
                                    size = "12pt";
                                    break;
                                }

                                case "5":
                                {
                                    size = "16pt";
                                    break;
                                }

                                case "6":
                                {
                                    size = "20pt";
                                    break;
                                }

                                case "7":
                                {
                                    size = "30pt";
                                    break;
                                }

                                default:
                                    break;
                                }

                                styleValue.Append("font-size: " + size.ToLower() + ";");
                                node.Attributes.Remove("size");
                            }

                            node.Attributes["style"].Value = styleValue.ToString();
                        }
                    }
                }

                // Add td style.
                var tdNodes = doc.DocumentNode.SelectNodes("//td");
                if (tdNodes != null)
                {
                    foreach (HtmlNode node in tdNodes)
                    {
                        bool hasStyle = node.Attributes.Contains("style");
                        if (!hasStyle)
                        {
                            node.Attributes.Add("style", string.Empty);
                        }

                        StringBuilder styleValue = new StringBuilder(node.Attributes["style"].Value.Trim());
                        if (!string.IsNullOrEmpty(styleValue.ToString()))
                        {
                            if (!styleValue.ToString().EndsWith(";"))
                            {
                                styleValue.Append("; ");
                            }
                        }

                        if (node.Attributes.Contains("align"))
                        {
                            styleValue.Append("text-align: " + node.Attributes["align"].Value + "; ");
                            node.Attributes.Remove("align");
                        }
                        else
                        {
                            styleValue.Append("text-align: left;");
                        }

                        if (node.Attributes.Contains("valign"))
                        {
                            styleValue.Append("vertical-align: " + node.Attributes["valign"].Value + "; ");
                            node.Attributes.Remove("valign");
                        }

                        if (node.Attributes.Contains("width"))
                        {
                            string width = node.Attributes["width"].Value;
                            if (node.Attributes["width"].Value.EndsWith("%"))
                            {
                                width = node.Attributes["width"].Value;
                            }
                            else
                            {
                                if (node.Attributes["width"].Value.EndsWith("px"))
                                {
                                    width = node.Attributes["width"].Value;
                                }
                                else
                                {
                                    width = node.Attributes["width"].Value + "px";
                                }
                            }

                            styleValue.Append("width: " + width + "; ");
                            node.Attributes.Remove("width");
                        }

                        if (!string.IsNullOrEmpty(styleValue.ToString()))
                        {
                            node.Attributes["style"].Value = styleValue.ToString();
                        }
                    }
                }

                // Add style to p tag.
                var pNodes = doc.DocumentNode.SelectNodes("//p");
                if (pNodes != null)
                {
                    foreach (HtmlNode node in pNodes)
                    {
                        if (node.HasAttributes)
                        {
                            bool hasStyle = node.Attributes.Contains("style");
                            if (!hasStyle)
                            {
                                node.Attributes.Add("style", string.Empty);
                            }

                            StringBuilder styleValue = new StringBuilder(node.Attributes["style"].Value.Trim());
                            if (!string.IsNullOrEmpty(styleValue.ToString()) && !styleValue.ToString().EndsWith(";"))
                            {
                                styleValue.Append(";");
                            }

                            if (node.Attributes.Contains("align"))
                            {
                                string value = node.Attributes["align"].Value;
                                styleValue.Append("text-align: " + value.ToLower() + ";");
                                node.Attributes.Remove("align");
                            }

                            node.Attributes["style"].Value = styleValue.ToString();
                        }
                    }
                }

                // Remove u tag from //u/div path but put underline in div tag.
                var uDivNodes = doc.DocumentNode.SelectNodes("//u/div");
                while (uDivNodes != null)
                {
                    foreach (HtmlNode node in uDivNodes)
                    {
                        bool hasStyle = node.Attributes.Contains("style");
                        if (!hasStyle)
                        {
                            node.Attributes.Add("style", string.Empty);
                        }

                        StringBuilder styleValue = new StringBuilder(node.Attributes["style"].Value.Trim());
                        if (!string.IsNullOrEmpty(styleValue.ToString()) && !styleValue.ToString().EndsWith(";"))
                        {
                            styleValue.Append(";");
                        }

                        styleValue.Append("text-decoration: underline;");

                        node.Attributes["style"].Value = styleValue.ToString();

                        HtmlNode lolo = node.ParentNode.ParentNode;
                        lolo.RemoveChild(node.ParentNode, true);
                    }
                    uDivNodes = doc.DocumentNode.SelectNodes("//u/div");
                }

                // Remove strong tag from //strong/div path but put bold in div tag.
                var strongDivNodes = doc.DocumentNode.SelectNodes("//strong/div");
                while (strongDivNodes != null)
                {
                    foreach (HtmlNode node in strongDivNodes)
                    {
                        bool hasStyle = node.Attributes.Contains("style");
                        if (!hasStyle)
                        {
                            node.Attributes.Add("style", string.Empty);
                        }

                        StringBuilder styleValue = new StringBuilder(node.Attributes["style"].Value.Trim());
                        if (!string.IsNullOrEmpty(styleValue.ToString()) && !styleValue.ToString().EndsWith(";"))
                        {
                            styleValue.Append(";");
                        }

                        styleValue.Append("font-weight: bold;");

                        node.Attributes["style"].Value = styleValue.ToString();

                        HtmlNode lolo = node.ParentNode.ParentNode;
                        lolo.RemoveChild(node.ParentNode, true);
                    }
                    strongDivNodes = doc.DocumentNode.SelectNodes("//strong/div");
                }

                // Replace p tag with div from //p/div path.
                var pDivNodes = doc.DocumentNode.SelectNodes("//p/div");
                while (pDivNodes != null)
                {
                    foreach (HtmlNode node in pDivNodes)
                    {
                        node.ParentNode.Name = "div";
                    }
                    pDivNodes = doc.DocumentNode.SelectNodes("//p/div");
                }


                // Remove div tag from //ol/div path.
                var olDivNodes = doc.DocumentNode.SelectNodes("//ol/div");
                while (olDivNodes != null)
                {
                    foreach (HtmlNode node in olDivNodes)
                    {
                        HtmlNode tatay = node.ParentNode;

                        bool hasStyle = node.Attributes.Contains("style");
                        if (hasStyle)
                        {
                            tatay.Attributes.Add(node.Attributes["style"]);
                        }

                        tatay.RemoveChild(node, true);
                    }
                    olDivNodes = doc.DocumentNode.SelectNodes("//ol/div");
                }

                // Remove div tag from //ul/div path.
                var ulDivNodes = doc.DocumentNode.SelectNodes("//ul/div");
                while (ulDivNodes != null)
                {
                    foreach (HtmlNode node in ulDivNodes)
                    {
                        HtmlNode tatay = node.ParentNode;

                        bool hasStyle = node.Attributes.Contains("style");
                        if (hasStyle)
                        {
                            tatay.Attributes.Add(node.Attributes["style"]);
                        }

                        tatay.RemoveChild(node, true);
                    }
                    ulDivNodes = doc.DocumentNode.SelectNodes("//ul/div");
                }

                // Remove div tag from //li/div path.
                var liDivNodes = doc.DocumentNode.SelectNodes("//li/div");
                while (liDivNodes != null)
                {
                    foreach (HtmlNode node in liDivNodes)
                    {
                        HtmlNode tatay = node.ParentNode;

                        bool hasStyle = node.Attributes.Contains("style");
                        if (hasStyle)
                        {
                            tatay.Attributes.Add(node.Attributes["style"]);
                        }

                        tatay.RemoveChild(node, true);
                    }
                    liDivNodes = doc.DocumentNode.SelectNodes("//li/div");
                }

                // Save the modified html to a new name.
                string formattedHTMLFile = outputFile.Replace(".pdf", string.Empty) + "_TEMP.html";
                doc.Save(formattedHTMLFile);

                HtmlDocument docCPM = new HtmlDocument();
                docCPM.OptionFixNestedTags   = true;
                docCPM.OptionWriteEmptyNodes = true;
                docCPM.OptionAutoCloseOnEnd  = true;

                string newHTML = outputFile.Replace(".pdf", string.Empty) + "_PDF.html";
                docCPM.Load(formattedHTMLFile);
                docCPM.Save(newHTML);

                FontFactory.RegisterDirectories();
                Document  document = new Document();
                PdfWriter writer   = PdfWriter.GetInstance(document, new FileStream(outputFile, FileMode.Create));
                try
                {
                    document.Open();

                    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
                    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                    CustomImageProvider imageProvider = new CustomImageProvider();
                    htmlContext.SetImageProvider(imageProvider);

                    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);

                    IPipeline pipeline =
                        new CssResolverPipeline(cssResolver,
                                                new HtmlPipeline(htmlContext,
                                                                 new PdfWriterPipeline(document, writer)));

                    XMLWorker worker = new XMLWorker(pipeline, true);
                    XMLParser p      = new XMLParser(worker);
                    p.Parse(new StreamReader(newHTML));
                    //XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StreamReader(newHTMLFile));

                    converted = true;
                }
                catch
                {
                    converted = false;
                    throw;
                }
                finally
                {
                    if (document.IsOpen())
                    {
                        document.Close();
                    }

                    if (writer != null)
                    {
                        writer.Close();
                    }

                    File.Delete(formattedHTMLFile);
                    File.Delete(newHTML);
                }
            }
            catch
            {
                converted = false;
                throw;
            }
            finally
            {
            }

            return(converted);
        }