0

Long story short: I've had to alter our current project to allow for PDF printing directly from the VB.Net application. I'm using PDFSharp to achieve this, and for the most part I've got it working fine.

However, for part of this it requires a basic type of richtext, which PDFSharp cannot handle. Instead I've written a small thing that basically converts it to HTML and then I can parse it a lot easier and just populate the PDF in that manner.

To do this, I created a separate project to test in, and the following process works:

  1. Create a richtextbox and a webbrowser
  2. Populate the richtextbox with the richtext
  3. Copy the richtext to the clipboard
  4. Paste the richtext into the WebBrowser documenttext
  5. Access the body html and use that

However, when I've tried to integrate this into the main project the WebBrowser object only occasionally takes the paste. The majority of the time it comes up blank. As far as I can tell, absolutely everything is identical between the two, except for the fact that in the one it's working everything is happening within a class, and in the one where it isn't it's all within a module. Is there something I should know that I don't regarding these?

Here are the relevant parts of the code:

Private RTF2 As New RichTextBox     'Rich text box to capture original format
Private Browse2 As New WebBrowser   'WebBrowser object
Private HTML2 As New RichTextBox    'Rich text box to put the HTML into so it can be processed
...
    'Called on the New() of the Module
    Browse2.DocumentText = ""
    Browse2.Document.ExecCommand("EditMode", False, Nothing)
...
    RTF2.Clear()
    RTF2.Rtf = Col1
    RTF2.SelectAll()
    RTF2.Copy()
    Clipboard.SetText(RTF2.Rtf, TextDataFormat.Rtf)

    Browse2.Navigate("about:blank")
    Browse2.Document.ExecCommand("Paste", False, Nothing)

After this point the Browse2.DocumentText() should be filled with the HTML, like it is in the smaller test project, but I get absolutely nothing.

Any advice would be greatly appreciated.

stolen_art
  • 23
  • 3
  • But... Why do you need to copy it? Can't you just set the DocumentText immediately? `Browse2.DocumentText = RTF2.Rtf`. – Visual Vincent Oct 28 '15 at 16:27
  • Unfortunately not. That just sets the document text to be the actual richtext in plain text, instead of actually formatting. Pasting it kind of forces the formatting options into the web browser so that the formatting switches from RTF to HTML – stolen_art Oct 28 '15 at 16:32
  • Might this help? [MSDN Forum](https://social.msdn.microsoft.com/Forums/vstudio/en-US/017d4eb8-34c8-4ee8-a7c9-2c3c8541ceb4/rtf-to-pdf-convertion-using-c?forum=csharpgeneral). Otherwise, checkout [iTextSharp](http://itextpdf.com/). Apparently that should be able to handle RTF. – Visual Vincent Oct 28 '15 at 16:48
  • Thanks for looking into it, I do appreciate it. Sadly iText isn't an option for us, as the firm won't purchase the license required, and they wouldn't allow for distribution of the software's code under the free licence. Regarding the Acrobat.dll, this won't handle what we're doing. Basically at the moment the whole document is constructed in code (via the PrintDocument object) and while this is fine and I've got it to handle everything we need it to, converting it to PDF isn't as straight forward, and the Acrobat.dll doesn't give the customisation options that we need for it. Thanks again – stolen_art Oct 28 '15 at 17:03
  • You said everything's in a module? Well modules seem to have problems when handling variables. I've read a lot of forum threads where people have tried to get/set variables in modules but they only return blank. You should move it to a class if possible. That should handle it properly. – Visual Vincent Oct 28 '15 at 17:11
  • I was a bit concerned that might be the case...I'll have to look at the knock-on impact of doing that across the whole project. Again, thanks for your help on this. – stolen_art Oct 28 '15 at 17:16
  • According to [this answer](http://stackoverflow.com/a/4529898/3740093) modules only exist for backward-compability (compability with older versions of VB). So it's best if you moved everything to a class instead, since modules seem to be deprecated. – Visual Vincent Oct 28 '15 at 17:18
  • You're welcome :)! If you don't want to do `ClassNameHere.MethodHere()` every time, you can put this in the top of your form code: `Imports ClassNameHere`. – Visual Vincent Oct 28 '15 at 17:24
  • Thanks for that, much appreciated again. Have converted module to a class, but unfortunately it's still not happy. The paste just isn't going in, so I'll have to dig a little deeper. Thanks for the tip on the Imports, makes things much easier! – stolen_art Oct 29 '15 at 09:24

0 Answers0