Bug 64801 - How to set headers in landscape using java apache poi
Summary: How to set headers in landscape using java apache poi
Status: NEEDINFO
Alias: None
Product: POI
Classification: Unclassified
Component: XWPF (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-12 02:46 UTC by Dilipkumar
Modified: 2020-10-27 04:49 UTC (History)
0 users



Attachments
Headers not coming once landscape page come while generating dynamic document (142.81 KB, application/vnd.openxmlformats-officedocument.wordprocessingml.document)
2020-10-12 02:46 UTC, Dilipkumar
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dilipkumar 2020-10-12 02:46:56 UTC
Created attachment 37494 [details]
Headers not coming once landscape page come while generating dynamic document

HI I am generating dynamic .docx document from another document butheaders for landcape page not coming .Below I have attached .docx file for your reference please find and guide me in stright path
Comment 1 Dominik Stadler 2020-10-25 04:16:33 UTC
Please provide the minimal java-code that produces the document. The resulting document alone does not help much in analyzing.
Comment 2 Dilipkumar 2020-10-27 04:49:36 UTC
(In reply to Dominik Stadler from comment #1)
> Please provide the minimal java-code that produces the document. The
> resulting document alone does not help much in analyzing.


Hi Dominik ,

Here I have provided method to generate header and Footer code when landscape coming while generating dynamic docx it doesn't support for us

 private void addHeaderFooter(XWPFDocument document) {
        try {
            /**
             * Header *
             */
            CTSectPr sectPr = document.getDocument().getBody().getSectPr();
            if (sectPr == null)
                sectPr = document.getDocument().getBody().addNewSectPr();
            XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
            XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
            XWPFParagraph[] parsHeader = new XWPFParagraph[1];
            XWPFParagraph paragraph = header.getParagraphArray(0);
            if (paragraph == null) {
                paragraph = header.createParagraph();
            }
            paragraph.setAlignment(ParagraphAlignment.LEFT);

            CTP ctParagraph = paragraph.getCTP();
            CTPPr paragraphProperties = ctParagraph.addNewPPr();
            CTJc ctJustification = paragraphProperties.addNewJc();
            ctJustification.setVal(STJc.LEFT);

            CTR leftHeader = ctParagraph.addNewR();
            CTText leftHeaderText = leftHeader.addNewT();
            leftHeaderText.setStringValue(sponsor);

            CTPTab rightTab = paragraph.createRun().getCTR().addNewPtab();
            rightTab.setRelativeTo(STPTabRelativeTo.MARGIN);
            rightTab.setAlignment(STPTabAlignment.RIGHT);
            rightTab.setLeader(STPTabLeader.NONE);
            paragraph.getCTP().addNewR().addNewT().setStringValue(protocol);
            parsHeader[0] = paragraph;

            document = paragraph.getDocument();
            int pos = document.getPosOfParagraph(paragraph);
            document.removeBodyElement(pos);
            /**
             * Footer *
             */
            Date date = new Date();
            String footerText = "Confidential";// "Created " + new SimpleDateFormat("yyyy-MM-dd").format(date);

            XWPFFooter footer = policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
            XWPFParagraph footerParagraph = footer.getParagraphArray(0);
            if (footerParagraph == null) {
                footerParagraph = footer.createParagraph();
            }
            footerParagraph.setAlignment(ParagraphAlignment.LEFT);

            ctParagraph = footerParagraph.getCTP();
            paragraphProperties = ctParagraph.addNewPPr();
            ctJustification = paragraphProperties.addNewJc();
            ctJustification.setVal(STJc.LEFT);

            CTR leftFooter = ctParagraph.addNewR();
            CTText leftFooterText = leftFooter.addNewT();
            leftFooterText.setStringValue(footerText);

            rightTab = footerParagraph.createRun().getCTR().addNewPtab();
            rightTab.setRelativeTo(STPTabRelativeTo.MARGIN);
            rightTab.setAlignment(STPTabAlignment.RIGHT);
            rightTab.setLeader(STPTabLeader.NONE);

            footerParagraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");
            footerParagraph.createRun().setText(" of ");
            footerParagraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");

            XWPFParagraph[] parsFooter = new XWPFParagraph[1];
            parsFooter[0] = footerParagraph;
            policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }