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

Last post 02-04-2010, 4:36 AM by aspose.notifier. 7 replies.
Sort Posts: Previous Next
  •  01-12-2010, 2:16 PM 216741

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

    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("<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 &gt; 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);
      }
     }
    }

     
  •  01-13-2010, 3:43 PM 216972 in reply to 216741

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

    Attachment: Present (inaccessible)

    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.


    Nayyer Shahbaz
    Support Developer, Aspose Sialkot Team
    About Us
    Contact Us

    Keep in touch! We're on Twitter and Facebook
     
  •  01-13-2010, 4:58 PM 216991 in reply to 216972

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

    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. Run it and look at sections #2+

    To reproduce the random headers missing 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

     
  •  01-13-2010, 5:33 PM 216996 in reply to 216972

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

    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();

     
  •  01-13-2010, 6:25 PM 216998 in reply to 216972

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

    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<int> 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();

     
  •  01-14-2010, 2:45 AM 217035 in reply to 216998

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

    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.


    Nayyer Shahbaz
    Support Developer, Aspose Sialkot Team
    About Us
    Contact Us

    Keep in touch! We're on Twitter and Facebook
     
  •  01-14-2010, 8:46 AM 217135 in reply to 216998

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

    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.


    Nayyer Shahbaz
    Support Developer, Aspose Sialkot Team
    About Us
    Contact Us

    Keep in touch! We're on Twitter and Facebook
     
  •  02-04-2010, 4:36 AM 220692 in reply to 216741

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

    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.
     
View as RSS news feed in XML