private static void Main(string[] args) { var htmlDocument = new HTMLDocument(); //htmlDocument.OnLoad += MakeImage; htmlDocument.OnReadyStateChange += MakePDF; htmlDocument.Navigate("http://jv.gilead.org.il/pg/22759-h/"); IsFinished.WaitOne(); htmlDocument.Dispose(); }
public void Merge(string[] htmlFiles, string outPath) { _htmlOutputPath = Path.GetDirectoryName(outPath); var temp = Path.Combine(_htmlOutputPath, "Output2"); Directory.CreateDirectory(temp); for (var i = 0; i < htmlFiles.Length; i++) { var htmlFile = htmlFiles[i]; var fileName = Path.GetFileName(htmlFile); var tempOutputFilePath = Path.Combine(temp, fileName); File.Copy(htmlFile, tempOutputFilePath); htmlFiles[i] = tempOutputFilePath; using (var htmlDocument = new HTMLDocument(htmlFiles[i])) { HTMLDivElement anchorDivContainer = (HTMLDivElement)htmlDocument.CreateElement("div"); HTMLBodyElement htmlDocumentBody = (HTMLBodyElement)htmlDocument.Body; HTMLHeadElement head = (HTMLHeadElement)htmlDocument.GetElementsByTagName("head")[0]; HTMLStyleElement htmlStyleElement = (HTMLStyleElement)htmlDocument.CreateElement("style"); var resetEvent = new AutoResetEvent(false); htmlStyleElement.OnLoad += (sender, e) => { resetEvent.Set(); }; htmlStyleElement.OnError += (sender, e) => { resetEvent.Set(); }; var cssContent = htmlDocument.CreateTextNode(_content); htmlStyleElement.AppendChild(cssContent); head.AppendChild(htmlStyleElement); resetEvent.WaitOne(); anchorDivContainer.SetAttribute("class", "container"); anchorDivContainer.SetAttribute("align", "center"); var paragraphElement = GetParagraphElement(htmlDocument, "Navigation"); anchorDivContainer.AppendChild(paragraphElement); if (i != 0) { var anchorBackward = GetAnchorElement(htmlDocument, "Backward"); anchorBackward.Href = Path.GetFileName(htmlFiles[i - 1]); anchorDivContainer.AppendChild(anchorBackward); } else { HTMLAnchorElement anchorBackward = GetAnchorElement(htmlDocument, "Backward"); anchorBackward.Href = Path.GetFileName(htmlFiles[htmlFiles.Length - 1]); anchorDivContainer.AppendChild(anchorBackward); } if (i != htmlFiles.Length - 1) { HTMLAnchorElement anchorForward = GetAnchorElement(htmlDocument, "Forward"); anchorForward.Href = Path.GetFileName(htmlFiles[i + 1]); anchorDivContainer.AppendChild(anchorForward); } else { HTMLAnchorElement anchorForward = GetAnchorElement(htmlDocument, "Forward"); anchorForward.Href = Path.GetFileName(htmlFiles[0]); anchorDivContainer.AppendChild(anchorForward); } Node firstChild = htmlDocumentBody.FirstChild; htmlDocumentBody.InsertBefore(anchorDivContainer, firstChild); var outputPath = Path.Combine(_htmlOutputPath, fileName); htmlDocument.Save(outputPath); htmlDocument.Dispose(); htmlFiles[i] = outputPath; ClearDirectory(temp); } } var name = Path.GetFileNameWithoutExtension(outPath); SaveDocument($"{name}.mhtml", htmlFiles[0], htmlFiles.Length); }