docx4j 将.docx转.pdf时,字体超出表格范围该如何解决?

使用 docxToPdf() 将 .docx 文件转换为 .pdf 时,中文的可以顺利换行,但是一连串的英文就会超出表格,无法自动换行。请问这种问题应该如何解决?

源word:
1675229860655.png
问题描述的内容:AAAAAA、AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA、被、、、、AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA、AAAAAAAAAAAAAAAAAA、AAAAAAAAAAAAAAAAAA、AAAAAAAAAAAAAAAAAAAAAAAA、AAAAAAAAAAAA、AAAAAAAAAAAAAAAAAA

转换后的pdf:
1675229814631.png

  • maven依赖版本

    <!--        docx转pdf-->
          <dependency>
              <groupId>org.docx4j</groupId>
              <artifactId>docx4j-JAXB-Internal</artifactId>
              <version>8.2.4</version>
          </dependency>
          <dependency>
              <groupId>org.docx4j</groupId>
              <artifactId>docx4j-export-fo</artifactId>
              <version>8.2.4</version>
          </dependency>
  • docxToPdf()方法

      /**
       * .docx转.pdf
       * @param docxPath docx文件路径
       * @param pdfPath 输出的pdf文件路径
       * @throws Exception
       */
      public static boolean docxToPdf(String docxPath, String pdfPath) throws Exception {
          FileOutputStream out = null;
          try {
              File docxfile = new File(docxPath);
              WordprocessingMLPackage pkg = Docx4J.load(docxfile);
    
              Mapper fontMapper = new IdentityPlusMapper();
              fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
              fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
              fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
              fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
              fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
              fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
              fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
              fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
              fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
              fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
              fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
              fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
              fontMapper.put("等线", PhysicalFonts.get("SimSun"));
              fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
              fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
              fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
              fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
              fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
              fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
              fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
              fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
              fontMapper.put("宋体扩展", PhysicalFonts.get("simsun-extB"));
              fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
              fontMapper.put("新細明體", PhysicalFonts.get("SimSun"));
              pkg.setFontMapper(fontMapper);
    
              out = new FileOutputStream(pdfPath);
              Docx4J.toPDF(pkg, out);
              // 删除源.docx文件
              docxfile.delete();
              return true;
    
    //        } catch (FileNotFoundException e) {
    //            e.printStackTrace();
    //        } catch (Docx4JException e) {
    //            e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
              return false;
          } finally {
              if (out != null) {
                  try {
                      out.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
    
阅读 2.8k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏