title: Converting word(.docx) to pdf using Apache POI and itext
body: <p>In my java web application i have a <strong>Persian</strong> template word(docx) document as a contract that customize it for my users whit their data using <strong>APACHE-POI</strong> and after that i have to convert it to pdf in order to prevent the file from being distorted by the operator.
  I tried to convert it using <strong>itext</strong> but i couldnt get success and couldnt find something helpful, can someone suggest a way for doing the conversion using itext or can anyone tell me if there is any other way to prevent the file from being distorted without doing conversion?</p>
  
  <p>Edit note:I did the conversion whit the following codes below but now i have lots of question marks in my pdf file, can anyone help??does itext supports persian or RTL languages?how chan i solve this problem?
  I am using version 5.0.6 of iText!!</p>
  
  <pre><code>convertWordToPdf("D:/PrivateBanking/docxCo.docx","D:/PrivateBanking/docxCo.pdf");
  
  public static void convertWordToPdf(String src, String desc){
          try{
              //create file inputstream object to read data from file
              FileInputStream fs=new FileInputStream(src);
              //create document object to wrap the file inputstream object
              XWPFDocument doc=new XWPFDocument(fs);
              //72 units=1 inch
              Document pdfdoc=new Document(PageSize.A4,72,72,72,72);
              //create a pdf writer object to write text to mypdf.pdf file
              PdfWriter pwriter=PdfWriter.getInstance(pdfdoc, new FileOutputStream(desc));
              //specify the vertical space between the lines of text
              pwriter.setInitialLeading(20);
              //get all paragraphs from word docx
              List&lt;XWPFParagraph&gt; plist=doc.getParagraphs();
  
              //open pdf document for writing
              pdfdoc.open();
              for (int i = 0; i &lt; plist.size(); i++) {
                  //read through the list of paragraphs
                  XWPFParagraph pa = plist.get(i);
                  //get all run objects from each paragraph
                  List&lt;XWPFRun&gt; runs = pa.getRuns();
                  //read through the run objects
                  for (int j = 0; j &lt; runs.size(); j++) {
                      XWPFRun run=runs.get(j);
                      //get pictures from the run and add them to the pdf document
                      List&lt;XWPFPicture&gt; piclist=run.getEmbeddedPictures();
                      //traverse through the list and write each image to a file
                      Iterator&lt;XWPFPicture&gt; iterator=piclist.iterator();
                      while(iterator.hasNext()){
                          XWPFPicture pic=iterator.next();
                          XWPFPictureData picdata=pic.getPictureData();
                          byte[] bytepic=picdata.getData();
                          Image imag=Image.getInstance(bytepic);
                          pdfdoc.add(imag);
  
                      }
                      //get color code
                      int color=getCode(run.getColor());
                      //construct font object
                      Font f=null;
                      if(run.isBold() &amp;&amp; run.isItalic())
                          f= FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.BOLDITALIC, new BaseColor(color));
                      else if(run.isBold())
                          f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.BOLD, new BaseColor(color));
                      else if(run.isItalic())
                          f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.ITALIC, new BaseColor(color));
                      else if(run.isStrike())
                          f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.STRIKETHRU, new BaseColor(color));
                      else
                          f=FontFactory.getFont(FontFactory.TIMES_ROMAN,run.getFontSize(),Font.NORMAL, new BaseColor(color));
                      //construct unicode string
                      String text=run.getText(-1);
                      byte[] bs;
                      if (text!=null){
                          bs=text.getBytes();
                          String str=new String(bs,"UTF-8");
                          //add string to the pdf document
                          Chunk chObj1=new Chunk(str,f);
                          pdfdoc.add(chObj1);
                      }
  
                  }
                  //output new line
                  pdfdoc.add(new Chunk(Chunk.NEWLINE));
              }
              //close pdf document
              pdfdoc.close();
          }catch(Exception e){e.printStackTrace();}
      }
  
   public static int getCode(String code){
          int colorCode;
          if(code!=null)
              colorCode=Long.decode("0x"+code).intValue();
          else
              colorCode=Long.decode("0x000000").intValue();
          return colorCode;
      }
  </code></pre>