We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Background color for PdfCompositeField

Thread ID:

Created:

Updated:

Platform:

Replies:

142367 Jan 30,2019 04:30 AM UTC Feb 11,2019 09:54 AM UTC ASP.NET Core - EJ 2 5
loading
Tags: PDF
Thu Nguyen
Asked On January 30, 2019 04:30 AM UTC

Hi,

I am creating a PdfCompositeField object that will draw a PdfPageNumberField and PdfPageCountField one in footer of pdf page.
I would like to set background color for this content.

For example:

Page 1 of 10

How can I do this?

Thanks,

Karthikeyan Chandrasekar [Syncfusion]
Replied On January 30, 2019 10:58 AM UTC

Hi Thu, 
We do not have default option to achieve your requirement, but it is possible to achieve using the below workaround. 

void CreatePDF() 
{ 
    PdfDocument document = new PdfDocument(); 
    document.Pages.Add(); 
    document.Pages.Add(); 
 
    AddFooter(document); 
    document.Save("Sample.pdf"); 
    document.Close(true); 
} 
 
 
void AddFooter(PdfDocument doc) 
{ 
    RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); 
    PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); 
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8); 
    PdfSolidBrush brush = new PdfSolidBrush(Color.Gray); 
 
    PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); 
 
    PdfPageCountField count = new PdfPageCountField(font, brush); 
 
    string pageNumberFormat = "Page {0} of {1}"; 
    PointF pageNumberLocation = new PointF(100, 20); 
    SizeF dimensionOfPageNumber = font.MeasureString(pageNumberFormat); 
 
    footer.Graphics.DrawRectangle(PdfBrushes.Aquamarine,  
        new RectangleF(new PointF(pageNumberLocation.X - 5F, pageNumberLocation.Y), dimensionOfPageNumber)); 
 
    PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, pageNumberFormat, pageNumber, count); 
    compositeField.Draw(footer.Graphics, pageNumberLocation); 
 
    doc.Template.Bottom = footer; 
} 

Regards, 
Karthikeyan  


Thu Nguyen
Replied On January 30, 2019 01:20 PM UTC

Thanks for your replying,

I tried with this solution. But it only measures on "Page {0} of {1}". With a longer string, it cannot fill all.
Example:


Karthikeyan Chandrasekar [Syncfusion]
Replied On February 1, 2019 09:54 AM UTC

 Hi Thu, 
Thanks for your update. I modified the sample to exactly meet your requirement. This sample will manually load the PDF document and add the footer instead of using the Composite field. Please refer the below code snippet and the sample. 
// Creating source document to add footer 
PdfDocument doc = new PdfDocument(); 
 
for (int pageIndex = 0; pageIndex < 100; pageIndex++) 
{ 
    doc.Pages.Add(); 
} 
MemoryStream ms = new MemoryStream(); 
doc.Save(ms); 
ms.Position = 0; 
 
// Add footer 
PdfLoadedDocument ldDoc = new PdfLoadedDocument(ms); 
for (int pageIndex = 0; pageIndex < ldDoc.PageCount; pageIndex++) 
{ 
    // Footer location in the page 
    PointF footerLocation = new PointF(200, 600); 
    PdfFont footerFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 35); 
    string pageNumberText = "Page " + pageIndex.ToString() + " of " + ldDoc.PageCount.ToString(); 
    SizeF footerDimension = footerFont.MeasureString(pageNumberText); 
 
    // Footer content goes here 
    ldDoc.Pages[pageIndex].Graphics 
        .DrawRectangle(PdfBrushes.Gray, new Syncfusion.Drawing.RectangleF(footerLocation, footerDimension)); 
    ldDoc.Pages[pageIndex].Graphics 
        .DrawString(pageNumberText, footerFont, PdfBrushes.Black, footerLocation); 
} 
 
using (var docStream = File.Create("Output.pdf")) 
    ldDoc.Save(docStream); 
 
 
Regards, 
Karthikeyan  


Thu Nguyen
Replied On February 10, 2019 02:27 PM UTC

Thanks,

I hope "background for dynamic field" will be a feature in future. :)
You know we should take care:
- length of text
- word-wrap

Karthikeyan Chandrasekar [Syncfusion]
Replied On February 11, 2019 09:54 AM UTC

Hi Thu Nguyen, 
Yes will consider this as a feature request. But we do not have any immediate plans to implement this feature. Please follow the feature status in the below link. 

Regards, 
Karthikeyan  


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon