r/visualbasic • • Jul 20 '24

VB.NET Help Rendering Conversion of Excel file to PDF and Showing it into PDF Viewer

So I am doing a document printing kiosk using VB.net 8.0, and i am at the point where when you click a excel file it would be converted into a pdf file then it would load a form where it contain a pdf viewer where you can see a preview of you file and its page number, file name etc before you can go and print it, the problem is when i click the excel file it does not fit the PDF Viewer, but when i open the converted file as pdf directly it is showing as intended, i think may it is rendering issue?

4 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/Time-Lavishness95 Jul 20 '24

in using WebView2, is there a way to hide that tool box at the top?

4

u/jd31068 Jul 20 '24

There is this Hide or modify the toolbar of Webview2 when viewing pdf - Stack Overflow I'm trying to do it in my test, but I haven't been able to get it to work quite yet.

2

u/Time-Lavishness95 Jul 20 '24

thanks man! i will look into it

3

u/jd31068 Jul 20 '24

I was able to get it working by using this code:

    Private Sub wbv_CoreWebView2InitializationCompleted(sender As Object, e As Core.CoreWebView2InitializationCompletedEventArgs) Handles wbv.CoreWebView2InitializationCompleted

        If e.IsSuccess Then
            ' if the component initializes okay, set it up to hide the toolbar
            wbv.CoreWebView2.Settings.HiddenPdfToolbarItems =
            CoreWebView2PdfToolbarItems.Bookmarks Or
            CoreWebView2PdfToolbarItems.FitPage Or
            CoreWebView2PdfToolbarItems.PageLayout Or
            CoreWebView2PdfToolbarItems.PageSelector Or
            CoreWebView2PdfToolbarItems.Print Or
            CoreWebView2PdfToolbarItems.Rotate Or
            CoreWebView2PdfToolbarItems.Save Or
            CoreWebView2PdfToolbarItems.SaveAs Or
            CoreWebView2PdfToolbarItems.Search Or
            CoreWebView2PdfToolbarItems.ZoomIn Or
            Core.CoreWebView2PdfToolbarItems.ZoomOut

        End If
    End Sub

2

u/Time-Lavishness95 Jul 21 '24

can i set it to fit one page in web view?

4

u/jd31068 Jul 21 '24

I've actually never used it before; I just like to try to help here and there where I think I might be able to. That being said this WebView2 fit to page - Microsoft Q&A has an answer of

webView21.CoreWebView2.ExecuteScriptAsync("document.body.style.zoom = 'fit-to-width';")

Add that inside the if statement handling the hiding of the icons on the toolbar, I tested it, and it works. Anything you can do with a PDF in MS Edge is possible in the WebView2 control, including running JavaScript to modify the page being displayed and Edge's PDF capabilities are really good.

2

u/Time-Lavishness95 Jul 21 '24

thanks a lot man! you're a lifesaver!

3

u/jd31068 Jul 21 '24

You're welcome, happy to lend a hand. Good luck with the rest of your project.