Reputation: 586
I am trying to use Docx4J (https://github.com/plutext/Docx4j4Android4) to read in contents of a Word file. However, with the following code, my program only reads in the body content of a Word file (stored in documentLines
) (does not read in the header or footer). How can I get my program to read in the contents of the file's header and footer?
Below is my code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == READ_IN_FILE) { // When a result has been received, check if it is the result for READ_IN_FILE
if (resultCode == Activity.RESULT_OK) { // heck if the operation to retrieve the Activity's result is successful
// Attempt to retrieve the file
try {
var uri = data?.data // Retrieve the file's resource locator
var document = WordprocessingMLPackage.load(uri?.let { contentResolver.openInputStream(it) })
var documentLines = document.mainDocumentPart.content
} catch (e: Exception) { // If the app failed to attempt to retrieve the error file, throw an error alert
Toast.makeText(this, "Sorry, but there was an error reading in the file", Toast.LENGTH_SHORT).show()
}
}
}
Upvotes: 1
Views: 850
Reputation: 18132
To access headers and footers, you need to retrieve the sections (via the document model). Each section can have 3 headers (first, default, even) and 3 footers (first, default, even).
Here is a Java example that displays the number of objects found in the body and the headers/footers:
import java.io.File;
import java.util.List;
import org.docx4j.model.structure.HeaderFooterPolicy;
import org.docx4j.model.structure.SectionWrapper;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class Test
{
public static void main(String[] args) throws Docx4JException
{
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File("File.docx"));
System.out.println("Body");
processMain(wordMLPackage.getMainDocumentPart());
List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
for(SectionWrapper sw : sectionWrappers)
{
System.out.println("\nSection");
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
processHeader(hfp.getFirstHeader());
processHeader(hfp.getDefaultHeader());
processHeader(hfp.getEvenHeader());
processFooter(hfp.getFirstFooter());
processFooter(hfp.getDefaultFooter());
processFooter(hfp.getEvenFooter());
}
}
static void processMain(MainDocumentPart part)
{
List<Object> content = part.getContent();
System.out.println(content.size()+" objects");
}
static void processHeader(HeaderPart part)
{
if(part!=null)
{
List<Object> content = part.getContent();
System.out.println(content.size()+" header object(s)");
}
}
static void processFooter(FooterPart part)
{
if(part!=null)
{
List<Object> content = part.getContent();
System.out.println(content.size()+" footer object(s)");
}
}
}
Upvotes: 5