0

In vb.net I need to print the contents showing in a browser control to printer.

I have used Gecko web-browser control in winform application and there is no direct way to print the page's contents.

Either way to print direct using InnerHtml or converting that html to pdf and then printing the pdf document.

currently I am using a third party library `ItextSharpe' but it gives errors.

public byte[] GetPDF(string pHTML) {
    byte[] bPDF = null;

    MemoryStream ms = new MemoryStream();
    TextReader txtReader = new StringReader(pHTML);

    // 1: create object of a itextsharp document class
    Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

    // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
    PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

    // 3: we create a worker parse the document
    HTMLWorker htmlWorker = new HTMLWorker(doc);

    // 4: we open document and start the worker on the document
    doc.Open();
    htmlWorker.StartDocument();

    // 5: parse the html into the document
    htmlWorker.Parse(txtReader);

    // 6: close the document and the worker
    htmlWorker.EndDocument();
    htmlWorker.Close();
    doc.Close();

    bPDF = ms.ToArray();

    return bPDF;
}
Byte[] bytes;
            bytes = GetPDF(browse.Document.Body.InnerHtml);
            var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
            System.IO.File.WriteAllBytes(testFile, bytes);

but throws errors while parsing.

Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.

enter image description here enter image description here

I have seen different examples over web but this is totally different, the examples or duplicate answer is about to export panels or grids but this is dynamic HTML and i need to convert it to PDF or print directly the client area.

Community
  • 1
  • 1
DareDevil
  • 5,249
  • 6
  • 50
  • 88
  • @Bruno Lowagie ; see the requirements, where is the duplicate, is that of similar , i have gone through those solutions. – DareDevil Aug 17 '16 at 10:55
  • You are using `HTMLWorker`. As explained in the duplicate question, there is no support whatsoever for `HTMLWorker`. It's obsolete and it has been abandoned a long time ago. You should not be surprised that "it gives errors." As explained in the answer I refer to, `HTMLWorker` isn't the tool if you want to convert *dynamic HTML* to PDF. – Bruno Lowagie Aug 17 '16 at 11:06
  • Go to Google, and type "HTMLWorker is". You will see that Google will complement this sentence and suggest: "HTMLWorker is obsolete". When you click those keywords, you'll find hundreds of pages where I explain over and over again that `HTMLWorker` was replaced by XML Worker. Don't you understand how frustrating it is having to say the same thing over and over and over again? – Bruno Lowagie Aug 17 '16 at 11:09
  • I got the code from this Link : http://www.rswebsols.com/tutorials/programming/asp-net-generate-pdf-html-itextsharp – DareDevil Aug 17 '16 at 11:39
  • @BrunoLowagie Before posting or typing question, there was no clue that HTMLWorker was obsolete, right, i found the code from the link mentioned above and put a question with all done efforts, you should handle things politely, it does not harm you to guide with good way, rather this way. – DareDevil Aug 17 '16 at 11:45
  • 1. I am the original author of iText. Rather than having developers curse that "something doesn't work", I want to inform them that some things don't work, e.g. `HTMLWorker` doesn't work. Many other aspects of iText are great, but `HTMLWorker` isn't. 2. I am the author of the [official documentation](http://itextpdf.com) and I don't understand why a developer would rather believe what is written on an unofficial web site than what is made clear on the official web site: http://developers.itextpdf.com/faq/category/parsing-xml-and-xhtml – Bruno Lowagie Aug 17 '16 at 11:50
  • 1
    People who do not read the official documentation are somewhat insulting because I spend many hours, days, weeks, months writing documentation that is made available for free on the **official web site** (not hidden somewhere on the internet). If you think I am not polite, think about how I feel about people who don't appreciate the documentation work I'm doing... – Bruno Lowagie Aug 17 '16 at 11:53
  • Ok Great, I will read this doc, and will implement accordingly, but i feel very upset when I expect some clue and being dealt a bit harshly, – DareDevil Aug 17 '16 at 12:08
  • You hurt easily. I didn't even mean to be harsh. When I'm harsh intentionally, it hurts much more. You should grow a thicker skin. – Bruno Lowagie Aug 17 '16 at 12:13
  • @DareDevil As you can see in the comments of that page, http://www.rswebsols.com/tutorials/programming/asp-net-generate-pdf-html-itextsharp#comment-16723, I commented on 22 March 2016 already that they use the obsolete `HTMLWorker`. So there really was a clue on a web page that you already had. – Amedee Van Gasse Aug 17 '16 at 15:17

0 Answers0