Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to extract a Word document from an URL and duplicate it in my C# page

Posted on 2007-10-12
9
Medium Priority
?
557 Views
Last Modified: 2008-01-09
Upon page load, my page should take an often-updated Word policy document residing at our sister organization's URL and reproduce it in my C# page--
--appending a button the user can click to acknowledge having read the policy and get their acceptance recorded in our own database.

The part involving the button is easy; on the other hand, I'm trying to figure out how to reproduce the Word .doc.  

I know how to reproduce an HTML file via streamreader, probably could do a txt file the same way.  But how to copy a proprietary file such as a .doc ?  Alien characters pop up.

Here's the page-load code I used in my cs, until my boss clarified she needed the .DOC from the directory instead of the HTML file by the same name:

 using System;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Text;


namespace SignIt
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //LOAD

            //read file from URL
            //write file to user's viewing location

            System.Text.StringBuilder sbuild = new StringBuilder();
            string temp = "";

            StreamReader webstream = new StreamReader(WebRequest.Create("http:   ( ***Our buddy organization*** )    .html").GetResponse().GetResponseStream());
     

             while ((temp = webstream.ReadLine()) != null)
            {
                sbuild.Append(temp + "\r\n");
            }

            webstream.Close();

            frmSignIt.Controls.Add(new System.Web.UI.LiteralControl(sbuild.ToString()));

            lblPolicy.Text = sbuild.ToString();

        }



}
0
Comment
Question by:CAWhite
  • 6
  • 3
9 Comments
 

Author Comment

by:CAWhite
ID: 20069132
By the way, my boss already shot down IFrames.  Somebody tried that before I got this task.
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20069260
User "Word Interop" to load the document, extract only the text and display it in your textbox.
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 2000 total points
ID: 20069278
Here is some discussion going on about the same
http://www.neowin.net/forum/lofiversion/index.php/t316480.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:CAWhite
ID: 20074382
Thanks, I'd been digesting that one.  

In that article, user DTMunir just wants a searchable result.  I've gotten that far.  

But it's not acceptable for a document I'm showing for users to read so I can get their signature.  How to retain the formatting of the original Word doc ?  
0
 

Author Comment

by:CAWhite
ID: 20074388
Using VS 2005 and Framework 2.0.
0
 

Author Comment

by:CAWhite
ID: 20074854
Correction-I'm getting good copy from code your page linked to.  

Thanks very much !

I had been seeing the results in a text box that couldn't read the special characters, and hadn't realized I was getting a good copy until I pasted it into Wordpad.  



In the target page,  I've been trying to figure out how to get the result to look like the original Word doc.

I've been through a lot of implementations--this code does Select All in the target document, then pastes it to TextBox1 (as one line--the whole contract).  Alternatively, I've tried outputting to a label (it's all run together--also not quite acceptable).

Does somebody know how to get the copied selection into a Webpage that looks like the original doc, and includes the acknowledgement button  ?  (my first assignment in C#, nothing I've tried has worked, I must just not see the light)

Too bad my boss doesn't want to do IFrames.




===============================================================


using System;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Web.UI;

namespace SignIt

{


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //LOAD

            //read file from URL
            //write file to user's viewing location
           
            Word.ApplicationClass wWordApp = new Word.ApplicationClass();
            wWordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;


            object fileName = "http:   ( ***Our buddy organization*** )    Contract.doc";            
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;

           
            Word.Document dFile = wWordApp.Documents.Open(ref fileName,
                                     ref missing, ref readOnly,
                                     ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref isVisible,
                                     ref missing, ref missing, ref missing, ref missing);

            dFile.Select();

            TextBox1.Text = wWordApp.Selection.Text;

        }

}
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 20074947
The problem is its a word document. It will be difficult to make it look exactly the same on a text box. Instead may i suggest you to just set a good font to the text that is displayed.
0
 

Author Comment

by:CAWhite
ID: 20080922
Oh my.  I've been exploring horribly arcane  options--yet all I needed to do for a vast improvement was set the text box "Multiline."

The special characters remain interspersed in the text, although content is definitely easy to read and  I can see if I can write a fast-enough filter to take care of the specials.  But I'm going to look at least a little into third-party solutions.

My current solution--copying from clipboard to text box-- is not without its problems--I wonder if the scroll bars will be an issue; you never know what the user's browser size is set to.

My boss says she disfavors the iFrames because accessibility for the visually-impaired is critical in our environment; essentially the iFrame is a page-within-a-page, and browsers for the blind tend not to know to go to the iFrame to read.  

Sarajguptha, thank you for your comments.  I most heavily relied on code from a page that was linked to early in the page you mentioned; its link was
http://www.codeproject.com/aspnet/wordapplication.asp


I found a similar situation explored with different options at http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21760178.html

0
 

Author Comment

by:CAWhite
ID: 20081460
Refined my solution a bit: Replaced some "box" characters that were going to the textbox, turned them from \a into \r  (carriage return?) codes.



        protected string fixStuff(string incoming)
        {
            string strFixed;
            strFixed = incoming.Replace("\r\a", "\r\r");
            return strFixed;
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question