页边距不适用于我的xwpf文档

问题描述

我正在通过apache poi创建一个xwpf文档。我想设置文档的页边距,但是运行代码时,页边距不会改变。我的文档中有表格,但是由于页边距问题,表格的单元格宽度没有变化。我在表中使用列节,并且想要创建此问题答案中的表。 How can I set table row's height different each column in word via apache poi

我的代码在下面

XWPFDocument document= new XWPFDocument();
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageMar pageMar = sectPr.addNewpgmar();
        pageMar.setLeft(BigInteger.valueOf(200L)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5"
        pageMar.setRight(BigInteger.valueOf(1360L));
        pageMar.setTop(BigInteger.valueOf(995L));
        pageMar.setBottom(BigInteger.valueOf(1870L));



          //one column section
          XWPFParagraph paragraph = document.createParagraph();

          //table over full width of one column section
          XWPFTable table = document.createTable();
          table.setWidth("100%");
          XWPFTableRow row = table.getRow(0);
          row.getCell(0).setColor("808080");
          row.getCell(0).getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER);
          row.getCell(0).getParagraphArray(0).setSpacingAfter(0);
          row.getCell(0).getParagraphArray(0).createRun().setText("OTC Request");
          row.getCell(0).getParagraphArray(0).getRuns().get(0).setBold(true);
          row.getCell(0).getParagraphArray(0).getRuns().get(0).setFontFamily("Arial");
          row.getCell(0).getParagraphArray(0).getRuns().get(0).setFontSize(10);
          row.getCell(0).getParagraphArray(0).getRuns().get(0).setColor("FFFFFF");

          //paragraph with section setting for one column section above
          XWPFParagraph paragraph3 = document.createParagraph();
          CTSectPr ctSectPr = paragraph3.getCTP().addNewPPr().addNewSectPr();
          CTColumns ctColumns = ctSectPr.addNewCols();
          ctColumns.setNum(BigInteger.valueOf(1));
          ctColumns.setEqualWidth(STOnOff.TRUE);

          //two column section
          //table over full width of one column in two column section
          XWPFTable table1 = document.createTable();
          table1.setWidth("100%");
          XWPFTableRow row_1 = table1.getRow(0);
          XWPFTableCell cell0_row_1=row_1.getCell(0);
          XWPFParagraph parag_general=cell0_row_1.addParagraph();
          parag_general.setSpacingAfter(0);
          XWPFRun run_general=parag_general.createRun();
          run_general.setBold(true);
          run_general.setFontFamily("Arial");
          run_general.setFontSize(10);
          run_general.setText("GENERAL @R_486_4045@ION");
          cell0_row_1.removeParagraph(0);
          //row1
          XWPFTableRow row1=table1.createRow();
          XWPFTableCell cell0_row1=row1.getCell(0);
          XWPFParagraph parag_cell0=cell0_row1.addParagraph();
          parag_cell0.setSpacingAfter(0);
          XWPFRun run_cell0=parag_cell0.createRun();
          run_cell0.setFontFamily("Arial");
          run_cell0.setFontSize(10);
          run_cell0.setText("1) Requestor*");
          run_cell0.addBreak();
          run_cell0.setText("PW Turkish Engine Center");
          cell0_row1.removeParagraph(0);
          //row2
          XWPFTableRow row2=table1.createRow();
          XWPFTableCell cell0_row2=row2.getCell(0);
          XWPFParagraph parag1_cell0=cell0_row2.addParagraph();
          parag1_cell0.setSpacingAfter(0);
          XWPFRun run1_cell0=parag1_cell0.createRun();
          run1_cell0.setFontFamily("Arial");
          run1_cell0.setFontSize(10);
          run1_cell0.setText("2) Maintenance Center * / Request Number");
          run1_cell0.addBreak();
          run1_cell0.setText("TEC-2020-(EVX2852-02)-01");
          cell0_row2.removeParagraph(0);
          //row3
          XWPFTableRow row3=table1.createRow();
          XWPFTableCell cell0_row3=row3.getCell(0);
          XWPFParagraph parag2_cell0=cell0_row3.addParagraph();
          parag2_cell0.setSpacingAfter(0);
          XWPFRun run2_cell0=parag2_cell0.createRun();
          run2_cell0.setFontFamily("Arial");
          run2_cell0.setFontSize(10);
          run2_cell0.setText("3) Date Requested * (Day-Month-Year)");
          run2_cell0.addBreak();
          run2_cell0.setText("16-04-20");
          cell0_row3.removeParagraph(0);
          //row4
          XWPFTableRow row4=table1.createRow();
          XWPFTableCell cell0_row4=row4.getCell(0);
          XWPFParagraph parag3_cell0=cell0_row4.addParagraph();
          parag3_cell0.setSpacingAfter(0);
          XWPFRun run3_cell0=parag3_cell0.createRun();
          run3_cell0.setFontFamily("Arial");
          run3_cell0.setFontSize(10);
          run3_cell0.setText("4) OTC Need Date * ");
          run3_cell0.addBreak();
          run3_cell0.setText("28-04-2020");
          cell0_row4.removeParagraph(0);
          //row5
          XWPFTableRow row5=table1.createRow();
          XWPFTableCell cell0_row5=row5.getCell(0);
          XWPFParagraph parag4_cell0=cell0_row5.addParagraph();
          parag4_cell0.setSpacingAfter(0);
          XWPFRun run4_cell0=parag4_cell0.createRun();
          run4_cell0.setFontFamily("Arial");
          run4_cell0.setFontSize(10);
          run4_cell0.setText("5) Engine Induction Date * ");
          run4_cell0.addBreak();
          run4_cell0.setText("12-09-2018");
          cell0_row5.removeParagraph(0);
          //row6
          XWPFTableRow row6=table1.createRow();
          XWPFTableCell cell0_row6=row6.getCell(0);
          XWPFParagraph parag5_cell0=cell0_row6.addParagraph();
          parag5_cell0.setSpacingAfter(0);
          XWPFRun run5_cell0=parag5_cell0.createRun();
          run5_cell0.setFontFamily("Arial");
          run5_cell0.setFontSize(10);
          run5_cell0.setText("6) Kitting Date * ");
          run5_cell0.addBreak();
          run5_cell0.setText("20-05-2020");
          cell0_row6.removeParagraph(0);
          //row7
          XWPFTableRow row7=table1.createRow();
          XWPFTableCell cell0_row7=row7.getCell(0);
          XWPFParagraph parag6_cell0=cell0_row7.addParagraph();
          parag6_cell0.setSpacingAfter(0);
          XWPFRun run6_cell0=parag6_cell0.createRun();
          run6_cell0.setFontFamily("Arial");
          run6_cell0.setFontSize(10);
          run6_cell0.setText("7) Engine Ship Date * ");
          run6_cell0.addBreak();
          run6_cell0.setText("18-06-2020");
          cell0_row7.removeParagraph(0);
          //row8
          XWPFTableRow row8=table1.createRow();
          XWPFTableCell cell0_row8=row8.getCell(0);
          cell0_row8.removeParagraph(0);
          XWPFParagraph parag7_cell0=cell0_row8.addParagraph();
          parag7_cell0.setSpacingAfter(0);
          XWPFRun run7_cell0=parag7_cell0.createRun();
          run7_cell0.setFontFamily("Arial");
          run7_cell0.setFontSize(10);
          run7_cell0.setBold(true);
          run7_cell0.setText("ENGINE OWNER");
          //row9
          XWPFTableRow row9=table1.createRow();
          XWPFTableCell cell0_row9=row9.getCell(0);
          XWPFParagraph parag8_cell0=cell0_row9.addParagraph();
          parag8_cell0.setSpacingAfter(0);
          XWPFRun run8_cell0=parag8_cell0.createRun();
          run8_cell0.setFontFamily("Arial");
          run8_cell0.setFontSize(10);
          run8_cell0.setText("8) Operator / (Lessor - if applicable) *");
          run8_cell0.addBreak();
          run8_cell0.setText("THY A.O.");
          cell0_row9.removeParagraph(0);
          //row10
          XWPFTableRow row10=table1.createRow();
          XWPFTableCell cell0_row10=row10.getCell(0);
          XWPFParagraph parag9_cell0=cell0_row10.addParagraph();
          parag9_cell0.setSpacingAfter(0);
          XWPFRun run9_cell0=parag9_cell0.createRun();
          run9_cell0.setFontFamily("Arial");
          run9_cell0.setFontSize(10);
          run9_cell0.setText("9) Leased Asset * (Yes/No)");
          run9_cell0.addBreak();
          run9_cell0.setText("NO");
          cell0_row10.removeParagraph(0);
          //row11
          XWPFTableRow row11=table1.createRow();
          XWPFTableCell cell0_row11=row11.getCell(0);
          XWPFParagraph parag10_cell0=cell0_row11.addParagraph();
          parag10_cell0.setSpacingAfter(0);
          XWPFRun run10_cell0=parag10_cell0.createRun();
          run10_cell0.setFontFamily("Arial");
          run10_cell0.setFontSize(10);
          run10_cell0.setText("10) Under IAE Contract [FHA/FPA] * (Yes/No)");
          run10_cell0.addBreak();
          run10_cell0.setText("NO");
          cell0_row11.removeParagraph(0);
          //row12
          XWPFTableRow row12=table1.createRow();
          XWPFTableCell cell0_row12=row12.getCell(0);
          XWPFParagraph parag11_cell0=cell0_row12.addParagraph();
          parag11_cell0.setSpacingAfter(0);
          XWPFRun run11_cell0=parag11_cell0.createRun();
          run11_cell0.setFontFamily("Arial");
          run11_cell0.setFontSize(10);
          run11_cell0.setText("       OTC Ref.: "+"N/A");
          cell0_row12.removeParagraph(0);
          //row13
          XWPFTableRow row13=table1.createRow();
          XWPFTableCell cell0_row13=row13.getCell(0);
          XWPFParagraph parag12_cell0=cell0_row13.addParagraph();
          parag12_cell0.setSpacingAfter(0);
          XWPFRun run12_cell0=parag12_cell0.createRun();
          run12_cell0.setFontFamily("Arial");
          run12_cell0.setFontSize(10);
          run12_cell0.setBold(true);
          run12_cell0.setText("ENGINE @R_486_4045@ION");
          cell0_row13.removeParagraph(0);
          //row14
          XWPFTableRow row14=table1.createRow();
          XWPFTableCell cell0_row14=row14.getCell(0);
          XWPFParagraph parag13_cell0=cell0_row14.addParagraph();
          parag13_cell0.setSpacingAfter(0);
          XWPFRun run13_cell0=parag13_cell0.createRun();
          run13_cell0.setFontFamily("Arial");
          run13_cell0.setFontSize(10);
          run13_cell0.setText("11) Engine Model *");
          run13_cell0.addBreak();
          run13_cell0.setText("V2524-SQ03");
          cell0_row14.removeParagraph(0);
          //row15
          XWPFTableRow row15=table1.createRow();
          XWPFTableCell cell0_row15=row15.getCell(0);
          XWPFParagraph parag14_cell0=cell0_row15.addParagraph();
          parag14_cell0.setSpacingAfter(0);
          XWPFRun run14_cell0=parag14_cell0.createRun();
          run14_cell0.setFontFamily("Arial");
          run14_cell0.setFontSize(10);
          run14_cell0.setText("12) Engine Serial Number *");
          run14_cell0.addBreak();
          run14_cell0.setText("VX2852");
          cell0_row15.removeParagraph(0);
          //row16
          XWPFTableRow row16=table1.createRow();
          XWPFTableCell cell0_row16=row16.getCell(0);
          XWPFParagraph parag15_cell0=cell0_row16.addParagraph();
          parag15_cell0.setSpacingAfter(0);
          XWPFRun run15_cell0=parag15_cell0.createRun();
          run15_cell0.setFontFamily("Arial");
          run15_cell0.setFontSize(10);
          run15_cell0.setText("13) Engine TSN / CSN *");
          run15_cell0.addBreak();
          run15_cell0.setText("38848:02 / 18599");
          cell0_row16.removeParagraph(0);
          //row18
          XWPFTableRow row18=table1.createRow();
          XWPFTableCell cell0_row18=row18.getCell(0);
          XWPFParagraph parag17_cell0=cell0_row18.addParagraph();
          parag17_cell0.setSpacingAfter(0);
          XWPFRun run17_cell0=parag17_cell0.createRun();
          run17_cell0.setFontFamily("Arial");
          run17_cell0.setFontSize(10);
          run17_cell0.setText("14) Is Engine Off Wing (Yes/No)");
          run17_cell0.addBreak();
          run17_cell0.setText("YES");
          cell0_row18.removeParagraph(0);
          //row19
          XWPFTableRow row19=table1.createRow();
          XWPFTableCell cell0_row19=row19.getCell(0);
          XWPFParagraph parag18_cell0=cell0_row19.addParagraph();
          parag18_cell0.setSpacingAfter(0);
          XWPFRun run18_cell0=parag18_cell0.createRun();
          run18_cell0.setFontFamily("Arial");
          run18_cell0.setFontSize(10);
          run18_cell0.setBold(true);
          run18_cell0.setText("PART @R_486_4045@ION");
          cell0_row19.removeParagraph(0);
          //row20
          XWPFTableRow row20=table1.createRow();
          XWPFTableCell cell0_row20=row20.getCell(0);
          XWPFParagraph parag19_cell0=cell0_row20.addParagraph();
          parag19_cell0.setSpacingAfter(0);
          XWPFRun run19_cell0=parag19_cell0.createRun();
          run19_cell0.setFontFamily("Arial");
          run19_cell0.setFontSize(10);
          run19_cell0.setText("15) Part Name / Description *");
          run19_cell0.addBreak();
          run19_cell0.setText("LPC - BLEED DUCT");
          cell0_row20.removeParagraph(0);
          //row21
          XWPFTableRow row21=table1.createRow();
          XWPFTableCell cell0_row21=row21.getCell(0);
          XWPFParagraph parag20_cell0=cell0_row21.addParagraph();
          parag20_cell0.setSpacingAfter(0);
          XWPFRun run20_cell0=parag20_cell0.createRun();
          run20_cell0.setFontFamily("Arial");
          run20_cell0.setFontSize(10);
          run20_cell0.setText("16) Part Number *");
          run20_cell0.addBreak();
          run20_cell0.setText("5W2318");
          cell0_row21.removeParagraph(0);
          //row22
          XWPFTableRow row22=table1.createRow();
          XWPFTableCell cell0_row22=row22.getCell(0);
          XWPFParagraph parag21_cell0=cell0_row22.addParagraph();
          parag21_cell0.setSpacingAfter(0);
          XWPFRun run21_cell0=parag21_cell0.createRun();
          run21_cell0.setFontFamily("Arial");
          run21_cell0.setFontSize(10);
          run21_cell0.setText("17) Part Serial Number * (if serialized)");
          run21_cell0.addBreak();
          run21_cell0.setText("JM2532F");
          cell0_row22.removeParagraph(0);
          //row23
          XWPFTableRow row23=table1.createRow();
          XWPFTableCell cell0_row23=row23.getCell(0);
          XWPFParagraph parag22_cell0=cell0_row23.addParagraph();
          parag22_cell0.setSpacingAfter(0);
          XWPFRun run22_cell0=parag22_cell0.createRun();
          run22_cell0.setFontFamily("Arial");
          run22_cell0.setFontSize(10);
          run22_cell0.setText("18) Part TSN / CSN * (LLP's / Modules)");
          run22_cell0.addBreak();
          run22_cell0.setText("N/A / N/A");
          cell0_row23.removeParagraph(0);
          //row24
          XWPFTableRow row24=table1.createRow();
          XWPFTableCell cell0_row24=row24.getCell(0);
          XWPFParagraph parag23_cell0=cell0_row24.addParagraph();
          parag23_cell0.setSpacingAfter(0);
          XWPFRun run23_cell0=parag23_cell0.createRun();
          run23_cell0.setFontFamily("Arial");
          run23_cell0.setFontSize(10);
          run23_cell0.setText("19) IPC figure / Item Reference");
          run23_cell0.addBreak();
          run23_cell0.setText("01-250");
          cell0_row24.removeParagraph(0);
          //row25
          XWPFTableRow row25=table1.createRow();
          XWPFTableCell cell0_row25=row25.getCell(0);
          XWPFParagraph parag24_cell0=cell0_row25.addParagraph();
          parag24_cell0.setSpacingAfter(0);
          XWPFRun run24_cell0=parag24_cell0.createRun();
          run24_cell0.setFontFamily("Arial");
          run24_cell0.setFontSize(10);
          run24_cell0.setText("20) Part Cost * (OTC Threshold=$25,000 USD)");
          run24_cell0.addBreak();
          run24_cell0.setText("118230");
          cell0_row25.removeParagraph(0);
          //row26
          XWPFTableRow row26=table1.createRow();
          XWPFTableCell cell0_row26=row26.getCell(0);
          XWPFParagraph parag25_cell0=cell0_row26.addParagraph();
          parag25_cell0.setSpacingAfter(0);
          XWPFRun run25_cell0=parag25_cell0.createRun();
          run25_cell0.setFontFamily("Arial");
          run25_cell0.setFontSize(10);
          run25_cell0.setText("21) Logistical / Financial Impact if OTC is Rejected");
          run25_cell0.addBreak();
          run25_cell0.setText("118230");
          cell0_row26.removeParagraph(0);

          //paragraph with section break continuous for two column section above
          XWPFParagraph paragraph1 = document.createParagraph();
          ctSectPr = paragraph1.getCTP().addNewPPr().addNewSectPr();
          ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
          ctColumns = ctSectPr.addNewCols();
          ctColumns.setNum(BigInteger.valueOf(2));
          ctColumns.setEqualWidth(STOnOff.X_1);

          //one column section again
          //table over full width of one column section
          
 
          XWPFTable table2 = document.createTable();
          table2.setWidth("100%");
          XWPFTableRow row27=table2.getRow(0);
          XWPFTableCell cell0_row27=row27.getCell(0);
          XWPFParagraph parag26_cell0=cell0_row27.addParagraph();
          parag26_cell0.setSpacingAfter(0);
          XWPFRun run26_cell0=parag26_cell0.createRun();
          run26_cell0.setFontFamily("Arial");
          run26_cell0.setBold(true);
          run26_cell0.setFontSize(10);
          run26_cell0.setText("DEVIATION @R_486_4045@ION");
          cell0_row27.removeParagraph(0);
          

          //section setting continuous for one column section above and whole document from here on
          XWPFParagraph paragraph2 = document.createParagraph();
          ctSectPr = paragraph2.getCTP().addNewPPr().addNewSectPr();
          ctSectPr.addNewType().setVal(STSectionMark.CONTINUOUS);
          ctColumns = ctSectPr.addNewCols();
          ctColumns.setNum(BigInteger.valueOf(1));

          
          FileOutputStream out = new FileOutputStream("C:\\Users\\ftk1187\\Desktop\\otc.docx");  
          document.write(out);
          out.close();
          document.close();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)