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.

Mon Dec 07, 2020 11:09 am

We have a docx template used for badges. On the badge we need a photo.

To do this we have a shape within which is the Tag #PHOTO#.

I can find the text #PHOTO# and replace it with an image from the DB. However I need to be able to set the size to the shape and need to identify the "owning" shape.

At the moment I'm relying on

Code: Select all
      foreach(Paragraph para in section.Paragraphs)
      {

        foreach (DocumentObject obj in para.ChildObjects)
        {
          if (obj is ShapeObject)
          {
            height = (obj as ShapeObject).Height;
            width = (obj as ShapeObject).Width;
          }
        }
      }


and hoping the shape is the last one found, but I would prefer to check and the Shape has the Tag I´m looking for.

Is there a way of doing this?

greygor69
 
Posts: 8
Joined: Mon Dec 07, 2020 11:01 am

Tue Dec 08, 2020 6:57 am

Hello,

Thanks for your inquiry!

You can set the shape size and get the shape has the Tag #PHOTO#. Please refer to the following code.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"E:\testdoc\shapeWithText.docx");
            Section sec = doc.Sections[0];
            TextSelection[] selections = doc.FindAllString("#PHOTO#", false, true);
            int index = 0;
            TextRange tr = selections[0].GetAsOneRange();

            //get the shape has the Tag #PHOTO#
            ShapeObject shape = tr.OwnerParagraph.OwnerTextBody.Owner as ShapeObject;
            //set the shape size
            shape.Width = 250;
            shape.Height = 250;

            //replace picture
            Image image = Image.FromFile(@"E:\downapp\pic\color.jpg");
            DocPicture pic = new DocPicture(doc);
            pic.LoadImage(image);
            index = tr.OwnerParagraph.ChildObjects.IndexOf(tr);
            tr.OwnerParagraph.ChildObjects.Insert(index, pic);
            tr.OwnerParagraph.ChildObjects.Remove(tr);

            doc.SaveToFile("InsertPic.docx", FileFormat.Docx);

If you have any other question, please feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Dec 09, 2020 9:56 am

That's excellent and it worked.

Just one last (hopefully) question

There is a chance that an image is not available to insert. In that case the Shape where the tag is present needs to be removed.

I'm trying to get a reference back to the shape (There is more than one shape in the document) so I can delete that specific shape. I'm having problems getting the shape index to do a RemoveAt.

setting height and width to 0 "works" but, Is there a better way to id and remove the shape?

greygor69
 
Posts: 8
Joined: Mon Dec 07, 2020 11:01 am

Thu Dec 10, 2020 10:26 am

Hello,

Thanks for your reply.

Please refer to the following code to remove the shape where the tag #PHOTO# is present. I have attached my test file.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(@"E:\testdoc\shapeWithText.docx");
            Section sec = doc.Sections[0];
            TextSelection[] selections = doc.FindAllString("#PHOTO#", false, true);
            TextRange tr = selections[0].GetAsOneRange();
            ShapeObject shape = tr.OwnerParagraph.OwnerTextBody.Owner as ShapeObject;
            shape.OwnerParagraph.ChildObjects.Remove(shape);
            doc.SaveToFile("DeleteShape.docx", FileFormat.Docx);


If the issue still exists, please provide us with your input file. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Dec 10, 2020 1:23 pm

Thanks for the quick reply.

I may have miscommunicated what I was trying to do.

We have a template. As part of that template is a shape where the photo will be added.

The current code uses Word and I am trying to replace that with Spire.

With the word code the shape is identified a new shape is created and a picture added. It is placed in the same location with the same size as the existing shape and then the original shape is removed.

I'm trying to achieve the same functionality with Spire. It is possible my approach is wrong because I'm new to this.

With your help I can find the right shape in the document but I can't seem to get the image to the same size as the shape. It appears inside the shape but is cropped and the shape is still visible.

And of course if I delete the shape it removes the image as well. I've attached the resulting docx to illustrate the issue

greygor69
 
Posts: 8
Joined: Mon Dec 07, 2020 11:01 am

Fri Dec 11, 2020 6:16 am

Hello,

Thanks for your reply!

Please refer to the following code to identify the shape with the Tag ”#PHOTO#”, insert a picture has the same size where the shape is located and remove the shape. I have attached my result file.
Code: Select all
                 Document doc = new Document();
            doc.LoadFromFile(@"E:\testdoc\LoyaltyCard\LoyaltyCard.docx");

            TextSelection[] selections = doc.FindAllString("#PHOTO#", false, true);
            TextRange tr = selections[0].GetAsOneRange();
            ShapeObject shape = tr.OwnerParagraph.OwnerTextBody.Owner as ShapeObject;
            int shapeIndex = shape.OwnerParagraph.ChildObjects.IndexOf(shape);

            //load picture
            Image image = Image.FromFile(@"E:\downapp\pic\insertPic.jpg");
            DocPicture pic = new DocPicture(doc);
            pic.LoadImage(image);

            //set picture properties
            pic.Width = shape.Size.Width;
            pic.Height = shape.Size.Height;

            pic.HorizontalAlignment = shape.HorizontalAlignment;
            pic.HorizontalOrigin = shape.HorizontalOrigin;
            pic.HorizontalPosition = shape.HorizontalPosition;

            pic.VerticalPosition = shape.VerticalPosition;
            pic.VerticalAlignment = shape.VerticalAlignment;
            pic.VerticalOrigin = shape.VerticalOrigin;

            pic.TextWrappingStyle = shape.TextWrappingStyle;
            pic.TextWrappingType = shape.TextWrappingType;
            //insert picture and remove the shape
            shape.OwnerParagraph.ChildObjects.Insert(shapeIndex, pic);
            shape.OwnerParagraph.ChildObjects.Remove(shape);

            doc.SaveToFile("picInsteadShape.docx",FileFormat.Docx);

If you have any other problem, please feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Dec 11, 2020 10:39 am

Hah, I was close with what I was doing yesterday when trying to achieve this.

But yes, this seems to work perfectly, many thanks.

greygor69
 
Posts: 8
Joined: Mon Dec 07, 2020 11:01 am

Mon Dec 14, 2020 2:43 am

Hello,

Thanks for your reply and sorry for the late reply as weekend.

Glad to hear that your issue has been solved.

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc