I have a Webview2 Control in my application, used to view PDF documents.
The application also stores to and reads from MS SQL server PDFs data.
Currently, I am retrieving binary data from SQL, convert them to a temporary file to disk and set:
webview2.source = New Uri("file://" + filename)
That's working fine so far, but of course I would like to do the job without writing and reading to and from disk.
Is there a way to do the same without accessing the disk?
Update (as recommended), what I tried. With part of code for better understandg:
Dim fieldOrdinal = reader.GetOrdinal(ColumnName)
reader.Read()
Dim blob = New Byte(reader.GetBytes(fieldOrdinal, 0, Nothing, 0, 0) - 1) {}
reader.GetBytes(fieldOrdinal, 0, blob, 0, blob.Length)
Dim pdfBase64 As String = Convert.ToBase64String(blob)
Dim html As String = "<!DOCTYPE html><html><head></head><body><div>" & $"<iframe width=100% height=500 src=\" & Chr(&H22) & "data:Application/pdf;base64,{pdfBase64}\" & Chr(&H22) & ">" & "</iframe></div></body></html>"
The webview2
control shows a frame, but without content
Final Update: Here the (correct) to VB translated and working code:
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>"
You can embed the PDF in an IFRAME
as Base64, the same way you embed images.
In this case, the mime type is of course application/pdf
.
Convert to Base64 your source PDF bytes with Convert.ToBase64String(), use a standard HTML5 header and set the IFRAME
and/or DIV
container to the desired size.
Use the WebView2.NavigateToString() method to load the HTML with the embedded PDF:
2097152
bytes (2MB)string pdfBase64 = Convert.ToBase64String([Your PDF bytes]);
string html = "<!DOCTYPE html><html><head></head><body><div>" +
$"<iframe width=100% height=500 src=\"data:application/pdf;base64,{pdfBase64}\">" +
"</iframe></div></body></html>";
webView2.NavigateToString(html);
VB.Net version:
Dim pdfBytes = [DataReader].GetSqlBytes([DataReader].GetOrdinal("[Column Name]")).Value
Dim pdfBase64 As String = Convert.ToBase64String(pdfBytes)
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>"
webView2.NavigateToString(html)
The iframe
size should not be set like that, you should use CSS styles (and probably a simple JavaScript to handle late redefinition of the size of the Window that contains your PDF).
You could build a simple HTML template and fill the Base64 content as described.