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

Free Support Forum - aspose.com

Exception while adding paragraphs through absorbing text fragment

Hello, consider following sample code,

Document doc=new Document();
Page _tocPage = doc.Pages.Add();
_tocPage.TocInfo = new TocInfo
{
FormatArrayLength = 3
};

        for (int i = 0; i < _tocPage.TocInfo.FormatArray.Length; i++)
        {
            LevelFormat lf = _tocPage.TocInfo.FormatArray[i];

            lf.TextState.FontSize = 10;
            lf.Margin.Top = 5;
            lf.Margin.Left = (i + 1) * 10;  // set indent for every level (+ 10 for every level)

                lf.TextState.ForegroundColor = Color.Blue;
            if (i == 0)
            {
                lf.LineDash = TabLeaderType.None;
                lf.TextState.FontStyle = FontStyles.Bold;
                lf.Margin.Top = 10;
            }
            if (i == 2)
            {
                lf.Margin.Left += 10;
            }
        }
        
        doc.Pages.Add();

        for (int i = 0; i < 50; i++)
        {
            Heading heading = new Heading(1)
            {
              
                Text = "Hello"+i
            };
            doc.Pages[2].Paragraphs.Add(heading);
        }
        doc.ProcessParagraphs();
        for (int i = 0; i < 50; i++)
        {
            TextFragmentAbsorber absorber = new TextFragmentAbsorber("Hello" + i);
            doc.Pages.Accept(absorber);
            if (absorber.TextFragments.Count > 0)
            {
                Heading heading2 = new Heading(1) ;
               

                




                heading2.IsAutoSequence = true;
                heading2.TocPage = doc.Pages[1];
                heading2.Text = absorber.TextFragments[1].Text;
                heading2.DestinationPage = absorber.TextFragments[1].Page;
                
                heading2.Top = absorber.TextFragments[1].Page.Rect.Height;
                doc.Pages[1].Paragraphs.Add(heading2);
            }

        }
       
        doc.Save(@"H:\userLable.pdf");

While saving the document, following exception is thrown

Invalid index: index should be in the range [1…n] where n equals to the text fragments count.

Now if i put 20 in for loops instead of 50 the code works.

Thank you

Hello Ranjeeta,


Thanks for contacting support.

By testing the scenario with your code snippet I have observed that content of TOC was exceeding than the actual page size because you were setting top margin for each TOC element. When I removed the code for setting the margin, so that TOC elements could fit on a single page and code was executed fine.

for (int i = 0; i
< _tocPage.TocInfo.FormatArray.Length; i++)<o:p></o:p>

{

LevelFormat lf = _tocPage.TocInfo.FormatArray[i];

lf.TextState.FontSize = 10;

//lf.Margin.Top = 5;

lf.Margin.Left = (i + 1) * 10;

// set indent for every level (+ 10 for every level)

lf.TextState.ForegroundColor = Color.Blue;

if (i == 0)

{

lf.LineDash = TabLeaderType.None;

lf.TextState.FontStyle = FontStyles.Bold;

//lf.Margin.Top = 10;

}

if (i == 2)

{

lf.Margin.Left += 10;

}

}


ranjeeta_balakrishnan_knorr-bremse_com:
Now if i put 20 in for loops instead of 50 the code works.

As shared above, the content was not fitting on a single page, so when you add less content, it fits on TOC page and code works fine. However I have tried to add 50 elements in TOC by breaking the loop into two parts. Please check the following code snippet where I have managed to add TOC on two pages to cover all heading elements.

Document doc = new Document();

Page _tocPage = doc.Pages.Add();

TocInfo tocInfo = new TocInfo { FormatArrayLength = 3 };

_tocPage.TocInfo = tocInfo;

for (int i = 0; i < _tocPage.TocInfo.FormatArray.Length; i++)

{

LevelFormat lf = _tocPage.TocInfo.FormatArray[i];

lf.TextState.FontSize = 10;

lf.Margin.Top = 5;

lf.Margin.Left = (i + 1) * 10;

// set indent for every level (+ 10 for every level)

lf.TextState.ForegroundColor = Color.Blue;

if (i == 0)

{

lf.LineDash = TabLeaderType.None;

lf.TextState.FontStyle = FontStyles.Bold;

lf.Margin.Top = 10;

}

if (i == 2)

{

lf.Margin.Left += 10;

}

}

doc.Pages.Add();

for (int i = 0; i < 50; i++)

{

Heading heading = new Heading(1)

{

Text = "Hello" + i

};

doc.Pages[2].Paragraphs.Add(heading);

}

doc.ProcessParagraphs();

for (int i = 0; i < 34; i++)

{

TextFragmentAbsorber absorber = new TextFragmentAbsorber("Hello" + i);

doc.Pages.Accept(absorber);

if (absorber.TextFragments.Count > 0)

{

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

heading2.TocPage = _tocPage;

heading2.Segments.Add(segment2);

heading2.DestinationPage = absorber.TextFragments[1].Page;

heading2.Top = absorber.TextFragments[1].Page.Rect.Height;

segment2.Text = absorber.TextFragments[1].Text;

_tocPage.Paragraphs.Add(heading2);

}

}

_tocPage = doc.Pages.Insert(2);

_tocPage.TocInfo = tocInfo;

for (int i = 34; i < 50; i++)

{

TextFragmentAbsorber absorber = new TextFragmentAbsorber("Hello" + i);

doc.Pages.Accept(absorber);

if (absorber.TextFragments.Count > 0)

{

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

heading2.TocPage = _tocPage;

heading2.Segments.Add(segment2);

heading2.DestinationPage = absorber.TextFragments[1].Page;

heading2.Top = absorber.TextFragments[1].Page.Rect.Height;

segment2.Text = absorber.TextFragments[1].Text;

_tocPage.Paragraphs.Add(heading2);

}

}

doc.Save(dataDir + @"userLable.pdf");



I have also attached an output, generated by above code, for your reference. In case of any further assistance, please feel free to contact us.


Best Regards,

Hello Asad,
Thank you for reply,
So according to above code I need to add one more toc page if first page is getting full.

So is their any way to know how many lines have been added till now to a page, or how much space is left on a page.

Thank you,

Hello Ranjeeta,


We are checking details in our environment as per your requirements and will get back to you shortly. Please be patient.


Best Regards,

Hello Ranjeeta,


Thanks for your patience.

I have tried to achieve the requirements by following code snippet but I am afraid that I could not get much success. I have tried to add TOC before adding content inside the PDF, as you can see in the code snippet, but it did not work out.


Document doc = new Document();<o:p></o:p>

Page _tocPage = doc.Pages.Add();<o:p></o:p>

TocInfo tocInfo = new TocInfo { FormatArrayLength = 3 };<o:p></o:p>

_tocPage.TocInfo = tocInfo;<o:p></o:p>

for (int i = 0; i < _tocPage.TocInfo.FormatArray.Length; i++)<o:p></o:p>

{<o:p></o:p>

LevelFormat lf = _tocPage.TocInfo.FormatArray[i];<o:p></o:p>

lf.TextState.FontSize = 10;<o:p></o:p>

lf.Margin.Top = 5;<o:p></o:p>

lf.Margin.Left = (i + 1) * 10;<o:p></o:p>

// set indent for every level (+ 10 for every level)<o:p></o:p>

lf.TextState.ForegroundColor = Color.Blue;<o:p></o:p>

if (i == 0)<o:p></o:p>

{<o:p></o:p>

lf.LineDash = TabLeaderType.None;<o:p></o:p>

lf.TextState.FontStyle = FontStyles.Bold;<o:p></o:p>

lf.Margin.Top = 10;<o:p></o:p>

}<o:p></o:p>

if (i == 2)<o:p></o:p>

{<o:p></o:p>

lf.Margin.Left += 10;<o:p></o:p>

}<o:p></o:p>

}

for (int i = 0; i
< 50; i++)<o:p></o:p>

{<o:p></o:p>

TextFragment tf = new TextFragment(“Hello” + i);<o:p></o:p>

Aspose.Pdf.Heading heading2 = new
Aspose.Pdf.
Heading(1);<o:p></o:p>

TextSegment segment2 = new TextSegment();<o:p></o:p>

heading2.TocPage = _tocPage;<o:p></o:p>

heading2.Segments.Add(segment2);<o:p></o:p>

segment2.Text = tf.Text;<o:p></o:p>

_tocPage.Paragraphs.Add(heading2);<o:p></o:p>

}

var p = doc.Pages.Add();<o:p></o:p>

for (int i = 0; i
< 50; i++)<o:p></o:p>

{<o:p></o:p>

Heading heading = new Heading(1)<o:p></o:p>

{<o:p></o:p>

Text = “Hello” + i<o:p></o:p>

};<o:p></o:p>

p.Paragraphs.Add(heading);<o:p></o:p>

}<o:p></o:p>

doc.ProcessParagraphs();



Hence, I have logged an issue as PDFNET-42704 in our issue tracking system, for the sake of detailed investigation.

ranjeeta_balakrishnan_knorr-bremse_com:
is their any way to know how many lines have been added till now to a page, or how much space is left on a page

However, in order to get count of lines, added in TOC page, you can do something like following where I have added a new TOC page after getting count of the paragraphs.


for (int i = 0; i < 50; i++)

{

TextFragment tf = new TextFragment("Hello" + i);

Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

TextSegment segment2 = new TextSegment();

heading2.TocPage = _tocPage;

heading2.Segments.Add(segment2);

segment2.Text = tf.Text;

_tocPage.Paragraphs.Add(heading2);

if(_tocPage.Paragraphs.Count == 34)

{

_tocPage = doc.Pages.Insert(_tocPage.Number + 1);

_tocPage.TocInfo = tocInfo;

}

}



As far as above logged issue is concerned, we will further look into the details of the issue and keep you informed on the status of its rectification. Please be patient and give us little time.

We are sorry for the inconvenience.


Best Regards,