How To create multiple pages dynamically when text does not fit in first page

I need to create a Pdf document with header,footers and text, should
be spanned to multiple pages when text does not fit in page and also footer need to be displayed
on every page.

1. when text size increases then text is not wrapping to nextline, instead it is displaying
half of the text.
What should I do, text should be fit in same line or to naxt line, and do not change the font size?

2.I tried to create multiple pages when text exceeds first page,
how can I create next page and draw text on that page?

I have gone through the syncusion documentation but, I couldn't solve above two problems.


10 Replies 1 reply marked as answer

SV Surya Venkatesan Syncfusion Team February 1, 2022 01:39 PM UTC

Hi Adnan,


We have created a sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Pdf_Sample_TextPaginationWithHF977948996 to achieve your requirements of creating a Pdf document with header, footers and text, should be spanned to multiple pages when text does not fit in page and also footer need to be displayed on every page and overcome the mentioned issues.


Kindly try the attached sample and let us know the result.


Regards,

Surya V



AA Adnan Ali February 13, 2022 06:21 AM UTC

Thanks for quick response. It worked fine but what if it is not a single element but multiple text elements

PdfLayoutFormat layoutFormat = new PdfLayoutFormat();

layoutFormat.Layout = PdfLayoutType.Paginate;

layoutFormat.Break = PdfLayoutBreakType.FitPage;

PdfTextElement textElement = new PdfTextElement("", new PdfStandardFont(PdfFontFamily.TimesRoman, 14));

@for (int i = 1; i < enumsClass.Priorities.Count+1; i++)

{

textElement = new PdfTextElement($"Priority: {i} ({@businessObjects.Where(a => a.Priority == i).Count()} objectDesciption)", new PdfStandardFont(PdfFontFamily.TimesRoman, 14));

textElement.Draw(pdfPage, new RectangleF(0, i*250, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat);

}



SV Surya Venkatesan Syncfusion Team February 14, 2022 03:13 PM UTC

Hi Adnan,


Currently, We are analyzing to achieve your requirement with multiple text elements and we will update the further details on February 16th 2022.


Regards,

Surya V



GK Gowthamraj Kumar Syncfusion Team February 16, 2022 02:44 PM UTC

Hi Adnan, 
 
Essential PDF allows you to create a multiple text in PDF document by using PdfTextElement class. The PdfLayoutFormat class helps to allow the text to flow across pages. The PdfLayoutResult class provides the rendered bounds of the previously added text which can be used to place successive elements without overlapping. We can get the previous text element by bottom bounds. Please refer the below code snippet, 
 
PdfTextElement textElement = new PdfTextElement("", new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
 
PdfLayoutResult result = null; 
float y = 10; 
 
for (int i = 0; i < 3; i++) 
 
{ 
if(i == 0) 
{ 
textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
result = textElement.Draw(pdfPage, new RectangleF(0, y, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat); 
} 
else 
{ 
 
textElement = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
result = textElement.Draw(result.Page, new RectangleF(0, y, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat); 
 
} 
y = result.Bounds.Height + 20; 
 
 
} 
 
Please refer the below documentation link, 
 
Please try the above sample on your end and let us know the result. 
 
Regards, 
Gowthamraj K 



AA Adnan Ali March 2, 2022 02:55 AM UTC

Thanks for your response, it works perfectly in the above sample. But if the text is small string not a long paragraph then if we iterate over it does not create multiple pages


PdfTextElement textElement = new PdfTextElement("", new PdfStandardFont(PdfFontFamily.TimesRoman, 14));


            PdfLayoutResult result = null;

            float y = 10;

            //i want to create 10-15 pages

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

            {

                if(i == 0)

                {

                    textElement = new PdfTextElement("This is one liner text/Paragraph", new PdfStandardFont(PdfFontFamily.TimesRoman, 14));

                    result = textElement.Draw(pdfPage, new RectangleF(0, y, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat);

                }

                else

                {

                    textElement = new PdfTextElement("This is one liner text/Paragraph", new PdfStandardFont(PdfFontFamily.TimesRoman, 14));

                    result = textElement.Draw(result.Page, new RectangleF(0, y+20, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat);

                }

                y = result.Bounds.Height + 40;

            }




If i do 

 y = y + 40; it does creates 2-3 pages but the text elements is not Visible on that pages except a single element 

Kindly let me know if i am missing something



GK Gowthamraj Kumar Syncfusion Team March 2, 2022 12:47 PM UTC

Hi Adnan, 
 
We can get the previous text element by bottom bounds ( Y and Height) and draw the successive text elements without overlapping. Please refer the below code snippet, 
 
PdfTextElement textElement = new PdfTextElement("", new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
 
PdfLayoutResult result = null; 
 
float y = 10; 
 
for (int i = 0; i < 600; i++) 
{ 
 
if (i == 0) 
 
{ 
 
textElement = new PdfTextElement("This is one liner text/Paragraph", new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
 
result = textElement.Draw(pdfPage, new RectangleF(0, y, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat); 
 
} 
else 
 
{ 
 
textElement = new PdfTextElement("This is one liner text/Paragraph", new PdfStandardFont(PdfFontFamily.TimesRoman, 14)); 
 
result = textElement.Draw(result.Page, new RectangleF(0, y, pdfPage.GetClientSize().Width, pdfPage.GetClientSize().Height - pdfDocument.Template.Bottom.Height), layoutFormat); 
 
} 
 
y = result.Bounds.Y+ result.Bounds.Height + 40; 
 
} 
 
 
Please try the above solution on your end and let us know the result. 
 
Regards, 
Gowthamraj K 



AA Adnan Ali March 2, 2022 05:59 PM UTC

Thanks, it does not work perfectly for my case(Multiple Pages) but i have found the way with the help of sample solution you provided


PdfTextElement element = new PdfTextElement("", new PdfStandardFont(PdfFontFamily.TimesRoman, 14));

PdfTextLayoutResult result = element.Draw(pdfPage, bounds, layoutFormat);

//PdfLayoutResult result = null;

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

{

bounds = new RectangleF(new PointF(0, result.LastLineBounds.Y + 20), new SizeF(pdfPage.Graphics.ClientSize.Width - 20, pdfPage.Graphics.ClientSize.Height - 10));

element.Text = "Some First text";

result = element.Draw(pdfPage, bounds.Location, bounds.Width, layoutFormat);

if ((result.Remainder != null) && (result.Remainder.Length > 0))

{

bounds = new RectangleF(new PointF(0, 10), new SizeF(pdfPage.Graphics.ClientSize.Width - 20, pdfPage.Graphics.ClientSize.Height - 10));

element.Text = result.Remainder;

result = element.Draw(pdfPage, bounds.Location, bounds.Width, layoutFormat);

}

else

{

pdfPage = result.Page;

bounds = new RectangleF(new PointF(0, result.LastLineBounds.Y + 20), new SizeF(pdfPage.Graphics.ClientSize.Width - 20, pdfPage.Graphics.ClientSize.Height - 10));

}

}




This way we can create as many pages as needed Thanks


Marked as answer

GK Gowthamraj Kumar Syncfusion Team March 3, 2022 10:43 AM UTC

Hi Adnan, 

We are glad to know that the reported issue is resolved. Please let us know if you need any further assistance in this. 

Regards, 
Gowthamraj K 



AA Adnan Ali April 13, 2022 05:06 PM UTC

Thanks, i have one more question regarding this 

The way we are writing text on the page 

element.Text = "Some First text";

result = element.Draw(pdfPage, bounds.Location, bounds.Width, layoutFormat);

and adding next text after getting the bounds of previous text element. as such 

if ((result.Remainder != null) && (result.Remainder.Length > 0))

{

   bounds = new RectangleF(new PointF(0, 10), new SizeF(pdfPage.Graphics.ClientSize.Width - 20,           pdfPage.Graphics.ClientSize.Height - 10));

   element.Text = result.Remainder;

  result = element.Draw(pdfPage, bounds.Location, bounds.Width, layoutFormat);

}

else

{

pdfPage = result.Page;

bounds = new RectangleF(new PointF(0, result.LastLineBounds.Y + 20), new SizeF(pdfPage.Graphics.ClientSize.Width - 20, pdfPage.Graphics.ClientSize.Height - 10));

}

Can we do the same if we want to draw multiple images, means can we draw image and get its bounds to draw next image

Thanks 



GK Gowthamraj Kumar Syncfusion Team April 15, 2022 02:05 PM UTC

Hi Adnan,


We do not have support for adding multiple images by using PdfLayoutResult approach. As a workaround, we can achieve this requirement by manually calculating the previous image bounds and draw the image based on the Y position. We have attached the sample and output document for your reference, please try the below sample on your end and let us know if it is suites your requirement or not.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Pdf_Sample_MultipleImage228932820

Output: https://www.syncfusion.com/downloads/support/directtrac/general/pd/Sample_MultiplImage-533658491


Kindly please try the above solution on your end and let us know if it suits your requirement.


Regards,

Gowthamraj K


Loader.
Up arrow icon