Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix docx header issue #2026 #2068

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,34 +297,55 @@ private void drawDocumentBackgroundImage(IPart imagePart) {
writer.closeTag("w:background");
}

void writeHeaderReference(BasicComponent header, boolean showHeaderOnFirst) {
String type = showHeaderOnFirst ? "first" : "default";
void writeHeaderReference(BasicComponent header, boolean first) {
String type = first ? "first" : "default";
writer.openTag("w:headerReference");
writer.attribute("w:type", type);
writer.attribute("r:id", header.getRelationshipId());
writer.closeTag("w:headerReference");
}

void writeFooterReference(BasicComponent footer) {
void writeFooterReference(BasicComponent footer, boolean first) {
String type = first ? "first" : "default";
writer.openTag("w:footerReference");
writer.attribute("w:type", type);
writer.attribute("r:id", footer.getRelationshipId());
writer.closeTag("w:footerReference");
}

Header createHeader(int headerHeight, int headerWidth, boolean wrapHeader) throws IOException {
Header createHeader(boolean showHeaderOnFirst, int headerHeight, int headerWidth, boolean wrapHeader)
throws IOException {
if (!showHeaderOnFirst) {
String uri = "header" + getHeaderID() + ".xml";
String type = ContentTypes.WORD_HEADER;
String relationshipType = RelationshipTypes.HEADER;
IPart headerPart = part.getPart(uri, type, relationshipType);
Header firstPageHeader = new Header(headerPart, this, headerHeight, headerWidth);
firstPageHeader.start();
firstPageHeader.end();
writeHeaderReference(firstPageHeader, true);
}
String uri = "header" + getHeaderID() + ".xml";
String type = ContentTypes.WORD_HEADER;
String relationshipType = RelationshipTypes.HEADER;
IPart headerPart = part.getPart(uri, type, relationshipType);
return new Header(headerPart, this, headerHeight, headerWidth, wrapHeader);
}

Footer createFooter(int footerHeight, int footerWidth, boolean wrapHeader) throws IOException {
Footer createFooter(boolean showHeaderOnFirst, int footerHeight, int footerWidth, boolean wrapHeader)
throws IOException {
String uri = "footer" + getFooterID() + ".xml";
String type = ContentTypes.WORD_FOOTER;
String relationshipType = RelationshipTypes.FOOTER;
IPart footerPart = part.getPart(uri, type, relationshipType);
return new Footer(footerPart, this, footerHeight, footerWidth, wrapHeader);
Footer footer = new Footer(footerPart, this, footerHeight, footerWidth, wrapHeader);
if (!showHeaderOnFirst) {
// This results in the same footer content referenced twice:
// Once as header, and once as default.
// This is necessary: otherwise Word won't show the footer on the first page.
writeFooterReference(footer, true);
}
return footer;
}

private int getHeaderID() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public class DocxWriter implements IWordWriter {

private boolean rtl = false;

private boolean showHeaderOnFirst;

private int wordVersion;

private String documentLanguage = "en";
Expand Down Expand Up @@ -157,31 +155,38 @@ public void endSection() {
document.endSection();
}

public boolean isFirstSection() {
return document.isFirstSection();
}

@Override
public void startHeader(boolean showHeaderOnFirst, int headerHeight, int headerWidth) throws IOException {
currentComponent = document.createHeader(headerHeight, headerWidth, this.wrappedTableHeaderFooter);
currentComponent = document.createHeader(showHeaderOnFirst, headerHeight, headerWidth,
this.wrappedTableHeaderFooter);
currentComponent.start();
this.showHeaderOnFirst = showHeaderOnFirst;
}

@Override
public void endHeader() {
currentComponent.end();
document.writeHeaderReference(currentComponent, showHeaderOnFirst);
document.writeHeaderReference(currentComponent, false);
currentComponent = document;
}

@Override
public void startFooter(int footerHeight, int footerWidth) throws IOException {
currentComponent = document.createFooter(footerHeight, footerWidth, this.wrappedTableHeaderFooter);
public void startFooter(boolean showHeaderOnFirst, int footerHeight, int footerWidth) throws IOException {
currentComponent = document.createFooter(showHeaderOnFirst, footerHeight, footerWidth,
this.wrappedTableHeaderFooter);
currentComponent.start();
}

@Override
public void endFooter() {
currentComponent.end();
document.writeFooterReference(currentComponent);
currentComponent = document;
if (currentComponent != null) {
currentComponent.end();
document.writeFooterReference(currentComponent, false);
currentComponent = document;
}
}

@Override
Expand Down Expand Up @@ -374,4 +379,9 @@ public void setWrappedTableHeaderFooter(boolean useWrappedTable) {
public boolean getWrappedTableHeaderFooter() {
return this.wrappedTableHeaderFooter;
}

@Override
public void writeEmptyElement(String tag) {
currentComponent.writeEmptyElement(tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1461,10 +1461,14 @@ private void writeHeaderFooter() throws IOException, BirtException {
String backgroundWidth = style.getBackgroundWidth();

SimpleMasterPageDesign master = (SimpleMasterPageDesign) previousPage.getGenerateBy();
boolean showHeaderOnFirst = master.isShowHeaderOnFirst();
if (!wordWriter.isFirstSection()) {
showHeaderOnFirst = true;
}
boolean empty = true;
if (previousPage.getPageHeader() != null || backgroundHeight != null || backgroundWidth != null) {
wordWriter.startHeader(!master.isShowHeaderOnFirst() && previousPage.getPageNumber() == 1, headerHeight,
contentWidth);

empty = false;
wordWriter.startHeader(showHeaderOnFirst, headerHeight, contentWidth);
if (backgroundHeight != null || backgroundWidth != null) {
String backgroundImageUrl = EmitterUtil.getBackgroundImageUrl(style,
reportContent.getDesign().getReportDesign(), reportContext.getAppContext());
Expand All @@ -1477,19 +1481,26 @@ private void writeHeaderFooter() throws IOException, BirtException {
wordWriter.endHeader();
}
if (previousPage.getPageFooter() != null) {
if (!master.isShowFooterOnLast() && previousPage.getPageNumber() == reportContent.getTotalPage()) {
empty = false;
// We support showFooterOnLast == false only in separate RunTask and RenderTask.
if (!master.isShowFooterOnLast() &&
reportContext.getAppContext().get("EngineTask").getClass().getSimpleName().equals("RenderTask")
&& previousPage.getPageNumber() == reportContent.getTotalPage()) {
IContent footer = previousPage.getPageFooter();
ILabelContent emptyContent = footer.getReportContent().createLabelContent();
emptyContent.setText(AbstractEmitterImpl.EMPTY_FOOTER);
wordWriter.startFooter(footerHeight, contentWidth);
wordWriter.startFooter(false, footerHeight, contentWidth);
contentVisitor.visit(emptyContent, null);
wordWriter.endFooter();
} else {
wordWriter.startFooter(footerHeight, contentWidth);
wordWriter.startFooter(showHeaderOnFirst, footerHeight, contentWidth);
contentVisitor.visitChildren(previousPage.getPageFooter(), null);
wordWriter.endFooter();
}
}
if (!empty && !showHeaderOnFirst) {
wordWriter.writeEmptyElement("w:titlePg");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ void drawDocumentBackgroundImage(String backgroundImageUrl, String backgroundHei
*/
void endSection();

/**
* Get the flag if the writer is still in the first section.
*
* This is initially true and will be false after endSection was called the
* first time.
*
* @return true while in first section, false otherwise.
*/
boolean isFirstSection();

/**
*
* Write page property
Expand Down Expand Up @@ -314,11 +324,12 @@ void drawImage(byte[] data, double height, double width, HyperlinkInfo hyper, IS
/**
* Start footer
*
* @param isFirstPage true for the first page, otherwise false.
* @param footerHeight footer height
* @param footerWidth footer width
* @throws IOException
*/
void startFooter(int footerHeight, int footerWidth) throws IOException;
void startFooter(boolean isFirstPage, int footerHeight, int footerWidth) throws IOException;

/**
* End footer
Expand Down Expand Up @@ -396,4 +407,11 @@ default boolean getWrappedTableHeaderFooter() {
return true;
}

/**
* Write an empty XML element.
*
* @param tag XML element name
*/
void writeEmptyElement(String tag);

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,34 @@ public void startSection() {
writer.openTag("w:sectPr");
}

private boolean firstSection = true;

/**
* @return whether we are still in the first section or not.
*/
public boolean isFirstSection() {
return firstSection;
}

/**
* Set if we are in first section.
*
* Usually only called with a value of false, because we start in the first
* section.
*
* @param firstSection true for first section, false for later sections.
*
*/
public void setFirstSection(boolean firstSection) {
this.firstSection = firstSection;
}

/**
* End section
*/
public void endSection() {
writer.closeTag("w:sectPr");
setFirstSection(false);
}

protected void drawImageShapeType(int imageId) {
Expand Down Expand Up @@ -1479,4 +1502,14 @@ public void setWrappedTableHeaderFooter(boolean useWrappedTable) {
public boolean getWrappedTableHeaderFooter() {
return this.wrappedTableHeaderFooter;
}

/**
* Write an empty XML element
*
* @param tag the XML element name
*/
public void writeEmptyElement(String tag) {
writer.openTag(tag);
writer.closeTag(tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,11 @@ public void endHeader() {
writer.closeTag("w:hdr");
}


@Override
public void startFooter(int footerHeight, int footerWidth) {
public void startFooter(boolean isFirstPage, int footerHeight, int footerWidth) {
writer.openTag("w:ftr");
writer.attribute("w:type", "odd");
writer.attribute("w:type", (isFirstPage ? "first" : "odd"));
startHeaderFooterContainer(footerHeight, footerWidth);
}

Expand Down