Saving and Retrieving word document into/from database

Hi,

I was trying to save a word document which has images, tables, lists into MSSQL database. I could store the document in varbinary(MAX) but while fetching the document back, this line from the programmer’s guide throws invalid casting error:

byte[] buffer = (byte[])dataTable.Rows[0]["FileContent"];

I fixed it with Unicode solutions and all. My issue is, the images from the original

document

is lost somewhere. I can see the placeholders but the images aren’t there.

Also, the saving and retrieving seems inconsistent with different word document,

especially with (.docx). Any help is appreciated. Thanks in advance.

Hi Julian,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please create a standalone/runnable simple application (for example a Console Application Project) that demonstrates the code (Aspose.Words code) you used to generate your output document
  • Please attach the output Word file that shows the undesired behavior.

Unfortunately, it is difficult to say what the problem is without the documents and simplified application. We need your documents and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Hi Tahir,

Thank you for your response and sorry at the same time for not making it clear. As I had no time to wait for the response, I did a detour and solved that issue with some other means. However, a small question. Is there anyway I can differentiate Headings (H1) with normal text? Given that the word document has not been formatted and the only clue is the headings are bolded. I want to create a navigation structure. e.g

THIS IS BOLDED

some paragrapth bold text within para

THIS IS BOLDED2

plain texts with images and tables…

Thanks in advance.

Hi Julian,

Thanks for your inquiry. Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style,
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

In your case, we suggest you please use ParagraphFormat.Style.Name property to get the style name. Hope this helps you. If you still face problem, please share your input document here for our reference. We will then provide you more information about your query along with code.

Hi Tahir,

Thanks for your reply. I tried couple of ways to sort this out. Some of the things I did was traversed through all the paragraphs and through their runs. What I did for now is, whenever the font is bold, I categorized that as a header (Which is not a solution at all). Heres the code:

foreach (Paragraph paragraph in paragraphs)
{
    NodeCollection runs = paragraph.GetChildNodes(NodeType.Run, true);
    double bigFont = 0;
    foreach (Run run in runs)
    {
        bigFont = run.Font.Size;
        if (run.Font.Bold && run.Font.Size > i)
        {
            paragraph.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
            // paragraphsWithStyle.Add(run);
        }
    }
}

All I need is to create a TOC on the basis of headers. I have attached an input test document.

Thanks in advance for your help.

Hi Julian,

Thanks for your inquiry. In your case, we suggest you please insert the TOC field at the start of document and change the style of bold text to heading styles e.g Heading 1, Heading 2 etc. Move the cursor to the start of document using DocumentBuilder.MoveToDocumentStart method and then insert the TOC field using DocumentBuilder.InsertTableOfContents method.

Note that InsertTableOfContents will only insert a TOC field, but will not actually build the table of contents. The table of contents is built by Microsoft Word when the field is updated. We suggest you please read following documentation link for your kind reference.

How-to Insert and Work with the Table of Contents Field