We're sorry Aspose doesn't work properply without JavaScript enabled.

# Best Practice to display a document on a web page

I’ve used the .NET version for years in a desktop application to create a Resume type system. I person would enter data into a database and when they are done they would click a button and see their document in a desktop viewer. It has worked well for years and is almost instant visually; however we like many others are moving to the web. My question is what is the best way to display the document in a browser. Currently I have the peraon download and then they open. Way to many steps. I would like to genterate the document in the same way in a .net web service but then display/stream it into a div. What would be the best and fastest way to do that. Convert to HTML? And Image? Any input is very helpful. Is there an example somewhere that produces a document and then displays it? Thanks

Thanks for your inquiry. In your case, we suggest you please save the document to HTML or HtmlFixed file format. You can send document to client browser. Please refer to the following article. Hope this helps you.
Sending to a Client Browser

Following code example shows how to send document to client browser.

MemoryStream stream = new MemoryStream(File.ReadAllBytes(@"c:\temp\input.docx"));
Document doc = new Document(stream);
HtmlSaveOptions options = new HtmlSaveOptions();
options.ExportImagesAsBase64 = true;
doc.Save(Response, "output.html", ContentDisposition.Inline, options);

Thanks for the information. Is there any way to maintain the pagination? So that if looks like the separate pages that the DOCX file would have?

I see the PAGE Field in the Custom Styles used for proper Aspose.Words-HTML-Aspose.Words Roundtrip but I’m not sure how to use it or if this will accomplish the separtate pages.

Thanks for your inquiry. You can insert page number in document’s body and header/footer. In this case, we suggest you please save the document to HtmlFixed file format. Please check the following code example. Hope this helps you.

MemoryStream stream = new MemoryStream(File.ReadAllBytes(@"c:\temp\in.docx"));
Document doc = new Document(stream);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Page : ");
builder.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false);
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
options.ExportEmbeddedImages = true;
options.ExportEmbeddedFonts = true;
options.ExportEmbeddedCss = true;
doc.Save(Response, "output.html", ContentDisposition.Inline, options);

Thank you for the information but that’s not what I’m looking for. Im looking for the background color to come between each of the pages so it does not look like one massive page. So it looks more like a pdf displayed page

Thanks for your inquiry. Please ZIP and attach your expected output document. We will then provide you more information about your query along with code.

Fantastic- In the zip file I’ve include a sample of the document that is created for final output. Also a picture of the page separation that is needed. This final document is normally emailed or downloaded for the end user. What I’m trying to give the end user is a Preview of that document so they do not need to download, open the document, see what’s wrong or forgotten and then do it over and over. This way there is a div on the web page that will be updated with a click and they can see what it is going to look like with the number of pages since the document is restricted to 5 pages. They do not need to edit the document on line but they select items from a database to include so whatever will be the fastest display option with a clean image will be the best solution. I have even been playing with building a image and displaying that. but if HTML code will work the best that will be great. Thanks for all of your help. biosketch-sample.zip (97.7 KB)

FYI- sorry for the confusion but I have two accounts and my name shows differently from the computer that I’m logged on

And here is a document that was created using ASPOSE WORDAspose_Created.zip (37.1 KB)

Thanks for your inquiry. Please note that Aspose.Words does not provide API to view or edit document in browser.

The HTML file format is not paginated. So, we suggest you please convert the document pages into images or save the document to HtmlFixed file format.

In your case, we suggest you please set the bottom border of document’s pages as shown below. Hope this helps you.

Document doc = new Document(stream);

PageSetup ps = doc.Sections[0].PageSetup;
ps.BorderAlwaysInFront = false;
ps.BorderDistanceFrom = PageBorderDistanceFrom.PageEdge;
ps.BorderAppliesTo = PageBorderAppliesTo.AllPages;

Border border = ps.Borders[BorderType.Bottom];
border.LineStyle = LineStyle.Single;
border.LineWidth = 30;
border.Color = System.Drawing.Color.Blue;
border.DistanceFromText = 0;

DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Page : ");
builder.InsertField(Aspose.Words.Fields.FieldType.FieldPage, false);
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
options.ExportEmbeddedImages = true;
options.ExportEmbeddedFonts = true;
options.ExportEmbeddedCss = true;
options.ShowPageBorder = false;

I hate to be a pest and I totally appreciate your help . I agree that the best solution is going to be converting to images and sending it to a browser. The page breaks between pages is totally needed. Do you happen to have any code that shows how to save as a image and send to a browser? Thank you.

I came up with this code and would love for you folks to check for efficiency

Dim doc As Aspose.Words.Document = New Aspose.Words.Document()

    Dim path As String = HttpContext.Current.Server.MapPath("")

doc = New Aspose.Words.Document(path + "\Biosketch2.doc")

Dim options As New Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png)
options.PageIndex = 0
options.Resolution = 125

Dim memstream As New MemoryStream
doc.Save(memstream, options)

context.Response.ContentType = "image/png"

Try
Dim MyImage As System.Drawing.Image = System.Drawing.Image.FromStream(memstream)
MyImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
MyImage.Dispose()

Catch ex As Exception
context.Response.Write("Error")
End Try


If saving it as a different image type will give better performance just let me know

Thanks for your inquiry. As per my understanding, you want to view the document in web browser and show the page break like MS Word pages. You can use code example shared in my previous post. You just need to modify the page’s bottom border according to your requirement.

The code example you are using converts the first page of document into image. Please use the following code example to convert each page of Word document to image.

Dim doc As Document = New Document((MyDir + "in.docx"))
Dim opts As ImageSaveOptions = New ImageSaveOptions(SaveFormat.Jpeg)
opts.PageIndex = 0
opts.PageCount = doc.PageCount
opts.PageSavingCallback = New HandlePageSavingCallback
doc.Save(MyDir + "18.3.jpg", opts)


Private Class HandlePageSavingCallback
Implements IPageSavingCallback

Public Sub PageSaving(ByVal args As PageSavingArgs)
args.PageFileName = String.Format(MyDir + "Page_{0}.png", args.PageIndex)
End Sub
End Class