title: DOC/DOCX to PDF conversion using Itext and POI java-improper alignment with embedded picture,underline and table is not working
body: <p>Requirement is to convert the uploaded file(doc,docx,jpg,png and pdf too) into PDF and then to store it in database.i have no issues converting jpg/png/pdf to PDF.Though i am able to generate pdf from doc and docx ,the aligment is not proper like embedded picture sizes are not as such in the input file,font ,underlines ,tables are not working.
  Kindly help me in gettin the proper embedded picture(sometimes i get half images),underline and tables to be written properly in PDF.</p>
  
  <p>I see few lines of coding on achiving the same(which takes care of complete end to end converting) in online by using the below JARS but when those are we dont allowed to use.
  <code>org.apache.poi.xwpf.converter.core-1.0.6.jar
  org.apache.poi.xwpf.converter.pdf-1.0.6.jar
  fr.opensagres.xdocreport.itext.extension-2.0.0.jar</code></p>
  
  <p>Jars used:
  <code>iText 2.1.7
  poi:3.12
  ooxml:3.12
  ooxml-schemas:3.12
  scratchpad:3.12
  xmlbeans:2.3.0</code></p>
  
  <p>Could anyone help me on getting the above issue fixed with the attached code please.<code>public class WordToPdfConverter {
      public static void main(final String[] args) {
  final String worddocLocation = "C:\\Users\\VBUVANES\\Desktop\\Word example with pasted images.docx";
  final String pdfLocation = "C:\\Users\\VBUVANES\\Desktop\\GeneratePDF\\testdocxtopdf_result4.pdf";
          final String type = "docx";
          convertWordToPdf(worddocLocation, pdfLocation, type);
      }</code></p>
  
  <pre><code>public static void convertWordToPdf(final String src, final String desc, final String fileType) {
      try {
          final FileInputStream fs = new FileInputStream(src);
          final XWPFDocument doc = new XWPFDocument(fs);
          final Document pdfdoc = new Document(PageSize.A4, 72, 72, 72, 72);
          final PdfWriter pwriter = PdfWriter.getInstance(pdfdoc,
                  new FileOutputStream(desc));
          pwriter.setInitialLeading(20);
          final List&lt;XWPFParagraph&gt; plist = doc.getParagraphs();
          pdfdoc.open();
          for (int i = 0; i &lt; plist.size(); i++) {
              final XWPFParagraph pa = (XWPFParagraph)plist.get(i);
              final List&lt;XWPFRun&gt; runs = pa.getRuns();
              for (int j = 0; j &lt; runs.size(); j++) {
                  final XWPFRun run = (XWPFRun) runs.get(j);
                  final List&lt;XWPFPicture&gt; piclist = run.getEmbeddedPictures();
                  final Iterator&lt;XWPFPicture&gt; iterator = piclist.iterator();
                  while (iterator.hasNext()) {
                  final XWPFPicture pic = (XWPFPicture) iterator.next();
                      final XWPFPictureData picdata = pic.getPictureData();
                      final byte[] bytepic = picdata.getData();
                      final Image imag = Image.getInstance(bytepic);
                      imag.scaleAbsoluteHeight(40);
                      imag.scaleAbsoluteWidth((imag.getWidth() * 30) / imag.getHeight());
                      pdfdoc.add(imag);
                  }
                  final int color = getCode(run.getColor());
                  Font f = null;
                  if (run.isBold() &amp;&amp; run.isItalic())
                      f = FontFactory.getFont(FontFactory.TIMES_ROMAN,
                              run.getFontSize(), Font.BOLDITALIC,
                          new Color(color));
                  else if (run.isBold())
                      f = FontFactory
                              .getFont(FontFactory.TIMES_ROMAN,
                                      run.getFontSize(), Font.BOLD,
                              new Color(color));
                  else if (run.isItalic())
                      f = FontFactory.getFont(FontFactory.TIMES_ROMAN, run
                          .getFontSize(), Font.ITALIC, new Color(
                              color));
                  else if (run.isStrike())
                      f = FontFactory.getFont(FontFactory.TIMES_ROMAN,
                              run.getFontSize(), Font.STRIKETHRU,
                          new Color(color));
                  else
                      f = FontFactory.getFont(FontFactory.TIMES_ROMAN, run
                          .getFontSize(), Font.NORMAL, new Color(
                              color));
                  final String text = run.getText(-1);
                  byte[] bs;
                  if (text != null) {
                      bs = text.getBytes();
                      final String str = new String(bs, "UTF-8");
                      final Chunk chObj1 = new Chunk(str, f);
                      pdfdoc.add(chObj1);
                  }
  
              }
              pdfdoc.add(new Chunk(Chunk.NEWLINE));
          }
          pdfdoc.close();
      } catch (final Exception e) {
          e.printStackTrace();
      }
  }
  
  public static int getCode(final String code) {
      int colorCode;
      if (code != null)
          colorCode = Long.decode("0x" + code).intValue();
      else
          colorCode = Long.decode("0x000000").intValue();
      return colorCode;
  }
  </code></pre>
  
  <p>}</p>