Hi Waghuldey,
In order to display the Word document in iframe you need to save the word document as HTML file and display it in iframe.
Check this example. Now please take its reference and correct your code.
I have created the example using the below article.
HTML
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="Upload" />
<hr />
<asp:Literal ID="ltEmbed" runat="server" />
Namespaces
C#
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
VB.Net
Imports System.IO
Imports Microsoft.Office.Interop.Word
Code
C#
protected void Upload(object sender, EventArgs e)
{
string embed = "<iframe src=\"{0}\" width=\"300px\" height=\"300px\">";
string folderPath = Server.MapPath("~/Temp/");
//If Directory not present, create it.
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string extension = Path.GetExtension(fuUpload.FileName);
if (extension.ToLower() == ".doc" || extension.ToLower() == ".docx")
{
object documentFormat = 8;
object htmlFilePath = folderPath + "Test.htm";
string directoryPath = folderPath + "Test_files";
object fileSavePath = folderPath + Path.GetFileName(fuUpload.PostedFile.FileName);
//Upload the word document and save to Temp folder.
fuUpload.PostedFile.SaveAs(fileSavePath.ToString());
//Open the word document in background.
_Application applicationclass = new Application();
applicationclass.Documents.Open(ref fileSavePath);
applicationclass.Visible = false;
Document document = applicationclass.ActiveDocument;
//Save the word document as HTML file.
document.SaveAs(ref htmlFilePath, ref documentFormat);
//Close the word document.
document.Close();
//Read the saved Html File.
string wordHTML = File.ReadAllText(htmlFilePath.ToString());
//Loop and replace the Image Path.
foreach (Match match in Regex.Matches(wordHTML, "<v:imagedata.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase))
{
wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);
}
//Delete the Uploaded Word File.
File.Delete(fileSavePath.ToString());
ltEmbed.Text = string.Format(embed, ResolveUrl("~/Temp/" + "Test.htm"));
}
else
{
fuUpload.SaveAs(folderPath + Path.GetFileName(fuUpload.FileName));
ltEmbed.Text = string.Format(embed, ResolveUrl("~/Temp/" + Path.GetFileName(fuUpload.FileName)));
}
}
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim embed As String = "<iframe src=""{0}"" width=""300px"" height=""300px"">"
Dim folderPath As String = Server.MapPath("~/Temp/")
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Dim extension As String = Path.GetExtension(fuUpload.FileName)
If extension.ToLower() = ".doc" OrElse extension.ToLower() = ".docx" Then
Dim documentFormat As Object = 8
Dim htmlFilePath As Object = folderPath & "Test.htm"
Dim directoryPath As String = folderPath & "Test_files"
Dim fileSavePath As Object = folderPath & Path.GetFileName(fuUpload.PostedFile.FileName)
fuUpload.PostedFile.SaveAs(fileSavePath.ToString())
Dim applicationclass As _Application = New Application()
applicationclass.Documents.Open(fileSavePath)
applicationclass.Visible = False
Dim document As Document = applicationclass.ActiveDocument
document.SaveAs(htmlFilePath, documentFormat)
document.Close()
Dim wordHTML As String = File.ReadAllText(htmlFilePath.ToString())
For Each match As Match In Regex.Matches(wordHTML, "<v:imagedata.+?src=[""'](.+?)[""'].*?>", RegexOptions.IgnoreCase)
wordHTML = Regex.Replace(wordHTML, match.Groups(1).Value, "Temp/" & match.Groups(1).Value)
Next
File.Delete(fileSavePath.ToString())
ltEmbed.Text = String.Format(embed, ResolveUrl("~/Temp/" & "Test.htm"))
Else
fuUpload.SaveAs(folderPath & Path.GetFileName(fuUpload.FileName))
ltEmbed.Text = String.Format(embed, ResolveUrl("~/Temp/" & Path.GetFileName(fuUpload.FileName)))
End If
End Sub
Screenshot
Word, PDF and Text File
![](https://imgur.com/GLXy1MP.jpg)
Displaying Word, PDF and Text File in iframe
![](https://imgur.com/i6tXsFW.gif)