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.

Sat Aug 27, 2022 11:46 am

Dears,
iam working on applaication , i create class to replace image on word by "${Photo}" , but i need to fit this image inside textbox or shape

kindl need your support

Main Class

Code: Select all
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

import java.util.HashMap;
import java.util.Map;

public class Main2 {

    public static void main(String[] args)
    {

        String filename="new_resume_001.docx";
        String newFileName="new_resume_001.docx";

        new WordTemplateCreator().findReplaceImage(filename,newFileName,"website.jpg");

    }

}


Linked Class


Code: Select all

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

import java.util.Map;

public class WordTemplateCreator {

    // These Class to Create Word with Varibale by Map method

    WordTemplateCreator(){}


    public void TableCreator(String filename,String newFileName, Object [] list)
    {
        for(int i =0 ; i<list.length;i++)
        {

            if (i==0){
                new TableCreator(filename, newFileName,i, (Map<String, String>) list[i]).Run();
            }
            else{
                new TableCreator(newFileName, newFileName,i, (Map<String, String>) list[i]).Run();
            }


        }
    }

    public void findReplaceWord(String filename,String newFileName,Map<String,String>wordmap){

        for (Map.Entry<String, String> name : wordmap.entrySet())
        {
            Document document = new Document(filename);
            document.replace(name.getKey(), name.getValue(), false, true);
            document.saveToFile(newFileName, FileFormat.Docx_2013);
        }

    }

/// Method to replace Image
    public void findReplaceImage(String filename, String newFileName,String imagePath){
        Document document = new Document(filename);
        TextSelection[] selections = document.findAllString("${Photo}", false, true);
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {
            TextSelection textSelection = (TextSelection)obj;
            DocPicture pic = new DocPicture(document);
            pic.loadImage(imagePath);
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }
        document.saveToFile(newFileName, FileFormat.Docx_2013);

    }
}


shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Mon Aug 29, 2022 9:58 am

Hi,

Thank you for your inquiry.
You can achieve your need by setting the background of the textbox, please refer to the code below. If you have any questions, please feel free to contact.
Code: Select all
        Document document = new Document(filename);
        TextSelection[] selections = document.findAllString("${Photo}", false, true);
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {
            TextSelection textSelection = (TextSelection)obj;
            range = textSelection.getAsOneRange();
            DocPicture pic = new DocPicture(document);
            pic.loadImage(imagePath);
            TextBox tb = (TextBox)range.getOwnerParagraph().getOwner().getOwner(); 
            //Set background for textbox
            tb.getFormat().getFillEfects().setType(BackgroundType.Picture);
            tb.getFormat().getFillEfects().setPicture("website.jpg");
            range.getOwnerParagraph().getChildObjects().remove(range);
        }
        document.saveToFile(newFileName, FileFormat.Docx_2013);

Sincerely,
Kylie
E-icbelue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Wed Aug 31, 2022 4:16 pm

i run the below mehod but ${Photo} not replace by photo

Code: Select all
  public void findReplaceImage(String filename, String newFileName,String imagePath)
    {
        Document document = new Document(filename);
        TextSelection[] selections = document.findAllString("${Photo}", false, true);
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {
            TextSelection textSelection = (TextSelection)obj;
            range = textSelection.getAsOneRange();
            DocPicture pic = new DocPicture(document);
            pic.loadImage(imagePath);

            TextBox tb = (TextBox)range.getOwnerParagraph().getOwner().getOwner();
            //Set background for textbox
            tb.getFormat().getFillEfects().setType(BackgroundType.Picture);
            tb.getFormat().getFillEfects().setPicture(imagePath);
            range.getOwnerParagraph().getChildObjects().remove(range);
           
           
        }
        document.saveToFile(newFileName, FileFormat.Docx_2013);

    }

shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Wed Aug 31, 2022 4:37 pm

it was problem in Main Class i solved
but still problem exsting , Photo replaced but not Fit

Code: Select all

 public void findReplaceImage(String filename, String newFileName,String imagePath)
    {
        Document document = new Document(filename);
        TextSelection[] selections = document.findAllString("${Photo}", false, true);
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {
            TextSelection textSelection = (TextSelection)obj;
            DocPicture pic = new DocPicture(document);
            pic.loadImage(imagePath);
            range = textSelection.getAsOneRange();
            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            //range.getOwnerParagraph().getChildObjects().remove(range);
            //Set background for textbox
            TextBox tb = (TextBox)range.getOwnerParagraph().getOwner().getOwner();
            tb.getFormat().getFillEfects().setType(BackgroundType.Picture);
            tb.getFormat().getFillEfects().setPicture(imagePath);
            range.getOwnerParagraph().getChildObjects().remove(range);

        }
        document.saveToFile(newFileName, FileFormat.Docx_2013);

    }


shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Thu Sep 01, 2022 8:21 am

Hi,

Thank you for your reply
Please try the code below to fit the textbox. Please feel free to contact if you have any questions.
Code: Select all
    public void findReplaceImage(String filename, String newFileName,String imagePath)
    {
        Document document = new Document(filename);
        TextSelection[] selections = document.findAllString("${Photo}", false, true);
        int index = 0;
        TextRange range = null;
        for (Object obj : selections) {
            TextSelection textSelection = (TextSelection)obj;
            range = textSelection.getAsOneRange();
            DocPicture pic = new DocPicture(document);
            pic.loadImage(imagePath);
            TextBox tb = (TextBox)range.getOwnerParagraph().getOwner().getOwner();
            float tb_width=tb.getWidth();
            float tb_height=tb.getHeight();
            //Set the image size to be the same as the text box
            pic.setHeight(tb_height);
            pic.setWidth(tb_width);

            index = range.getOwnerParagraph().getChildObjects().indexOf(range);
            range.getOwnerParagraph().getChildObjects().insert(index,pic);
            range.getOwnerParagraph().getChildObjects().remove(range);
        }
        document.saveToFile(newFileName, FileFormat.Docx_2013);


Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Fri Sep 02, 2022 10:33 am

Thank YOU Very Match :) :) :)

shadywardy
 
Posts: 19
Joined: Thu Aug 18, 2022 2:25 pm

Mon Sep 05, 2022 2:45 am

Hi,

You're welcome.
If you need assitsance in the future, please feel free to contact us.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc