【问题标题】:Making a string inside a sentence bold - Apache POI使句子中的字符串变为粗体 - Apache POI
【发布时间】:2022-11-13 22:37:50
【问题描述】:

如何在句子中使字符串变为粗体?

我刚开始使用 Apache POI 就像 4 小时前一样,想:

  1. 在 Word 文档 (.docx/XWPF) 中定位不同的字符串
  2. 将字符串替换为地图的值
  3. 如果字符串有特殊标志,则使值显示为粗体。

    我试图首先通过迭代段落(不是运行)来解决这个问题,但它没有用。我从这篇文章中得到了我目前的解决方案here

    前两个步骤一切正常,现在我想让每个包含特殊标志(如${key:bold}${score:bold})的值加粗。我试图创建一个新的XWPFRun 来写入粗体字符串,但它不能与下面的代码一起使用......

    ParagraphFieldReplacer.java(目前正在为第 1 步和第 2 步工作)

    import java.util.List;
    import java.util.Map;
    
    import org.apache.poi.ooxml.POIXMLException;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    
    public final class ParagraphFieldReplacer {
        
        public static void replace(final Student student, final XWPFDocument document, final Map<String, FormatObject> fields) {
            final List<XWPFParagraph> paragraphs = document.getParagraphs();
            for (final XWPFParagraph paragraph : paragraphs)
                replaceParagraph(student, paragraph, fields);
        }
        
        private static void replaceParagraph(final Student student, final XWPFParagraph paragraph, final Map<String, FormatObject> fields) throws POIXMLException {
            List<XWPFRun> runs;
            String paragraphtext;
            String runsText;
            XWPFRun nextRun;
            String target;
            XWPFRun run;
            for (final String key : fields.keySet()) {
                paragraphtext = paragraph.getText();
                if (!(paragraphtext.contains("${")))
                    return;
                target = "${" + key + "}";
                if (!(paragraphtext.contains(target)))
                    continue;
                runs = paragraph.getRuns();
                for (int i = 0; i < runs.size(); i++) {
                    run = runs.get(i);
                    runsText = run.getText(0);
                    if (runsText.contains("${") || (runsText.contains("$") && runs.get(i + 1).getText(0).substring(0, 1).equals("{"))) {
                        while (!(openTagCountIsEqualCloseTagCount(runsText))) {
                            nextRun = runs.get(i + 1);
                            runsText = runsText + nextRun.getText(0);
                            paragraph.removeRun(i + 1);
                        }
                        if (!(runsText.contains(target)))
                            run.setText(runsText, 0);
                        else {
                            final FormatObject format = fields.get(key);
                            final String handle = format.handle(student);
                            run.setText(runsText.replace(target, handle), 0);
                        }
                    }
                }
            }
        }
        
        private static boolean openTagCountIsEqualCloseTagCount(final String runText) {
            final int openTagCount = (runText.split("\\$\\{", -1).length - 1);
            final int closeTagCount = (runText.split("}", -1).length - 1);
            return (openTagCount == closeTagCount);
        }
        
    }
    

    FormatObject.java

    public final class FormatObject {
        
        private boolean bold;
        private FormatHandler<Student, String> handler;
        
        public FormatObject(final FormatHandler<Student, String> handler, final boolean bold) {
            this.handler= handler;
            this.bold = bold;
        }
        
        public boolean isBold() {
            return bold;
        }
        
        public String handle(final ZertifikationsStudent student) {
            return handler.handle(student);
        }
        
    }
    
    

    FormatHandler.java

    @FunctionalInterface
    public interface FormatHandler<P, R> {
        
        public R handle(P p);
        
    }
    

    感谢阅读/帮助!

【问题讨论】:

    标签: java ms-word apache-poi


    【解决方案1】:

    前几天我得到了这个工作!

    使其加粗的最佳方法是在文档本身中使格式指示符加粗。

    另一种方法是创建一个新的XWPFRun 并将其设置为bold。文本应该是键的值。之后,您只需将所有固定的XWPFRuns 添加到ArrayList 中,然后在删除所选段落中的每个运行后重新插入它们。然后,您可以删除包含格式指示符的旧正常 XWPFRun

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-11
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2015-04-14
      相关资源
      最近更新 更多