从HWPFDocument转换为XWPFDocument,反之亦然

时间:2014-12-19 08:15:15

标签: apache-poi

我有一个Word文档,我必须用PDF格式替换文本并将此文档保存在Oracle数据库中。有人知道如何解决这个问题吗?

String inputFilename = "/root/GeneratorUmow/web/WEB-INF/umowy/kkb/wniosekozaciag.docx";
   try(InputStream is = new FileInputStream(inputFilename)) {
      dbd = new DokumentyBlobDAO();
      db.setNazwaPliku("Wniosek_o_zaciagniecie.pdf");
      db.setTypDokuemntu(876990);
      if(du.getTypKlienta() == S_ID_Lead_Partner_Typ_Klienta_KKB) { 
         POIFSFileSystem fs = null;            
         fs = new POIFSFileSystem(is);
         HWPFDocument doc = new HWPFDocument(fs); //funkcja zamyka inputstream
         Range range = doc.getRange();
         range.replaceText("my&&nazwa&&firmy", "KAKAOWY SZATAN");
         //konwersja na pdf
                FileOutputStream fos = new FileOutputStream(pathToDirectory + File.separator + folderName+File.separator + "wniosekzaciagniecie.docx");
                doc.write(fos);


                String inputChangeFile = pathToDirectory+ File.separator +folderName+File.separator+"wniosekzaciagniecie.docx";
                InputStream inputstream = new FileInputStream(inputFilename);
                length = inputChangeFile.length();
                System.out.println("dlugosc lancucha " + length);

                XWPFDocument document = new XWPFDocument(inputstream);            
                System.out.println("message3");
                PdfOptions options = PdfOptions.create();
                PdfOptions options = PdfOptions.create().fontEncoding("windows-1250");
                System.out.println("message4");
                OutputStream out = new FileOutputStream(new File(pathToDirectory+ File.separator +folderName+File.separator+"kakaowyszal.pdf"));
                PdfConverter.getInstance().convert(document, out, options);
                System.out.println("message5");



            }
        }
        catch( Exception ex){
            System.out.println("err"  + ex);
            return -1;
        }

0 个答案:

没有答案