Text Share Online

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

//Define font (Bold + Italic)
PdfTrueTypeFont font = new PdfTrueTypeFont(
new Font(“Arial”, 28f, FontStyle.Bold | FontStyle.Italic), true);

//Set text color (light gray)
PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);

//Text to draw
string text = “PROVISIONAL”;

//Text format (center align)
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
float x = page.Canvas.ClientSize.Width / 2;
float y = page.Canvas.ClientSize.Height / 3;

//Measure text width
SizeF textSize = font.MeasureString(text, format);

//Draw the text
page.Canvas.DrawString(text, font, brush, x, y, format);

//Draw lines above and below text
float linePadding = 5; //space between text and line
float textTop = y – textSize.Height / 2;
float textBottom = y + textSize.Height / 2;

float startX = x – textSize.Width / 2;
float endX = x + textSize.Width / 2;

PdfPen pen = new PdfPen(Color.Gray, 1f);

//Top line
page.Canvas.DrawLine(pen, startX, textTop – linePadding, endX, textTop – linePadding);

//Bottom line
page.Canvas.DrawLine(pen, startX, textBottom + linePadding, endX, textBottom + linePadding);

//Extra bottom line (double underline)
page.Canvas.DrawLine(pen, startX, textBottom + linePadding + 4, endX, textBottom + linePadding + 4);

 

 

 

 

static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(40));

// Create grid with 1 column
PdfGrid grid = new PdfGrid();
grid.Columns.Add(1);
PdfGridRow row = grid.Rows.Add();
row.Cells[0].Value = “PROVISIONAL”;

// Style
PdfGridCellStyle style = new PdfGridCellStyle();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(“Arial”, 18, FontStyle.Bold | FontStyle.Italic), true);
style.Font = font;
style.TextBrush = new PdfSolidBrush(Color.FromArgb(120, Color.Gray)); // semi-transparent
style.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
style.CellPadding = new PdfPaddings(0, 10, 0, 20);
row.Cells[0].Style = style;

// Hook event for custom underline/overline
grid.EndCellLayout += (sender, args) =>
{
if (args.CellValue == “PROVISIONAL”)
{
RectangleF rect = args.Bounds;
PdfCanvas g = args.Graphics;
float midY = rect.Y + rect.Height / 2;

// Draw overline (top)
g.DrawLine(PdfPens.Gray, rect.Left + 5, rect.Top + 5, rect.Right – 5, rect.Top + 5);

// Draw first underline
g.DrawLine(PdfPens.Gray, rect.Left + 5, midY + font.Size, rect.Right – 5, midY + font.Size);

// Draw second underline (just below)
g.DrawLine(PdfPens.Gray, rect.Left + 5, midY + font.Size + 3, rect.Right – 5, midY + font.Size + 3);
}
};

// Draw grid
grid.Draw(page, new PointF(0, 100));

doc.SaveToFile(“WatermarkStyle.pdf”);
doc.Close();

Share This: