Page 1 of 1

Protect only docx Header

PostPosted: Thu Sep 01, 2016 4:01 pm
by ayushgupta31
Hi,

Does anyone have an idea how can we use DOCX4J APIs to protect the Header Part in a docx file?
Protecting the whole document is easy, but I am specifically looking to protect just the header part.
Body should be editable to the user.

I am using the below code, but it does not set set the header as read only.

WordprocessingMLPackage template = WordprocessingMLPackage.load(new File(inputFile));
BindingHandler bindingHandler = new BindingHandler(template);
List<SectionWrapper> sectionWrappers = template.getDocumentModel().getSections();
HeaderPart header = null;

for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
if (hfp.getDefaultHeader() != null) {
header = hfp.getDefaultHeader();
bindingHandler.applyBindings(header);
System.out.println(XmlUtils.marshaltoString(header.getJaxbElement(),true, true));
}
}

Ayush

Re: Protect only docx Header

PostPosted: Fri Sep 02, 2016 8:50 pm
by jason
At https://github.com/plutext/docx4j/issues/214 I wrote:

You can put your header content in a content control, and set it to locked so that it can't be edited or deleted. But this is imperfect. A user can insert sibling content next to your content control. Or they can delete your header, or insert other headers (eg first page, odd/even, or an entirely new section).

If you can figure out a better way to do it in Word, then it ought to be simple enough to do it in docx4j. But please use StackOverflow or the docx4j forums if you need help, since this isn't an issue as such.

Re: Protect only docx Header

PostPosted: Mon Sep 05, 2016 3:16 pm
by ayushgupta31
Can you please show me how to put the header content in content control? I am able to do it for the body part, but not for the header part.