【问题标题】:Apache POI HWPF - problem in convert doc file to pdfApache POI HWPF - 将 doc 文件转换为 pdf 的问题
【发布时间】:2010-07-28 10:53:33
【问题描述】:

我目前正在使用 apache poi 处理 Java 项目。 现在在我的项目中,我想将 doc 文件转换为 pdf 文件。转换成功完成,但我只得到 pdf 格式的文本,而不是任何文本样式或文本颜色。 我的 pdf 文件看起来像黑白的。虽然我的 doc 文件是彩色的并且有不同的文本样式。

这是我的代码,

 POIFSFileSystem fs = null;  
 Document document = new Document(); 

 try {  
     System.out.println("Starting the test");  
     fs = new POIFSFileSystem(new FileInputStream("/document/test2.doc"));  

     HWPFDocument doc = new HWPFDocument(fs);  
     WordExtractor we = new WordExtractor(doc);  

     OutputStream file = new FileOutputStream(new File("/document/test.pdf")); 

     PdfWriter writer = PdfWriter.getInstance(document, file);  

     Range range = doc.getRange();
     document.open();  
     writer.setPageEmpty(true);  
     document.newPage();  
     writer.setPageEmpty(true);  

     String[] paragraphs = we.getParagraphText();  
     for (int i = 0; i < paragraphs.length; i++) {  

         org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
        // CharacterRun run = pr.getCharacterRun(i);
        // run.setBold(true);
        // run.setCapitalized(true);
        // run.setItalic(true);
         paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");  
     System.out.println("Length:" + paragraphs[i].length());  
     System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());  

     // add the paragraph to the document  
     document.add(new Paragraph(paragraphs[i]));  
     }  

     System.out.println("Document testing completed");  
 } catch (Exception e) {  
     System.out.println("Exception during test");  
     e.printStackTrace();  
 } finally {  
                 // close the document  
    document.close();  
             }  
 }  

请帮帮我。

提前谢谢。

【问题讨论】:

    标签: java apache apache-poi hwpf


    【解决方案1】:

    如果您查看 Apache Tika,这里有一个从 HWPF 文档中读取一些样式信息的好示例。 Tika 中的代码根据 HWPF 内容生成 HTML,但您应该会发现非常相似的内容适用于您的案例。

    Tika 类是 https://svn.apache.org/repos/asf/tika/trunk/tika-parsers/src/main/java/org/apache/tika/parser/microsoft/WordExtractor.java

    关于 word 文档需要注意的一点是,任何一个 Character Run 中的所有内容都应用了相同的格式。因此,一个段落由一个或多个字符运行组成。一些样式应用于段落,其他部分在运行时完成。根据您感兴趣的格式,它可能在段落或运行中。

    【讨论】:

      【解决方案2】:

      如果您使用 WordExtractor,您将只获得文本。尝试使用CharacterRun 类。您将获得样式和文本。请参考以下示例代码。

      Range range = doc.getRange();
      for (int i = 0; i < range.numParagraphs(); i++) {
          org.apache.poi.hwpf.usermodel.Paragraph poiPara = range.getParagraph(i);
          int j = 0;
          while (true) {
              CharacterRun run = poiPara.getCharacterRun(j++);
              System.out.println("Color "+run.getColor());
              System.out.println("Font size "+run.getFontSize());
              System.out.println("Font Name "+run.getFontName());
              System.out.println(run.isBold()+" "+run.isItalic()+" "+run.getUnderlineCode());
              System.out.println("Text is "+run.text());
              if (run.getEndOffset() == poiPara.getEndOffset()) {
                  break;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 2012-04-14
        • 2014-01-26
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多