Load Document from byte array

Technical support for Spire.Doc

Moderator: iceblue support

Load Document from byte array

Postby softecregistrierung » Thu Mar 17, 2011 1:45 pm

Can you please give me an example, how to load a Document from a byte array?
I tried the code below but i get the following error: 'Can't open storage on LockBytes'

Best regards,
Hubert

Code: Select all
byte[] theData = ...Loaded from Database

Document document = new Document();
byte[] dataIn = new byte[(int)theData.Length];
using (Stream ms = new MemoryStream(dataIn, true))
{
   ms.Write(theData, 0, theData.Length);
   document.LoadFromStream(ms, FileFormat.Auto);
}
softecregistrierung
 
Posts: 21
Joined: Mon Mar 07, 2011 9:35 am

Re: Load Document from byte array

Postby harry.support » Mon Mar 21, 2011 10:00 am

Dear Hubert,
It looks like your code has a problem. The byte array dataIn has no doc data. You forget to init it.
Please try:
Code: Select all
using System;
using System.IO;
using Spire.Doc;

namespace LoadDocFromByteArray
{
    class Program
    {
        static void Main(string[] args)
        {
            String[] files = new String[] { @"..\..\Sample1.doc", @"..\..\Sample2.docx"};
            for(int i = 0; i < files.Length; i++)
            {
                byte[] buffer = File.ReadAllBytes(files[i]);
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    Document doc = new Document();
                    doc.LoadFromStream(stream, FileFormat.Auto);
                    doc.SaveToFile(String.Format("Sample{0}.doc", i));
                }
            }
        }
    }
}


A full demo was attached, please check out.
You do not have the required permissions to view the files attached to this post.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Load Document from byte array

Postby softecregistrierung » Tue Mar 22, 2011 6:29 am

Thanks a lot.
The line
Code: Select all
ms.Write(theData, 0, theData.Length);
was wrong.
But now i want to get a byte array instead of saving it to a file. Can you give me a short example?

Best regards,
Hubert
softecregistrierung
 
Posts: 21
Joined: Mon Mar 07, 2011 9:35 am

Re: Load Document from byte array

Postby harry.support » Thu Mar 24, 2011 6:43 am

Dear Hubert,
Please try:
[code]
byte[] buffer = null;
using (MemoryStream stream = new MemoryStream())
{
document.SaveToStream(stream, FileFormat.Doc);
buffer = stream.ToArray();
}
[code]
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Load Document from byte array

Postby softecregistrierung » Thu Mar 24, 2011 7:09 am

it is not working when i use the parameter 'FileFormat.Auto'.
Can you confirm this?

Best regards,
Hubert
softecregistrierung
 
Posts: 21
Joined: Mon Mar 07, 2011 9:35 am

Re: Load Document from byte array

Postby harry.support » Mon Mar 28, 2011 1:58 am

Dear Hubert,
You could not use 'FileFormat.Auto' when you save a document. You must explicitly specify the format which you want to save.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Load Document from byte array

Postby simorav » Fri Jul 27, 2012 8:55 am

Hi,
I have the same problem ('Can't open storage on LockBytes') when trying to load bytes from a DOC file created from an HTML (see attachment) in a web application.
Can you help me?

Best regards
You do not have the required permissions to view the files attached to this post.
simorav
 
Posts: 9
Joined: Mon Jul 16, 2012 1:46 pm

Re: Load Document from byte array

Postby amy.zhao » Mon Jul 30, 2012 3:35 am

Hi simorav,

Your file is HTML file not doc file, so you can try to use the following way to load your file.
Code: Select all
Document doc = new Document();
            doc.LoadFromFile(@"..\..\NL_11_2012.html",FileFormat.Html, XHTMLValidationType.None);


If you have any problems, please contact us.

Have a nice day!

Amy
e-iceblue support
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Load Document from byte array

Postby simorav » Mon Jul 30, 2012 9:05 am

Hi Amy,
I tryed your code but I obtain an error: "URI formats are not supported."
The problme is that I'm working in SharePoint 2007 context, so my file is uploaded in a web library an I can use it only by url address.

Code: Select all
doc.LoadFromFile("http://............/NL_11_2012.html", FileFormat.Html, XHTMLValidationType.None);


Any suggestion?

Thanks, regards
Simone
simorav
 
Posts: 9
Joined: Mon Jul 16, 2012 1:46 pm

Re: Load Document from byte array

Postby harry.support » Tue Jul 31, 2012 6:00 am

Hello Simone,

Sorry for these inconvenience caused by us. We have analysed your HTML and we found some issues which our Spire.Doc does not support at present, for example: the default border of table, the default width of table, the color is style by rgb(r, g, b) method and so on. You html document is too complex for our Spire.Doc. We add your issue to our plan, but we could fix it at present.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Load Document from byte array

Postby simorav » Tue Sep 04, 2012 2:02 pm

Hi Harry,
I tryed to simplify my html file to convert.
I tryed to use a plain html file like this:

Code: Select all
<html>
   <head/>
    <body>
       <div>Hello World</div>
    </body>
</html>


it worked fine, and I obtained a nice PDF file.
After I tryed to insert an image in the html code:

Code: Select all
<html>
   <head/>
    <body>
       <div>
            <img src="http://www.e-iceblue.com/forum/styles/prosilver/imageset/forum-logo.gif" alt="" />
       </div>
    </body>
</html>


I obtained the error "URI formats are not supported.".
Then I tryed to download the previous image and insert it in the html code:

Code: Select all
<html>
   <head/>
    <body>
       <div>
            <img src="C:\\forum-logo.gif" alt="" />
       </div>
    </body>
</html>


it worked fine, and I obtained a nice PDF file.
I need to use web url in my file, because my code works in a web application and it hasn't access to local resorces of the server.
How can I do this?

Thanks
Simone
simorav
 
Posts: 9
Joined: Mon Jul 16, 2012 1:46 pm

Re: Load Document from byte array

Postby harry.support » Wed Sep 05, 2012 10:48 am

Hi Simone,

We have add a new feature to support to get image from a absolute url as your second html code:
Code: Select all
<html>
   <head/>
    <body>
       <div>
            <img src="http://www.e-iceblue.com/forum/styles/prosilver/imageset/forum-logo.gif" alt="" />
       </div>
    </body>
</html>

Please download the hotfix Spire.Doc Pack (Hot Fix) Version:4.4.24 from http://www.e-iceblue.com/Download/download-word-for-net-now.html

And it can also get a image from a relative url base on a absolute url, for example:
Code: Select all
<!--- html file sample.html -->
<html>
   <head/>
    <body>
       <div>
            <img src="imageset/forum-logo.gif" alt="" />
       </div>
    </body>
</html>

Code: Select all
Document doc = new Document();
using(TextReader reader = File.OpenText(@"sample.html"))
{
   doc.LoadHTML(reader, "http://www.e-iceblue.com/forum/styles/prosilver/",
      Spire.Doc.Documents.XHTMLValidationType.None);
}
doc.SaveToFile("test.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("test.docx");

Hope it will help you.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Load Document from byte array

Postby simorav » Wed Sep 05, 2012 3:29 pm

Hi Harry,
thanks for your quickly answer, I'm trying to use the hotfix and now the error reported it seems fixed.

Now I have another problem: when I try to load an image loaded in my local webserver I obtain an error like this:

The remote server returned an error: (401) Unauthorized.
at System.Net.WebClient.OpenRead(Uri address)

Who is the user that try to read the uri? Is the current user or I have to explicit authorize a specific user?

Thanks, bye
Simone
simorav
 
Posts: 9
Joined: Mon Jul 16, 2012 1:46 pm

Re: Load Document from byte array

Postby amy.zhao » Thu Sep 06, 2012 3:14 am

Dear Simone,

It looks like that the WebClient got the response with status 401. So we think the WebClient has right to access the remote server but the remote server which hosts the image has some problems about the authorization. You can do a simple test in the same application to check it:
Code: Select all
using (WebClient client = new WebClient())
{
   using (Stream stream = client.OpenRead(new Uri("your image url")))
   {
      Image image = Image.FromStream(stream);
      image.Save("img.png");
   }
}


Best wishes,
Amy
e-iceblue support
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Load Document from byte array

Postby simorav » Thu Sep 06, 2012 7:50 am

Hi Amy,
I still have 401 error when I try to open image using WebClient.
If I specify credentials using this code:

Code: Select all
client.Credentials = CredentialCache.DefaultCredentials;


I'm able to download the image.
So, how can I specify credentials used by Spire.Doc dll?
simorav
 
Posts: 9
Joined: Mon Jul 16, 2012 1:46 pm

Next

Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 1 guest

cron