- Home
- Forum
- ASP.NET Core - EJ 2
- Background color for PdfCompositeField
Background color for PdfCompositeField
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,
SIGN IN To post a reply.
5 Replies
KC
Karthikeyan Chandrasekar
Syncfusion Team
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
TN
Thu Nguyen
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:
KC
Karthikeyan Chandrasekar
Syncfusion Team
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
TN
Thu Nguyen
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
KC
Karthikeyan Chandrasekar
Syncfusion Team
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
SIGN IN To post a reply.
- 5 Replies
- 2 Participants
-
TN Thu Nguyen
- Jan 30, 2019 04:30 AM UTC
- Feb 11, 2019 09:54 AM UTC