We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Section Header problems in direct-to-file mode (v4.0.0)

I am having problems working with headers in direct-to-file mode in Aspose.PDF for .NET version 4.0.0. Version 4.1.1 fails much earlier in the process as I explained in a separate thread.

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.)

Sample:

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("", i, r.Next(50,200), randomSeed))))
using (var xslStream = new MemoryStream(Encoding.Default.GetBytes(
@"
Section # Page Sp#$NL
Section # Page Sp#$NL
^^ Header.IsSubsequentPagesOnly =
WORKED! (not shown on section page 1)
FAILED (shown on section page 1)
^^#$NL
".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);
}
}
}

Hello Jed,

Sorry for replying you late.

I've tested the scenario with the following code snippet using Aspose.Pdf for .NET 4.1.1 and I'm unable to notice both the problems IsSubsequentPagesOnly & Header not shown on random pages that you've reported. Can you please take a look over the following code snippet and see if I'm not missing anything. I've also attached the resultant PDF document that I've generated.

[C#]

//Create a file stream to create the PDF document
FileStream fs = new FileStream(@"D:/pdftest/SingleSeg-dtest2.pdf", FileMode.Create);

//Instantiate the Pdf instance and pass the file stream object to its constructor
Pdf pdf = new Pdf(fs);
//Add a section to the PDF document
Aspose.Pdf.Section sec1 = pdf.Sections.Add();
Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.HeaderFooter(sec1);
sec1.OddHeader = sec1.EvenHeader = hf1;
//Enable this header for first page only
hf1.IsSubsequentPagesOnly = true;
//Instantiate a Text paragraph that will store the content to show as header
Text text = new Text(hf1, "header for first page");
//Add the text object to the Paragraphs collection of HeaderFooter object to
//display header on the pages of PDF document
hf1.Paragraphs.Add(text);
Random r = new Random();

//Add 300 text paragraphs to the section
for (int i = 0; i < 300; i++)
{
int lines = r.Next(50, 200);
Text t = new Text(hf1, String.Format("Section #{0} ({1} lines [{2}]) Page $Sp", i, lines, randomSeed));
sec1.AddParagraph(t);
}
//Close the Pdf. This method is used only for direct file mode
pdf.Close();

We apologize for your inconvenience.

Your example does not include more than one section in the PDF. My problem with IsSubsequentPagesOnly is that it only works for the first section in direct-to-file mode.


To reproduce the IsSubsequentPagesOnly issue using v4.1.1 and my initial code:
1. Replace ‘$Sp’ with anything else… ‘Sp’ so v4.1.1 won’t crash
2. Replace ‘randomSeed = Environment.TickCount’ with ‘randomSeed = 1670481123’ or 'randomSeed = 1768452777’
3. Run it and look at section #2 page #3

The problem appears to be the interaction of IsPageNumberRestarted and IsSubsequentPagesOnly. Sample duplication for second section in italics, important changes also bold:


//Create a file stream to create the PDF document
FileStream fs = new FileStream(@“D:/pdftest/SingleSeg-dtest3.pdf”, FileMode.Create);

//Instantiate the Pdf instance and pass the file stream object to its constructor
Pdf pdf = new Pdf(fs);
//Add a section to the PDF document
Aspose.Pdf.Section sec1 = pdf.Sections.Add();
Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.HeaderFooter(sec1);
sec1.OddHeader = sec1.EvenHeader = hf1;
//Enable this header for first page only
hf1.IsSubsequentPagesOnly = true;
//Instantiate a Text paragraph that will store the content to show as header
Text text = new Text(hf1, “header for first page”);
//Add the text object to the Paragraphs collection of HeaderFooter object to
//display header on the pages of PDF document
hf1.Paragraphs.Add(text);
Random r = new Random();

//Add 300 text paragraphs to the section
for (int i = 0; i < 300; i++)
{
int lines = r.Next(50, 200);
Text t = new Text(hf1, String.Format(“Section #{0} ({1} lines [{2}]) Page $Sp”, i, lines, randomSeed));
sec1.AddParagraph(t);
}

sec1 = pdf.Sections.Add();
//Restart page numbers, please
sec1.IsPageNumberRestarted = true;
hf1 = new Aspose.Pdf.HeaderFooter(sec1);
sec1.OddHeader = sec1.EvenHeader = hf1;
//Enable this header for first page only
hf1.IsSubsequentPagesOnly = true;
//Instantiate a Text paragraph that will store the content to show as header
text = new Text(hf1, “header for second section);
//Add the text object to the Paragraphs collection of HeaderFooter object to
//display header on the pages of PDF document
hf1.Paragraphs.Add(text);
r = new Random();

//Add 300 text paragraphs to the section
for (int i = 0; i < 300; i++)
{
int lines = r.Next(50, 200);
Text t = new Text(hf1, String.Format(“Section #{0} ({1} lines [{2}]) Page $Sp”, i, lines, randomSeed));
sec1.AddParagraph(t);
}

//Close the Pdf. This method is used only for direct file mode
pdf.Close();

In regards to the Header not shown on random pages problem, the following bold changes to your sample yield a .PDF missing a header on the last page:


//Create a file stream to create the PDF document
FileStream fs = new FileStream(@“D:/pdftest/SingleSeg-dtest5.pdf”, FileMode.Create);

//Instantiate the Pdf instance and pass the file stream object to its constructor
Pdf pdf = new Pdf(fs);

Action addSection = numLines =>

{

//Add a section to the PDF document
Aspose.Pdf.Section sec1 = pdf.Sections.Add();

sec1.IsPageNumberRestarted = true;
Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.HeaderFooter(sec1);
sec1.OddHeader = sec1.EvenHeader = hf1;
//Enable this header for first page only
hf1.IsSubsequentPagesOnly = true;
//Instantiate a Text paragraph that will store the content to show as header
Text text = new Text(hf1, “header for first page”);
//Add the text object to the Paragraphs collection of HeaderFooter object to
//display header on the pages of PDF document
hf1.Paragraphs.Add(text);
Random r = new Random();

//Add numLines text paragraphs to the section
for (int i = 0; i < numLines; i++)
{
int lines = r.Next(50, 200);
Text t = new Text(hf1, String.Format(“Section w/ {0} lines line {1}”, numLines, i + 1));
sec1.AddParagraph(t);
}

};

addSection(88);

addSection(145);

//Close the Pdf. This method is used only for direct file mode
pdf.Close();

Hello Jed,

Thanks for sharing the detailed information regarding the issue.

While testing the code snippets that you've shared with Aspose.Pdf for .NET 4.1.1, I'm able to notice both the problems. The issues have been communicated to the development team and they're looking into the details of these problems. Soon you'll be updated with the status of correction.

We're really sorry for such inconvenience that you've been facing in this regard. Please accept our humble apologies as we'll get back to you with some definite solution, shortly.

Thanks for your cooperation.

Hello Jed,

For the sake of correction, I've logged both the issues IsSubsequentPagesOnly not working and Header not shown on random pages as PDFNET-13499 and PDFNET-13500 in our issue tracking system. We'll further investigate these issues and will keep you updated with the status of correction. We apologize for your inconvenience.

The issues you have found earlier (filed as 13499;13500) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.