Thursday, December 1, 2022

[FIXED] How do I make an iframe show at 100%?

Issue

I am trying to follow along with this post to show a PDF in a WebView2. I have set my WebView2 to dockstill "Fill", so it will use the entire space of the form.

The author recommends:

Dim html As String = "<!DOCTYPE html><html><head></head><body><div>" &
        $"<iframe width=100% height=500 src=""data:Application/pdf;base64,{pdfBase64}"">" &
        "</iframe></div></body></html>"

Me.WebView2.NavigateToString(html)

This works.

The PDF is displayed over the width of the webview / form and with 500 pixels in height.

However, I would like the iframe to be the size of the parent. To do that, I change "500" to "100%" like this:

Dim html As String = "<!DOCTYPE html><html><head></head><body><div>" &
        $"<iframe width=100% height=100% src=""data:Application/pdf;base64,{pdfBase64}"">" &
        "</iframe></div></body></html>"

This has no effect. Instead, the pdf that is shown in the iframe gets a height of 152 pixels.

enter image description here

What am I doing wrong?


Solution

Probably just invalid or at least not well-formatted HTML string. I'd suggest using the XElement syntax to build the HTML output, it helps to write the tags and attributes correctly...

Dim pdfBytes = 'The PDF byte array...
Dim pdfBase64 = Convert.ToBase64String(pdfBytes)
Dim html =
    <html>
        <body>
            <iframe
                width="100%"
                height="100%"
                src=<%= $"data:application/pdf;base64,{pdfBase64}" %>>
            </iframe>
        </body>
    </html>.ToString()

WebView21.NavigateToString(html)

SO73708467

Nice VB.NET feature.



Answered By - dr.null
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.