Extract Formatted Cell Content Images from Word Document & Save in SQL Database Table using C# .NET - Question Answer

asdas.png (31.6 KB)
I want to store information in each field of this table along with the image and other properties in the database

@m_mosavi,

You can simply extract the Table from Document, insert it inside a new temporary document, save the temporary document into byte array and finally store the byte[] inside database .Please check the following C# Code of Aspose.Words for .NET API:

Document doc = new Document("E:\\Temp\\in.docx");
Table targetTable = doc.FirstSection.Body.Tables[0];

Document cloneDoc = (Document)doc.Clone(false);
cloneDoc.RemoveAllChildren();
cloneDoc.EnsureMinimum();

Table importedTable = (Table)cloneDoc.ImportNode(targetTable, true, ImportFormatMode.KeepSourceFormatting);
cloneDoc.FirstSection.Body.InsertBefore(importedTable, cloneDoc.FirstSection.Body.LastParagraph);

MemoryStream stream = new MemoryStream();
cloneDoc.Save(stream, SaveFormat.Docx);
stream.Position = 0;
byte[] bytes = stream.ToArray();

Please also check the following article:

Hope, this helps.

asdasdasdasdasdasdasdasgfgdfgdfgdf.png (49.6 KB)

Unfortunately, this piece of code didn’t work for me
Which version should I use?

@m_mosavi,

You do not need to use Aspose.Cells API; only use Aspose.Words for .NET 20.2 to accomplish this task. In case the problem still remains, please create a standalone simple Console Application that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size. Thanks for your cooperation.

Thanks for the help, I was able to save the information
But ,I want to get the data with format of each cell in the table and store each one in the corresponding fields in the bank
for Example :
row[0][0] save in column Question-Text in sql table
row[1][0] save in column answer1-Text in sql table
row[2][0] save in column answer2-Text in sql table
row[3][0] save in column answer3-Text in sql table
row[4][0] save in column answer4-Text in sql table
Example.png (31.5 KB)

@m_mosavi,

Please ZIP and upload your sample input Word document here for testing. We will then investigate the scenario on our end and provide you more information.

sample.zip (91.3 KB)

my sample input Word document here for testing

@m_mosavi,

You can copy content of a particular Cell inside a temporary Document and then save the whole Document instance to SQL Database field. Sample C# .NET code is as follows:

Document doc = new Document("E:\\Temp\\Sample\\sample.docx");

Table targetTable = doc.FirstSection.Body.Tables[0];

for (int i = 0; i < targetTable.Rows.Count; i++)
{
    Row row = targetTable.Rows[i];
    for (int j = 0; j < row.Cells.Count; j++)
    {
        Cell cell = row.Cells[j];

        Document cloneDoc = (Document)doc.Clone(false);
        cloneDoc.RemoveAllChildren();
        cloneDoc.EnsureMinimum();

        foreach (Node node in cell.ChildNodes)
            cloneDoc.FirstSection.Body.AppendChild(cloneDoc.ImportNode(node, true));

        cloneDoc.FirstSection.Body.FirstParagraph.Remove();

        // cloneDoc.Save("E:\\Temp\\Sample\\doc_row_" + i + "_cell_" + j + ".docx");

        MemoryStream stream = new MemoryStream();
        cloneDoc.Save(stream, SaveFormat.Docx);
        stream.Position = 0;
        byte[] bytes = stream.ToArray();
    }
}

Hope, this helps.

1 Like

Thank you very much for your help
But, I want to show this content to the user later in the web page format and inside the ckeditor or tinymce but I have not saved this content in text format

@m_mosavi,

Instead of saving to DOCX format, you can save to HTML or TXT format and then save to SQL Database. Here is sample code:

...
...
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.Html);
opts.ExportImagesAsBase64 = true;
opts.CssStyleSheetType = CssStyleSheetType.Inline;

MemoryStream stream = new MemoryStream();
cloneDoc.Save(stream, opts);
stream.Position = 0;
byte[] bytes = stream.ToArray();
1 Like

Thank you very much for your help, but I didn’t try to display the text along with the image and format inside the editor. Unfortunately, aspose libraries are incomprehensible.
Thank you for guiding me whenever possible
sample.png (53.4 KB)

It is extracted by TxtSaveOptions simple means only

@m_mosavi,

You can try specifying encoding during loading or saving a Document instance. For example:

TxtSaveOptions opts = new TxtSaveOptions();
opts.Encoding = System.Text.Encoding.UTF8;

Hope, this helps.

I’ve been working on this for a few days, but unfortunately I didn’t get any results. I couldn’t save the text to the bank with all the features or display it in the editor.
It seems impossible in this way
Thank you for your help

@m_mosavi,

Can you please summarize your ‘full scenario’ (usecase) here for our reference? And also, please provide complete steps (along with source code) that we can follow to reproduce the same problem on our end. Thanks for your cooperation.

I have a question design page.
On this page, the user enters the text of the question and options into the relevant fields and saves on the button click and the information is stored in the bank.
Now I want the user to be able to upload the Word file and save the text of the question and their options in the bank.
I want them saved if the text of the question and options has an image or style.
I want this information to be saved as text, not a file, or … just text that I can then display and print inside a text editor such as ckeditor.Untitled.png (10.1 KB)
Untitled2.png (129.1 KB)

@m_mosavi,

Please check the following code:

TxtSaveOptions opts = new TxtSaveOptions();
opts.Encoding = System.Text.Encoding.UTF8;

Document doc = new Document("E:\\Temp\\Sample\\sample.docx");
Table targetTable = doc.FirstSection.Body.Tables[0];

// remove all images from Document object
targetTable.GetChildNodes(NodeType.Shape, true).Clear();
// remove all equations from Document object
targetTable.GetChildNodes(NodeType.OfficeMath, true).Clear();

DocumentBuilder builder = new DocumentBuilder();
builder.Font.Bidi = true;
builder.ParagraphFormat.Bidi = true;
for (int i = 0; i < targetTable.Rows.Count; i++)
{
    // first row represents question text
    if (i == 0)
    {
        string question = targetTable.Rows[i].Cells[0].ToString(opts);
        // we are writing to a new Document instance in memory for demo purpose
        // But you can save string in DB
        builder.Font.Color = Color.Red;
        builder.Writeln(question);
    }
    else
    {
        builder.Font.Color = Color.Black;
        // remaining rows are options
        string option = targetTable.Rows[i].Cells[0].ToString(opts);
        // print or save it in DB
        builder.Writeln(option);
    }
}

builder.Document.Save("E:\\temp\\Sample\\20.4.docx"); 

Hope, this helps in achieving what you are looking for. Also, please check the following article: