title: repetitive header/footer issue on creating ms word document using apache poi
body: <p>I have a project which creating ms word document. In this word document, I want to create header/footer. To do this I wrote this codes</p>
  
  <pre><code>public static void header_footer(String SalesOrder,String type) throws InvalidFormatException, IOException
      {
  
          XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();   
  
            if (headerFooterPolicy == null) 
                headerFooterPolicy = document.createHeaderFooterPolicy();
  
                XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
                XWPFTable asd= header.createTable(1, 2);
                asd.setTableAlignment(TableRowAlign.LEFT);
                XWPFTableRow row;
                XWPFTableCell cell,cell2;
                CTHeight ht;
                CTTrPr trPr;
                CTTblWidth cellWidth;
                row= asd.getRow(0);
                trPr = row.getCtRow().addNewTrPr();
                ht = trPr.addNewTrHeight();
                ht.setVal(BigInteger.valueOf(-1070));         
                cell=row.getCell(0);        
  
                XWPFParagraph parag=document.createParagraph();
                parag = cell.getParagraphArray(0);
                XWPFRun r1=parag.createRun();
  
  
                InputStream pic = access_connection.class.getResourceAsStream("page1_header.png");
  
  
                r1.addPicture(pic, XWPFDocument.PICTURE_TYPE_PNG, imgFile2, Units.toEMU(160), Units.toEMU(55)); 
  
  
                  cellWidth = cell.getCTTc().addNewTcPr().addNewTcW();            
                  cellWidth.setW(BigInteger.valueOf(3500));
                  cell2=row.getCell(1);
                  cellWidth = cell2.getCTTc().addNewTcPr().addNewTcW();           
                  cellWidth.setW(BigInteger.valueOf((short)20000));
  
  
              parag=cell2.getParagraphArray(0);
              parag.setVerticalAlignment(TextAlignment.CENTER);
              parag.setAlignment(ParagraphAlignment.CENTER);
              cell2.setVerticalAlignment(XWPFVertAlign.CENTER);
              XWPFRun r2=parag.createRun();
              r2.setFontSize(14);
              r2.setFontFamily("Calibri");
              r2.setText("TURKISH ENGINE CENTER SHOP VISIT REPORT");
              r2.addBreak();
              r2.setText("ESN "+SalesOrder.substring(1, 7)+"                         Istanbul,TURKEY");
  
              XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
              XWPFParagraph parag2= footer.createParagraph();
              XWPFRun run3=parag2.createRun();
              XWPFRun run4=parag2.createRun();
              run3.setFontFamily("Arial");
              run4.setFontFamily("Arial");
              run3.setBold(true);
              run4.setItalic(true);
              run3.setFontSize(8);
              run4.setFontSize(9);
              run3.setText("U.S. Export Classification: EAR ECCN 9E991");
              run3.addBreak();
              run4.setText("Printed by TEC Local EVM System");
              XWPFParagraph parag3= footer.createParagraph();
              XWPFRun run5=parag3.createRun();
              parag3.setAlignment(ParagraphAlignment.RIGHT);
              run5.setFontSize(7);
              run5.setFontFamily("Arial");
              run5.setText("Page ");
              run5.getCTR().addNewPgNum();
  
  //          footer.setCommited(true);
  
  
  
  
          try {
  
  
                  FileOutputStream input = new FileOutputStream(word_saveas_file+SalesOrder+"_"+type+"_Report.docx");
  
                  document.write(input);
                  input.close();  
  //              input.flush();
  
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>I'm running my project from .jsp web page on tomcat server. When I run my project first time, everything running correctly. But when I run my project second time without stopping or restarting tomcat server, header/footer is repeating. I don't understand why only header/footer repeating. I'm creating ms word documents which are different names but still have this problem.</p>
  
  <pre><code>public static void Start(String SalesOrder, String path,String type) throws SQLException, ClassNotFoundException {
  
        baslik[0]="ATA";  
        baslik[1]="LID";
        baslik[2]="PN";
        baslik[3]="QPE";
        baslik[4]="SV";
        baslik[5]="IS";
        baslik[6]="RS";
        baslik[7]="REP";
        baslik[8]="Defect";
  
        baslik2[0]="PN";
        baslik2[1]="NEW";
        baslik2[2]="REP";
        baslik2[3]="SV";
        baslik2[4]="Information Only";
  
       try {
          start_page_1(SalesOrder,path,type);
      } catch (InvalidFormatException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
           try {
              start_page_2(SalesOrder,type);
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          try {
              header_footer(SalesOrder,type);
          } catch (InvalidFormatException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          eng_general(SalesOrder);
          oil_sys(SalesOrder);
          GroupName(1);
          partoff(SalesOrder,type);
  
      //baglanti= DriverManager.getConnection("jdbc:ucanaccess://"+db_file,db_username,db_password);
        //sorgu=baglanti.createStatement();
       //deneme=sorgu.executeQuery("SELECT * FROM TableRun");
          System.out.println("Process Completed");
  
  
      }
  public static void Start_PDM(String SalesOrder, String path,String type) throws SQLException, ClassNotFoundException, IOException {
  
        baslik[0]="ATA";  
        baslik[1]="LID";
        baslik[2]="PN";
        baslik[3]="QPE";
        baslik[4]="SV";
        baslik[5]="IS";
        baslik[6]="RS";
        baslik[7]="REP";
        baslik[8]="Defect";
  
        baslik2[0]="PN";
        baslik2[1]="NEW";
        baslik2[2]="REP";
        baslik2[3]="SV";
        baslik2[4]="Information Only";
  
       try {
          start_page_1(SalesOrder,path,type);
      } catch (InvalidFormatException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
           try {
              start_page_2(SalesOrder,type);
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          try {
              header_footer(SalesOrder,type);
          } catch (InvalidFormatException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          GroupName(1);
          partoff(SalesOrder,type);
          /*try {
              Runtime.getRuntime().exec("taskkill /F /IM java.exe");
              Runtime.getRuntime().exec("net start java");
              System.exit(0);
          } catch (Throwable e) {
  
          }   */
          baglanti.close();
  
      //baglanti= DriverManager.getConnection("jdbc:ucanaccess://"+db_file,db_username,db_password);
        //sorgu=baglanti.createStatement();
       //deneme=sorgu.executeQuery("SELECT * FROM TableRun");
          System.out.println("Process Completed");
  
  
      }
  </code></pre>
  
  <p>I'm using first method for create word document with <code>eng_general</code> and <code>oil_sys</code> methods. In the second method I'm creating word document without <code>eng_general</code> and <code>oil_sys</code>.</p>