为有中文需求的客户提供多渠道中文技术支持.

Mon Feb 20, 2023 9:13 am

请问一下首行缩进2字符,还有图片的格式布局设置为四周型,谢谢

hannibal22
 
Posts: 52
Joined: Wed Aug 17, 2022 7:01 am

Mon Feb 20, 2023 10:04 am

您好,

感谢您的咨询。
我们设置缩进值是以point为单位,暂不支持字符缩进。
如果要设置首行缩进和设置图片环绕文字为四周型,请参考如下代码:
Code: Select all
        Document document = new Document();
        document.loadFromFile("test.docx");
        Section section = document.getSections().get(0);
        Paragraph paragraph = section.getParagraphs().get(0);
        // 设置首行缩进
        paragraph.getFormat().setFirstLineIndent(15f);
       
        //设置图片为四周环绕型
        DocPicture docPicture = paragraph.appendPicture("Doc-Java.png");
        docPicture.setTextWrappingStyle(TextWrappingStyle.Square);


如果我的代码没有满足您的需求,你可以随时联系我们。

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Feb 21, 2023 1:03 am

请问一下 如何获取Paragraph paragraph 中的图片 为DocPicture docPicture 这个对象??

hannibal22
 
Posts: 52
Joined: Wed Aug 17, 2022 7:01 am

Tue Feb 21, 2023 1:41 am

您好,

感谢您的咨询。
请参考以下代码:
Code: Select all
        Document document = new Document();
        document.loadFromFile("test.docx");
        Section section = document.getSections().get(0);

        List<DocPicture> pictureList= new ArrayList<>();
        // 获取所有段落集合
        ParagraphCollection paragraphs = section.getParagraphs();
        // 例如获取第二个段落的所有DocPicture对象
        int paraIndex = 1;
        Paragraph paragraph = paragraphs.get(paraIndex);
        // 获取段落所有子对象
        DocumentObjectCollection childObjects = paragraph.getChildObjects();
        for (int j = 0; j < childObjects.getCount(); j++) {
            // 判断子对象类型
            if (childObjects.get(j).getDocumentObjectType().equals(DocumentObjectType.Picture)) {
                DocPicture docPicture = (DocPicture) childObjects.get(j);
                // 添加docPicture对象到集合中
                pictureList.add(docPicture);
            }
        }

如果我的代码没有满足您的需求,请提供您的文档和描述您想实现的效果,感谢您的帮助。

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Feb 21, 2023 2:36 am

请问一下 如何 把设置好的四周型的图片 再放回原来的段落中并覆盖之前的图片?谢谢

hannibal22
 
Posts: 52
Joined: Wed Aug 17, 2022 7:01 am

Wed Feb 22, 2023 10:41 am

您好,

感谢您的咨询。
请参考以下代码:
Code: Select all
        DocPicture docPicture = new DocPicture(document);
        docPicture.loadImage("test.png");
        docPicture.setTextWrappingStyle(TextWrappingStyle.Square);

        // 获取某段落子对象数量
        int count = paragraph.getChildObjects().getCount();
        Map<Integer,DocPicture> docMap = new HashMap<>();
        int index= 0;
        for (int i = 0; i < count; i++) {
            // 获取每一个段落子对象
            DocumentObject documentObject = paragraph.getChildObjects().get(i);
            // 判断子对象类型是否为图片
            if (documentObject.getDocumentObjectType().equals(DocumentObjectType.Picture)) {
                // 如果是的话,添加到map中
                DocPicture docPicture1 = (DocPicture) paragraph.getChildObjects().get(i);

                docMap.put(index++,docPicture1);
            }
        }

        // 遍历完某个段落之后,可以去替换某张图片,例如替换第二张
        DocPicture picture = docMap.get(1);
        // 获取图片在段落中的索引值
        int pictureIndex = paragraph.getChildObjects().indexOf(picture);
        // 插入新图片,删除原有图片
        paragraph.getChildObjects().insert(pictureIndex,picture);
        paragraph.getChildObjects().removeAt(pictureIndex + 1);

如果我的代码没有满足您的需求,请提供您的文档和描述您具体想实现的效果,感谢您的帮助

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to 中文技术支持