How to prevent ParagraphFormat leaking into previous page after a PageBreak

We have the following code and we’re expecting that the first page should not have a line with a black background color.

builder.Writeln("it's unexpected that the line below has a black background color");
builder.InsertBreak(BreakType.PageBreak);
builder.ParagraphFormat.Shading.BackgroundPatternColor = Color.Black;
builder.Write("this line should have a black background color");

@gojanpaolo

Your code inserts the page break in the second paragraph. So, you get the background color of page break.

Please use the following code example to get the desired output.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("it's unexpected that the line below has a black background color");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln();
builder.ParagraphFormat.Shading.BackgroundPatternColor = Color.Black;
builder.Writeln("this line should have a black background color");

doc.Save(MyDir + "19.7.docx");

@tahir.manzoor Thanks for the immediate response. Unfortunately, calling Writeln will generate an extra line as shown on the attached document. (I changed builder.Writeln(); to builder.Writeln("this is an extra line"); so the extra line is more visible)

I’ve also attached what we would like DocumentBuilder to generate (I used MS Word to generate the attached file).

files.zip (17.6 KB)

@gojanpaolo

Please use the following code example to get the desired output.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("it's unexpected that the line below has a black background color");
builder.InsertBreak(BreakType.PageBreak);

builder.ParagraphFormat.ClearFormatting();
Paragraph paragraph = builder.InsertParagraph();
builder.MoveTo(paragraph);
builder.ParagraphFormat.Shading.BackgroundPatternColor = Color.Black;
builder.Write("this line should have a black background color");

OoxmlSaveOptions options = new OoxmlSaveOptions();
options.Compliance = OoxmlCompliance.Iso29500_2008_Strict;
doc.Save(MyDir + "19.7.docx", options);

@gojanpaolo

Further to my previous post, you can also achieve your requirement using following code snippet.

builder.Writeln("it's unexpected that the line below has a black background color");
builder.ParagraphFormat.PageBreakBefore = true;
builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Black;
builder.Write("this line should have a black background color");

@tahir.manzoor Thanks for the two approaches. Could you please clarify how Iso29500_2008_Strict fixes the issue? And what other effects will using Iso29500_2008_Strict have on a document? We’re looking into using that approach since the second approach, PageBreakBefore, doesn’t blend well with our current architecture.

We’re worried that we might need to use the default OoxmlCompliance (i.e. Ecma376_2006) in the future.

It looks like we can’t use OoxmlSaveOptions for a PDF. So we can’t use this approach. :frowning:

@tahir.manzoor Is there any other solution that wouldn’t require changing the ParagraphFormat and is compatible when saving to PDF? Thank you!

Also, should the behavior described on my first post be considered a bug?

@gojanpaolo,

Another simple workaround is to just add Page Break character at the end of first paragraph. Hope, this helps.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Paragraph targetPara = builder.CurrentParagraph;
builder.Writeln("it's unexpected that the line below has a black background color");
targetPara.AppendChild(new Run(doc, ControlChar.PageBreak));

builder.ParagraphFormat.Shading.BackgroundPatternColor = Color.Black;
builder.Write("this line should have a black background color");

doc.Save(@"E:\Temp\19.7.docx");
doc.Save(@"E:\Temp\19.7.pdf");

@awais.hafeez Thanks for the alternative workaround. But it also adds an extra line on the 2nd page.

@gojanpaolo

The following code example does not add extra line in output PDF and DOCX.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("it's unexpected that the line below has a black background color");
builder.ParagraphFormat.PageBreakBefore = true;
builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Black;
builder.Write("this line should have a black background color");

doc.Save(MyDir + @"19.7.docx");
doc.Save(MyDir + @"19.7.pdf");

@tahir.manzoor Yes, we managed to use that approach and fit it in our architecture. This is how our code looks like now

new DocumentBuilder()
.ApplyDefaults()
.BuildPageFooter()
.BuildFormHeader()
.InsertEmptyLine()
.BuildProposalFormInstructions()
.InsertEmptyLine()
.BuildCompanyInformation(eplProposalForm.CompanyInformation)
.InsertPageBreak(_ => _
.BuildCurrentInsuranceInformation(eplProposalForm.CurrentInsuranceInformation))
.InsertEmptyLine()
.BuildCommonlyOwnedEntities(eplProposalForm.CommonlyOwnedEntity)
.InsertEmptyLine()
.BuildHRContactInformation(eplProposalForm.HRContactInformation)
.InsertEmptyLine()
.BuildEmployeeProfile(eplProposalForm.EmployeeProfile)
.InsertEmptyLine()
.BuildLitigationInformation(eplProposalForm.LitigationInformation)
.Document;

We’d prefer to keep the responsibility of adding the separation (i.e.page break, empty line, etc) between different sections in our root builder. This way, we can easily re-arrange the sections or use a different separation (e.g. from page break to empty line) when we need to.

InsertPageBreak code looks like this

public static DocumentBuilder InsertPageBreak(
    this DocumentBuilder builder,
    Action<DocumentBuilder> build)
{
    var paragraph = builder.InsertParagraph();
    build(builder);
    paragraph.ParagraphFormat.PageBreakBefore = true;
    return builder;
}

It could be a lot cleaner like this

// ...
.InsertEmptyLine()
.BuildCompanyInformation(eplProposalForm.CompanyInformation)
.InsertPageBreak()
.BuildCurrentInsuranceInformation(eplProposalForm.CurrentInsuranceInformation)
.InsertEmptyLine()
// ...

… but the work-around is ok for now. Thank you for all your help. :slight_smile: