Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save documentprocessing/5cc9bad8cb899f4f971368555ae3970b to your computer and use it in GitHub Desktop.

Select an option

Save documentprocessing/5cc9bad8cb899f4f971368555ae3970b to your computer and use it in GitHub Desktop.
Insert Table in a PDF document using PDFSharp for .NET
using PdfSharp.Pdf;
using PdfSharp.Drawing;
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 12, XFontStyle.Regular);
int rows = 5;
int cols = 3;
double cellWidth = 100;
double cellHeight = 30;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
double x = j * cellWidth;
double y = i * cellHeight;
gfx.DrawRectangle(XPens.Black, x, y, cellWidth, cellHeight);
gfx.DrawString($"Cell {i + 1},{j + 1}", font, XBrushes.Black,
new XRect(x, y, cellWidth, cellHeight), XStringFormats.Center);
}
}
document.Save("TableInPDF.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment