|
| 1 | +package app; |
| 2 | + |
| 3 | +import com.itextpdf.io.image.ImageDataFactory; |
| 4 | +import com.itextpdf.kernel.colors.Color; |
| 5 | +import com.itextpdf.kernel.colors.DeviceRgb; |
| 6 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 7 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 8 | +import com.itextpdf.layout.Document; |
| 9 | +import com.itextpdf.layout.element.Image; |
| 10 | +import com.itextpdf.layout.element.Paragraph; |
| 11 | +import com.itextpdf.layout.element.Text; |
| 12 | +import java.io.File; |
| 13 | +import java.io.FileInputStream; |
| 14 | +import java.io.FileNotFoundException; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.OutputStream; |
| 19 | +import java.util.List; |
| 20 | +import java.util.logging.Level; |
| 21 | +import java.util.logging.Logger; |
| 22 | +import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| 23 | +import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
| 24 | +import org.apache.poi.xwpf.usermodel.XWPFPicture; |
| 25 | +import org.apache.poi.xwpf.usermodel.XWPFRun; |
| 26 | + |
| 27 | +public class Converter { |
| 28 | + |
| 29 | + private final static Logger LOGGER = Logger.getLogger(Converter.class.getName()); |
| 30 | + |
| 31 | + public Converter() { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Converts Docx into PDF. |
| 36 | + * |
| 37 | + * @param docIn Path to the Docx |
| 38 | + * @param docOut Path, where the PDF will be saved |
| 39 | + * @throws FileNotFoundException |
| 40 | + * @throws IOException |
| 41 | + */ |
| 42 | + public void convert(String docIn, String docOut) throws FileNotFoundException, IOException { |
| 43 | + OutputStream out; |
| 44 | + try (InputStream in = new FileInputStream(new File(docIn)); |
| 45 | + XWPFDocument document = new XWPFDocument(in)) { |
| 46 | + out = new FileOutputStream(new File(docOut)); |
| 47 | + PdfDocument pdfDocument; |
| 48 | + Document pdfDoc; |
| 49 | + |
| 50 | + PdfWriter writer = new PdfWriter(docOut); |
| 51 | + pdfDocument = new PdfDocument(writer); |
| 52 | + pdfDoc = new Document(pdfDocument); |
| 53 | + List<XWPFParagraph> paragraphs = document.getParagraphs(); |
| 54 | + |
| 55 | + paragraphs.forEach(p -> { |
| 56 | + List<XWPFRun> runs = p.getRuns(); |
| 57 | + Paragraph par = new Paragraph(); |
| 58 | + /* Each chunk (word) can have its own formatting. Therefore each of them will be iterated. */ |
| 59 | + runs.stream().map(run -> { |
| 60 | + // Text formatting |
| 61 | + Text text = formatText(run); |
| 62 | + // Text color |
| 63 | + text = colorText(run, text); |
| 64 | + // Adding images |
| 65 | + try { |
| 66 | + Image image = extractPictures(run); |
| 67 | + if (image != null) { |
| 68 | + par.add(image); |
| 69 | + } |
| 70 | + } catch (IOException ex) { |
| 71 | + LOGGER.log(Level.SEVERE, "Error while processing image", ex); |
| 72 | + } |
| 73 | + |
| 74 | + return text; |
| 75 | + }).forEachOrdered(text -> { |
| 76 | + par.add(text); |
| 77 | + }); |
| 78 | + pdfDoc.add(par); |
| 79 | + }); |
| 80 | + |
| 81 | + pdfDocument.close(); |
| 82 | + pdfDoc.close(); |
| 83 | + } |
| 84 | + out.close(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Provides basic text formatting. |
| 89 | + * |
| 90 | + * @param run |
| 91 | + * @return formatted text |
| 92 | + */ |
| 93 | + private Text formatText(XWPFRun run) { |
| 94 | + Text text = new Text(run.text()); |
| 95 | + if (run.isBold()) { |
| 96 | + text.setBold(); |
| 97 | + } |
| 98 | + if (run.isItalic()) { |
| 99 | + text.setItalic(); |
| 100 | + } |
| 101 | + return text; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Applies color to a text. Handles Exceptions, if no color is provided (e.g. auto). |
| 106 | + * |
| 107 | + * @param run |
| 108 | + * @param text |
| 109 | + * @return colored text |
| 110 | + */ |
| 111 | + private Text colorText(XWPFRun run, Text text) { |
| 112 | + try { |
| 113 | + Color color = hexToRgb(run.getColor()); |
| 114 | + text.setFontColor(color); |
| 115 | + } catch (NumberFormatException ex) { |
| 116 | + LOGGER.log(Level.FINE, ex.toString()); |
| 117 | + } catch (Exception ex) { |
| 118 | + LOGGER.log(Level.FINE, "Color not found", ex); |
| 119 | + } |
| 120 | + return text; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Converts HEX to RGB. |
| 125 | + * |
| 126 | + * @param hex |
| 127 | + * @return |
| 128 | + * @throws NumberFormatException if no color is provided (e.g. auto) |
| 129 | + * @throws Exception if an unexpected exception occurs |
| 130 | + */ |
| 131 | + private Color hexToRgb(String hex) throws NumberFormatException, Exception { |
| 132 | + if (hex != null && hex.length() != 6) { |
| 133 | + throw new NumberFormatException(hex + " is not a color"); |
| 134 | + } |
| 135 | + int r = Integer.valueOf(hex.substring(0, 2), 16); |
| 136 | + int g = Integer.valueOf(hex.substring(2, 4), 16); |
| 137 | + int b = Integer.valueOf(hex.substring(4, 6), 16); |
| 138 | + |
| 139 | + Color rgb = new DeviceRgb(r, g, b); |
| 140 | + return rgb; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Extracts possible images. |
| 145 | + * |
| 146 | + * @param run |
| 147 | + * @return Extracted image |
| 148 | + * @throws IOException |
| 149 | + */ |
| 150 | + private Image extractPictures(XWPFRun run) throws IOException { |
| 151 | + Image image = null; |
| 152 | + if (run.getEmbeddedPictures() != null) { |
| 153 | + for (XWPFPicture picture : run.getEmbeddedPictures()) { |
| 154 | + InputStream is = picture.getPictureData().getPackagePart().getInputStream(); |
| 155 | + image = new Image(ImageDataFactory.create(is.readAllBytes())); |
| 156 | + |
| 157 | + // image settings |
| 158 | + image.setWidth((float) picture.getWidth()); |
| 159 | + |
| 160 | + return image; |
| 161 | + } |
| 162 | + } |
| 163 | + return image; |
| 164 | + } |
| 165 | +} |
0 commit comments