×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Anthonio Achiduzu
Added: Jan 14, 2022 10:49 AM
Views: 391
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.ha10id.reports.service;
  7.  
  8. import com.itextpdf.io.font.PdfEncodings;
  9. import com.itextpdf.kernel.font.PdfFont;
  10. import com.itextpdf.kernel.font.PdfFontFactory;
  11. import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
  12. import com.itextpdf.kernel.pdf.PdfDocument;
  13. import com.itextpdf.kernel.pdf.PdfWriter;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import org.apache.poi.hwpf.HWPFDocument;
  20. import org.apache.poi.hwpf.extractor.WordExtractor;
  21. import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
  22. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  23.  
  24. import com.itextpdf.layout.Document;
  25. //import static com.lowagie.text.ElementTags.FONT;
  26. import com.lowagie.text.Font;
  27.  
  28. import org.springframework.stereotype.Service;
  29.  
  30. /**
  31.  *
  32.  * @author ha10id
  33.  */
  34. @Service
  35. public class DocToPdfService {
  36.  
  37.     public Boolean convert(String src, String dst) throws FileNotFoundException, IOException {
  38.         String k = null;
  39.         OutputStream fileForPdf = null;
  40.         String fileName = "./test.doc";
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter("./DocToPdf.pdf"));
  42.         //Below Code is for .doc file
  43.         if (fileName.endsWith(".doc")) {
  44.             HWPFDocument doc = new HWPFDocument(new FileInputStream(
  45.                     fileName));
  46.             WordExtractor we = new WordExtractor(doc);
  47.             k = we.getText();
  48. //                fileForPdf = new FileOutputStream(new File(
  49. //                        "./DocToPdf.pdf"));
  50. //                PdfDocument pdfDoc = new PdfDocument(new PdfWriter("./DocToPdf.pdf"));
  51.             we.close();
  52.         } //Below Code for .docx file
  53.         else if (fileName.endsWith(".docx")) {
  54.             XWPFDocument docx = new XWPFDocument(new FileInputStream(fileName));
  55.             // using XWPFWordExtractor Class
  56.             XWPFWordExtractor we = new XWPFWordExtractor(docx);
  57.             k = we.getText();
  58. //                fileForPdf = new FileOutputStream(new File(
  59. //                        "./DocxToPdf.pdf"));
  60. //                PdfDocument pdfDoc = new PdfDocument(new PdfWriter("./DocToPdf.pdf"));
  61.             we.close();
  62.         }
  63. //        PdfFont f1 = PdfFontFactory.createFont(Font.BOLD, PdfEncodings.CP1250, EmbeddingStrategy.PREFER_EMBEDDED);
  64.         try (Document document = new Document(pdfDoc)) {
  65.  
  66.             System.out.println(k);
  67.             document.add(new Paragraph(k, new Font(Font.FontFamily.HELVETICA, 10)));
  68.             document.close();
  69. //            fileForPdf.close();
  70.         }
  71.         return true;
  72.  
  73.     }
  74. }
  75.