Main Problem: Section Header with IsSubsequentPagesOnly = true is included in document for all sections on all pages except the first one. (Should not be included on first page of each section.)
Another Problem: Header not shown on random pages. (Example: If you hard-code randomSeed = 1670481123; below then section 2 page 3 does not include header.)
namespace HeaderIsSubsequentPagesOnly
{
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Aspose.Pdf;
class HeaderIsSubsequentPagesOnly
{
static void Main(string[] args)
{
// Problem: Section.IsPageNumberRestarted works, but Section.Header.IsSubsequentPagesOnly only works the first time.
// (All following sections incorrectly render the header on the section's first page.)
int randomSeed = Environment.TickCount;
string outputFile = Path.GetTempFileName();
File.Move(outputFile, Path.ChangeExtension(outputFile, ".PDF"));
outputFile = Path.ChangeExtension(outputFile, ".PDF");
using (var pdfStream = File.OpenWrite(outputFile))
{
// TODO: Set path to license
//new License().SetLicense(@"...");
var pdf = new Pdf(pdfStream);
var r = new Random(randomSeed);
for (int i = 1; i <= 50; ++i)
using (var xmlStream = new MemoryStream(Encoding.Default.GetBytes(String.Format("<xml Section=\"{0}\" Lines=\"{1}\" Seed=\"{2}\"/>", i, r.Next(50,200), randomSeed))))
using (var xslStream = new MemoryStream(Encoding.Default.GetBytes(
@"<xsl:stylesheet version=`1.0` xmlns:xsl=`http://www.w3.org/1999/XSL/Transform`>
<xsl:template match=`/*`>
<Pdf xmlns=`Aspose.Pdf`>
<Section IsPageNumberRestarted=`true` PageWidth=`595` PageHeight=`842`>
<Header IsSubsequentPagesOnly=`true`>
<Text><Segment>Section #<xsl:value-of select=`concat(@Section, ' (', @Lines, ' lines [', @Seed ,']) ')`/> Page Sp#$NL</Segment></Text>
</Header>
<Footer>
<Text><Segment>Section #<xsl:value-of select=`concat(@Section, ' (', @Lines, ' lines [', @Seed ,']) ')`/> Page Sp#$NL</Segment></Text>
</Footer>
<Text><Segment>^^ Header.IsSubsequentPagesOnly = <xsl:choose>
<xsl:when test=`@Section = 1`>WORKED! (not shown on section page 1)</xsl:when>
<xsl:otherwise>FAILED (shown on section page 1)</xsl:otherwise>
</xsl:choose> ^^#$NL</Segment></Text>
<Text><Segment><xsl:call-template name=`NL`><xsl:with-param name=`i` select=`@Lines`/></xsl:call-template></Segment></Text>
</Section>
</Pdf>
</xsl:template>
<xsl:template name=`NL`>
<xsl:param name=`i` select=`0` />
<xsl:if test=`$i > 2`>
<xsl:value-of select=`concat('Section #', @Section, ' line ', @Lines - $i + 3, ' of ', @Lines, '#$NL')`/>
<xsl:call-template name=`NL`><xsl:with-param name=`i` select=`$i - 1`/></xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>".Replace('`', '"'))))
pdf.BindXML(xmlStream, xslStream);
pdf.Close();
}
Process.Start(outputFile).WaitForExit();
// Same problem when using the API
using (var pdfStream = File.OpenWrite(outputFile))
{
pdfStream.SetLength(0);
var pdf = new Pdf(pdfStream);
pdf.PageSetup.PageWidth = Aspose.Pdf.PageSize.A4Width;
pdf.PageSetup.PageHeight = Aspose.Pdf.PageSize.A4Height;
var r = new Random(randomSeed);
for (int i = 1; i <= 50; ++i)
{
int lines = r.Next(50, 200);
var section = pdf.Sections.Add();
section.IsPageNumberRestarted = true;
var header = new HeaderFooter(section) { IsSubsequentPagesOnly = true };
header.Paragraphs.Add(new Text(header, String.Format("Section #{0} ({1} lines [{2}]) Page Sp", i, lines, randomSeed)));
section.EvenHeader = section.OddHeader = header;
var footer = new HeaderFooter(section);
footer.Paragraphs.Add(new Text(footer, String.Format("Section #{0} ({1} lines, [{2}]) Page Sp", i, lines, randomSeed)));
section.EvenFooter = section.OddFooter = footer;
StringBuilder document = new StringBuilder();
document.AppendFormat("^^ Header.IsSubsequentPagesOnly {0} ^^#$NL#$NL", i == 1 ? "WORKED! (not shown on section page 1)" : "FAILED (shown on section page 1)");
for (int j = 3; j <= lines; ++j)
{
document.AppendFormat("Section #{0} line {1} of {2}#$NL", i, j, lines);
}
section.AddParagraph(new Text(document.ToString()));
}
pdf.Close();
}
System.Diagnostics.Process.Start(outputFile).WaitForExit();
File.Delete(outputFile);
}
}
}