Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Aug 20, 2021 5:08 am

Hi Team,

Let me know if there is any way to extract the alternate text/content from the object.

These are the some elements I see inside document.xml file.

<v:group><w:pict><mc:Fallback><mc:AlternateContent><w:r>
</v:group></w:pict></mc:Fallback></mc:AlternateContent></w:r>


Basically, there is one image inside textbox but unlike normal cases, this is embedding inside the alternate content element. When I try to copy the same object in other file, it works.

I try to create the same object in sample document but it works so cannot share it and the original document has some sensitive information so I suspect on sharing the same.

Please confirm if this extraction is possible.

pr20080798
 
Posts: 149
Joined: Wed Jan 20, 2021 1:15 pm

Fri Aug 20, 2021 11:43 am

Hi,

Thank you for your inquiry.
According to your description, I learned that you want to get the "Alternative text" property of the shape object, right? If so, I'm sorry to tell it is not available in our Spire.Doc at present. I have submitted the requirement to our Dev team to implement, once it is released, I will inform you.
BTW, to help us have a better test based on your document when the property is available, please share your sample word document with us, you could remove the sensitive data from your sample and then send to us via email (support@e-iceblue.com)

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Tue Aug 24, 2021 3:41 am

Thanks Annika.

I do not have sample document for this case as of now, and the images/shapes I talked about have the sensitive information so cannot share here.

Till the time developers confirm on the alternate text, could you please confirm me on the overlapped objects? So the case is, if I put word objects over to the image, is it possible to extract it as an image and the overlapped object should also part of it? Looks like the document I mentioned at the beginning of this thread has same scenarios.

Thanks!

pr20080798
 
Posts: 149
Joined: Wed Jan 20, 2021 1:15 pm

Tue Aug 24, 2021 11:06 am

Hi,

Thank you for your feedback.
1. According to your first post ("there is one image inside textbox"), I learned that your word has a textbox object that contains an image, and you want to extract the whole object as a picture. Our Spire.Doc could achieve it. Sample code is shown in below code.
2. As for the overlapping objects you mentioned in last post, if it is a shape group, it can also be extracted as a picture. Please refer to the following code.
If this is not what you want, please provide further information to help us have a better understanding. Sharing your sample Word document will be greatly helpful, we promise to keep your document confidential and we will not use it for any other purpose. So you can remove sensitive information and then send it to our email (support@e-iceblue.com).
Code: Select all
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(input);
                 
            int shapeCount = 0;                   
            foreach (Section section in doc.Sections)
            {
                foreach (Paragraph para in section.Paragraphs)
                {
                    foreach (DocumentObject obj in para.ChildObjects)
                    {
                        if (obj.DocumentObjectType == DocumentObjectType.TextBox)
                        {
                            TextBox textbox = obj as TextBox;
                            string fileName = string.Format("Image-{0}.png", shapeCount);
                            Image image = ConverTextBoxToImage(textbox);
                            image.Save(fileName, ImageFormat.Png);
                            shapeCount++;
                        }
                        if (obj.DocumentObjectType == DocumentObjectType.ShapeGroup)
                        {
                            ShapeGroup shapeGroup = obj as ShapeGroup;
                            string fileName = string.Format("Image-{0}.png", shapeCount);
                            Image image = ConverShapeToImage(shapeGroup);
                            image.Save(fileName, ImageFormat.Png);
                            shapeCount++;
                        }
                    }
                }
            }
                     
        }
        public static Image ConverTextBoxToImage(TextBox textBox)
        {
            Document document = new Document();
            Section section = document.AddSection();
            section.AddParagraph().ChildObjects.Add(textBox.Clone());
            Image image = document.SaveToImages(0, ImageType.Bitmap);
            document.Close();
            return image;
        }
        public static Image ConverShapeToImage(ShapeGroup group)
        {
            Document document = new Document();
            Section section = document.AddSection();
            section.AddParagraph().ChildObjects.Add(group.Clone());           
            Image image = document.SaveToImages(0, ImageType.Bitmap);
            document.Close();
            return image;
        }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Wed Aug 25, 2021 3:50 am

Thanks Annika for your response.

I am looking for the image which has some drawn shapes over to that. When I extract the image, the drawn shape gets missed in the extracted image.

Even in case of any shape object, if I draw any other shape on top of that with lesser size than background shape's size, the top one is also missing in extracted image.

Please help if these layers (object on top of another object) can be extracted as a single image. Please note, these are not grouped shapes.

Sample document for overlapped objects is shared via email.

Thanks.

pr20080798
 
Posts: 149
Joined: Wed Jan 20, 2021 1:15 pm

Wed Aug 25, 2021 11:05 am

Hi,

Thank you for sharing files via email.
Sorry our Spire.Doc does not have a direct way to treat the overlapped objects as a whole element and extract it as an image. For the files you provided, you can use the following code to copy all objects in a new document and save it as image.
Code: Select all
        Document doc = new Document();
            doc.LoadFromFile(input);
            int shapeCount = 0;                   
            foreach (Section section in doc.Sections)
            {
                foreach (Paragraph para in section.Paragraphs)
                {
                    Document document = new Document();
                    Section tempSection = document.AddSection();
                    Paragraph paragraph = tempSection.AddParagraph();
                    foreach (DocumentObject obj in para.ChildObjects)
                    {                                             
                        if (obj is DocPicture)
                        {
                            DocPicture dp = obj as DocPicture;                           
                            paragraph.ChildObjects.Add(dp.Clone());                                                     
                        }
                        if (obj is TextBox)
                        {
                            TextBox textBox = obj as TextBox;
                            paragraph.ChildObjects.Add(textBox.Clone());
                        }
                        if (obj.DocumentObjectType == DocumentObjectType.Shape)
                        {
                            ShapeObject shape = obj as ShapeObject;
                            paragraph.ChildObjects.Add(shape.Clone());

                        }
                    }
                    string fileName = string.Format("Image-{0}.png", shapeCount);
                    Image image = document.SaveToImages(0, ImageType.Bitmap);
                    image.Save(fileName, ImageFormat.Png);
                    shapeCount++;
                }
            }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc