If two sections are on the same page, first section does not have a footer, but we want to add the footer to the second section(on the same page), how could we achieve this with Aspose words java?
@cvsformulary Please see our documentation to learn how to work with headers/footers:
https://docs.aspose.com/words/java/working-with-headers-and-footers/
Header/footer are defined per section in MS Word document, so you can simply create the required header/footer in any section and fill it with required data.
My question was around two sections being on the SAME page, first section does not have headers while the second one has the headers, currently second section’s headers do not display because I believe they get driven by the first section being on the same page, I have tried the linkToPrevious option with both true/false values, I have also tried copying headers across the sections, tried explicitly inserting an header to the second section, none of this seems to work, if you have something that could show this, an example available anywhere, would be a great help, Thankyou!
@cvsformulary, if a page has 2 sections then footer of the first section will displayed on this page.
This code example adds a footer to the second section:
Document doc = new Document("sections.docx");
// Get section 2.
Section section = doc.getSections().get(1);
// Create new footer and add it to section 2.
var footerType = HeaderFooterType.FOOTER_PRIMARY;
HeaderFooter footer = new HeaderFooter(doc, footerType);
section.getHeadersFooters().add(footer);
// Add a text to the created footer.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(section.getBody().getFirstParagraph());
builder.moveToHeaderFooter(footerType);
builder.writeln("Section2 footer " + HeaderFooterType.getName(footerType));
doc.save("out.docx");
sections.docx (13.4 KB)
out.docx (11.3 KB)
As you can see on the screenshot, page 2 has 2 sections, but Microsoft Word shows that footer of the section 1 is shown on page 2 (marked in red). The footer for section 2 is displayed only on page 3.
@alexey.noskov Thankyou for your time and help, I appreciate this so much!
So it looks like we cannot display the footer of section2 on the same page where it started, because there was another section(section 1) which has no footers, section 2 footers are only starting from the next page where it is sort of the first section starting. This is where I was confused as the requirement was to show the footer of second section on this page 2 which clearly word itself is not doing and it starts from page 3 rather.
Am I understanding this correct, I have spent enough time on this and I think its about time to consider it as out of the box unsupported functionality.
Thankyou!
@cvsformulary, Yes, you are correct. Aspose.Words can add the footer to section 2 on the same page, but Microsoft Word does not display it. In this case Aspose.Words cannot control how Microsoft Word lays out documents.
@denis.shvydkiy @alexey.noskov
One last time, per current requirement I have two sections on a report, for example preface and drug sections, sometimes prefaces goes over to say 2 and half pages, and we start drug section from the same page where the preface sections ends, so they are sharing the same page, per my research we cannot show the footer for only the last page of a section, would you confirm? I have the pages for each sections calculated but is there a way to know that if two sections are sharing the same page, basically is there a way to check if a particular page(not the document) has multiple sections? I am trying to see if there is a way to figure out for that last page where preface ends and drug section starts, if know that this page has these two sections, go ahead and add the footer, it does not seem to work currently because adding it to preface section will put it in all preface pages, which we dont want, we only want to start the footer from where the drug section starts(we cant start it from new page). SO i was thinking if they share the same page and if there is an option to add to that preface section but ONLY to the last page, Is that something you guys think worth looking into? It hasn’t worked for me yet, but just exploring every avenue before giving up on this.
Once again thankyou for your time and help!
@cvsformulary, Yes, if a page has the preface section followed by the drug section, Microsoft Word will show the footer of the preface section on this page.
To check on what page section starts and ends, you can use the following example:
Document doc = new Document("sections.docx");
LayoutCollector collector = new LayoutCollector(doc);
int sectionIndex = 1;
for (Section section : doc.getSections())
{
int sectionStartPage = collector.getStartPageIndex(section.getBody().getFirstParagraph());
int sectionEndPage = collector.getEndPageIndex(section.getBody().getLastParagraph());
System.out.println("Section " + sectionIndex + " starts on page " + sectionStartPage + ", ends on page " + sectionEndPage);
sectionIndex++;
}
You could modify this example for your needs and check whether the end of preface section is on the same page where the drug section starts.
@denis.shvydkiy @alexey.noskov I got that part, problem is that this footer would also appear on the other pages of the preface section, which we don’t want, we want this only on the last page which is shared by these two sections? How do we do that?
@denis.shvydkiy @alexey.noskov
I was trying with another approach where I use extractPages method which takes the index and count of pages to be returned from a document, I was throwing preface document at it, and grab all the pages but last one, and create a document with these pages, add this temp doc to the central document, then extract the very last page, add the footer to it and populate this temp document and add this one to the main document, and then work similarly on drug doc and add it to the main document, and now the main doc is populated via three docs(by dividing the initial preface into 2 docs). This seemed feasible to me, but extractPages method started behaving very inconsistently during my testing, for example, if I have 3 pages in my document, and the third page has only 2 lines in it, extractPages is not finding anything on the page 3, it somehow suppresses that content and brings everything to the second page, so my dynamic indexes being used to extract the pages start failing, Can you please help me with this, why would the extractPages method behave this way? If there are 3 pages in the original preface, I would expect the extract method to keep it the same way, but it does not, do we look for certain amount of content within this method which starts returning 2 pages instead of the original 3? I am uploading the preface and the returned document, you can see how the original document has 3 pages but the pdf generated has that content adjusted within the initial 2 pages Affinity Preface.doc.docx (404.5 KB)
T123 PBM- RxClaim Source Medispan_Comprehensive_081423.pdf (166.5 KB)
@cvsformulary I cannot reproduce your issue. The page extraction for the provided document is working as expected and produces 3 pages. Could you please share a simple console application which reproduces the issue?
@Konstantin.Kornilov Affinity Preface Document has 3 pages, and the last page has these lines ending with “These listings do not imply or constitute an endorsement, sponsorship or recommendation by Affinity Health Plan.”
If you look at T123PBM document, these lines are in page 2, Page 3 is a completely different document for drug section, I was talking about the Preface document having 3 pages originally, and then its being compressed into the initial two pages of the PBM Pdf that’s being generated. 3rd Page has nothing to do with the preface document.
@cvsformulary Unfortunately, we cannot reproduce the incorrect output you have attached. I have tested with the following code to extract pages from the document:
Document doc = new Document("C:\\Temp\\in.docx");
for(int pageIndex = 0; pageIndex < doc.getPageCount(); pageIndex++)
{
Document page = doc.extractPages(pageIndex, 1);
page.save("C:\\Temp\\page_" + pageIndex + ".docx");
}
And pages are extracted correctly from your document.