Embed word document into ASP.net page and access using VBscript - asp.net

I have some code that opens a word document using VBScript on an ASP.net page:
set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "c:\inetpub\wwwroot\JSWordTest\test.doc", False, False, False
This works great but opens the word doc in another window. Ideally I would like to make this look as if it is contained in the current page perhaps in an IFrame. I have some other buttons which paste text into the word document when clicked.
I cannot just set the src of the iframe to the word document as need a reference to the word document (objWord) to allow me to paste text into it in real time again using Vbscript to do this.
Not sure if this is possible but any ideas/alternatives welcome?
Requirements:
The word doc needs to be displayed from web browser
At the side of the word document will be some buttons which when clicked paste text into it

You can use this technique to get the contents of the Word document without displaying any windows at all.
' Declare an object for the word application '
Set objWord = CreateObject("Word.Application")
objWord.Visible = False ' Don''t show word '
objWord.Documents.open("C:\test.doc") ' Open document '
objWord.Selection.WholeStory ' Select everything in the doc '
strText = objWord.Selection.Text ' Assign document contents to var'
objWord.Quit False ' Close Word, don't save '
Once you've got the contents of the document in the variable you can do what you want with it as far as writing it out with a document.write or whatever method you want to use.
You can find more detail on the MS Word application object and its methods here: http://msdn.microsoft.com/en-us/library/aa221371(office.11).aspx

If it is an option to install an ActiveX component at the client machines, you can try EDraw Office Viewer component or the cheapter Ultra Office Control. Both are based on the DSOFramer example by Microsoft and provider similar methods to interface with the documents.
Sample code is given and shows how to trigger dialogs, insert text, etc.

You can get inspirations from Excel Viewer component. It is like EDraw Office Viewer but free and open source. Currently, it opens office documents only but you can easily change it to work with Word.

You could try saving to HTML format
Const wdFormatHTML = 8
dim doc
set doc = objWord.Documents.open("C:\test.doc")
doc.SaveAs "doc.htm", wdFormatHTML
' etc ...
and then use that as the source of your iframe document. Bear in mind that when saving to HTML format, Word creates a corresponding resources folder (for images etc), so you might need to take that into account.

Related

Add header/footer in exported word file from ASP.NET

I have an "export to word" feature in my application. It works perfectly. I use gridview's content exported into word file.
Now I want to add header/footer in exported word file which is generated from the below code:
Dim fileName As String = "Test_" & Format(DateTime.Now, "MMddyyyyhhmmss") & ".doc"
Dim sw As New StringWriter()
Dim w As New HtmlTextWriter(sw)
gvContent.RenderControl(w)
Dim content As String = sw.GetStringBuilder().ToString()
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word"
Response.Write(finalContent)
Response.Flush()
Response.End()
Also the header & footer should be displayed in all pages of the word file, yust as when using Word's header/footer feature.
Is it possible? Does anyone have an idea for this?
What you're doing is actually creating an HTML file and giving it an extension that Word knows to open. You aren't creating a real .DOC file, but Word will recognize the HTML in and and display it.
I suspect that the flavor of HTML it's looking for is identical to the flavor it saves in. So I created a new document in Word 2013, added headers and footers, and saved it as an HTML file. After inspecting the HTML file, it appears that Word leaves those out. So I doubt that there is a way of specifying headers and footers in HTML files that it opens.
What you can do is switch to generating real MS Word files. These will have better support across various Word clients and Word-equivalents (such as Mac versions, mobile versions, and Libre Office).
Micrsoft provides a library for generating .DOCX files, called Open XML SDK. However, I've found that a bit hard to use.
I personally have used DocX a few times. Here's how you'd accomplish this with that library (code taken from this blog post):
C#
// Create a new document.
using (DocX document = DocX.Create(#"Test.docx"))
{
// Add Header and Footer support to this document.
document.AddHeaders();
document.AddFooters();
// Get the default Header for this document.
Header header_default = document.Headers.odd;
// Get the default Footer for this document.
Footer footer_default = document.Footers.odd;
// Insert a Paragraph into the default Header.
Paragraph p1 = header_default.InsertParagraph();
p1.Append("Hello Header.").Bold();
// Insert a Paragraph into the document.
Paragraph p2 = document.InsertParagraph();
p2.AppendLine("Hello Document.").Bold();
// Insert a Paragraph into the default Footer.
Paragraph p3 = footer_default.InsertParagraph();
p3.Append("Hello Footer.").Bold();
// Save all changes to this document.
document.Save();
}// Release this document from memory.
VB.NET (as translated by Telerik because I don't know VB.NET)
' Create a new document.
Using document As DocX = DocX.Create("Test.docx")
' Add Header and Footer support to this document.
document.AddHeaders()
document.AddFooters()
' Get the default Header for this document.
Dim header_default As Header = document.Headers.odd
' Get the default Footer for this document.
Dim footer_default As Footer = document.Footers.odd
' Insert a Paragraph into the default Header.
Dim p1 As Paragraph = header_default.InsertParagraph()
p1.Append("Hello Header.").Bold()
' Insert a Paragraph into the document.
Dim p2 As Paragraph = document.InsertParagraph()
p2.AppendLine("Hello Document.").Bold()
' Insert a Paragraph into the default Footer.
Dim p3 As Paragraph = footer_default.InsertParagraph()
p3.Append("Hello Footer.").Bold()
' Save all changes to this document.
document.Save()
End Using
' Release this document from memory.
Note that the above code was taken from a blog post written in 2010. The library is likely to have changed in the intervening six years.

Providing PDF specific parameters in an ASP.NET Generic Handler writing a PDF BLOB stream

EDIT: modified Title to be more specific
I've created a generic handler in VS2012 using their basic template as a starting point and modified it to grab a pdf from our sqlserver. The primary code block is this:
buffer = DirectCast(rsp.ScalarValue, Byte())
context.Response.ContentType = "application/pdf"
context.Response.OutputStream.Write(buffer, 0, buffer.Length)
context.Response.Flush()
And this works fine to display the BLOB as a pdf using whichever pdf plugin is installed on any given browser.
My Question: How can I modify the handler to write Adobe PDF specific parameters to the output? Specifically I'm trying to set width='fit' such that the output PDF stream will autofit the document to the width of the popup window.
NB: Writing the BLOB to a pdf file and serving the PDF is not an option.
Thanks in advance for any advice or links
I don't think there's anything that you can do in your handler. According to that document PDF viewers can examine the URL that was used to open the PDF but there are no HTTP headers that you can set. So you'll need to modify the thing that links to your handler to have those parameters in place. Alternatively, you could build a pre-handler that HTTP redirects to your new handler with those parameters in place.
Also, that document was written in 2007 and was intended for Adobe Acrobat and Adobe Reader. Most modern browsers ship with their own internal PDF viewer these days so unless you are only targeting Adobe your efforts might be wasted.

DocumentMap/Table of Contents for DocumentViewer, and calling a hyperlink by name or link number for XPS documents

I have XPS files with header bookmarks. If you open the source document in Word and go to view->Document Map, you see all of the bookmarks on the left. Is it possible to get this same functionality in DocumentViewer, like you would get with a PDF document in some sort of PDF reader?
Also, the RequestNavigateEventHandler shows that each hyperlink/bookmark in an XPS document has a specific Uri which is something like "C:\my path\to\file.xps#PG_N_LNK_X" where X is a unique number for the link and N is the page number. I would like to figure out a way to call a bookmark by its heading. For example, if I had a section called "Main Screen" which was on page 8 of the XPS File, the Uri for that bookmark would end something like #PG_8_LNK_3. Is it possible for me to get that Uri from the Bookmark Heading?
For those wondering, XPS documents are simply ZIP files. Extract the zip and parse the XML file \Documents\1\DocStructure.Struct for the outline entries.
Take a look at:
How to open a XPS in a specified bookmarks
The method "GetBookmarks" in the last answer extracts the bookmarks from the XPS-File. The method "GotoBookmarkElement" navigates to the bookmark.
A faster way to navigate to the bookmark could be done by setting the Frame.Source property:
DocFrame.Source = new Uri(string.Format("pack://file:,,,{0}/FixedDocSeq.fdseq#{1}", _filePath.Replace('\\', ','), bookmark.Name));
The input string for the Uri-Constructor look´s like:
"pack://file:,,,C:,temp,Help,Manual.xps/FixedDocSeq.fdseq#PG_6_LNK_380"
for a file which is located in:
"C:\\temp\\Help\\Manual.xps"

Download the html text collected in a string builder to word document in VB.NET Console Application

I have a requirement to move the html text available in a string builder to a word document and open the word document after the data is appended in a VB.NET console application. I am new to console applications and am not sure how this could be done, but I am aware that if I am using a Web Application then I can use the following code:
Response.AppendHeader("Content-Type", "application/msword")
Response.AppendHeader("Content-disposition", "attachment; filename=myword.doc")
Response.Write(String Builder Variable)
Can some help me with the code please....???
Write it to a '.doc' file and use ShellExecute to call 'open' on it.

Converting Plain Text to Clickable link or Link to PlainText in asp.net

I need your advice with converting plain text to an URL.
The scenario will be this: The user will select some entry and then click a "convert to link" button.
The entry text the user selected will convert to (link: selected_text). I do it with JavaScript. And after that, when he clicks the Save button to save all his entry, I don't know how to store (link: selected_text) in tha database.
The URL will be like this: www.mysite.aspx?t=selected_text.
I can convert (link: selected_text) by using replace function in code-behind. But then I don't know how to show user as clickable and also by not showing <a href="www.mysite.aspx?t=selected_text">
It can be difficult to understand therefore I will show some of my codes to explain.
Private Sub Save(ByVal Entry As String) ' Entry Comes from entry textbox '
Dim elected As String
selected = Entry.Replace("(link: ", "<a href http://www.mysite.com?link=")
selected = Entry.Replace(")", ">")
' then here starts save but not necessary to show '
End Sub
If you must save processed input for some reason
(link: here)
must be converted to
(link: here)
To store in database, you'll have to track the changes separately somehow and post them back to the server. I'd suggest a HiddenInput control.
Do not save it as www.mysite.com?t=here. Just save the entry as the user types it. While showing it to user later, convert the "(link: here)" to link and show that.
Save the post as the user wrote it. This will make it easier to allow editing of the post later. When you render the message you should use a regular expression to replace it with a real link. You should never replace all ")" with ">". What happends if i write "hello (world)"?
The result:
Hello (world>
You can find great regular expressions here:
http://regexlib.com

Resources