m2nj.PdfHelper
1.0.0
dotnet add package m2nj.PdfHelper --version 1.0.0
NuGet\Install-Package m2nj.PdfHelper -Version 1.0.0
<PackageReference Include="m2nj.PdfHelper" Version="1.0.0" />
<PackageVersion Include="m2nj.PdfHelper" Version="1.0.0" />
<PackageReference Include="m2nj.PdfHelper" />
paket add m2nj.PdfHelper --version 1.0.0
#r "nuget: m2nj.PdfHelper, 1.0.0"
#:package m2nj.PdfHelper@1.0.0
#addin nuget:?package=m2nj.PdfHelper&version=1.0.0
#tool nuget:?package=m2nj.PdfHelper&version=1.0.0
m2nj.PdfHelper
m2nj.PdfHelper is a .NET 8 library for creating PDFs from pages containing images, text, tables, and vector shapes. This stable release is version 1.0.0.
Install
dotnet add package m2nj.PdfHelper --version 1.0.0
Document and page setup
Create a document, set its PDF version before adding the first page, then add one or more pages. GeneratePdf() returns the complete PDF as a byte[].
using m2nj.PdfHelper;
using var document = PdfContent.CreateDocument();
document.PdfVersion = PdfVersion.V1_7;
var page = document.AddPage();
page.Size = PdfPageSize.A4;
page.Layout = PdfPageLayout.Portrait;
page.Margin = new PdfMargins(left: 36, top: 36, right: 36, bottom: 36);
byte[] pdfBytes = document.GeneratePdf();
File.WriteAllBytes("document.pdf", pdfBytes);
Available PDF versions are V1_4, V1_5, V1_6, V1_7, and V2_0. Predefined page sizes are A3, A4, A5, Letter, and Legal. For a custom page, set Size = PdfPageSize.Custom, then set both CustomWidthPixels and CustomHeightPixels. Layout can be Portrait or Landscape; predefined dimensions rotate for landscape, while custom dimensions are used exactly as supplied.
Margins and element coordinates are in pixels. The PDF page box is converted to points at 96 DPI ($72$ PDF points per inch). Coordinates use a top-left origin.
Elements
Elements are added to a page through page.Elements.Add(...). The ZOrder property determines paint order: lower values are drawn first, higher values later. A background image can use ZOrder = -1 to sit behind ordinary elements at 0. Every PdfElement also has RotationDegrees, a clockwise rotation from 0 to 360 degrees applied around the center of its bounds.
Images: PdfImage and PdfImageOptions
Use page.AddImage(bytes, options) to add an image. Width, Height, X, and Y are required pixel values; ZOrder is optional and defaults to 0.
var bytes = File.ReadAllBytes("background.png");
page.AddImage(bytes, new PdfImageOptions
{
Width = 500,
Height = 300,
X = 40,
Y = 40,
ZOrder = -1,
RotationDegrees = 5
});
Other elements use the same inherited property with an object initializer:
page.Elements.Add(new PdfRectangle(100, 100, 200, 80, new PdfColor(240, 245, 250))
{
RotationDegrees = 12
});
The current image pipeline converts input images to JPEG for PDF embedding. It uses System.Drawing.Common, so image conversion is currently Windows-supported. Transparent input is flattened against white.
Single text: PdfText
PdfText places one text run at an explicit top-left pixel position. Supply text, font, color, x, and y.
page.Elements.Add(new PdfText(
"Invoice",
new PdfFont("Helvetica", 28, PdfFontWeight.Bold),
new PdfColor(25, 58, 96),
x: 48,
y: 48));
Fonts: PdfFont, PdfFontWeight
Built-in PDF font families are Helvetica, Times, and Courier. Aliases include Arial, Times New Roman, and Courier New. PdfFontWeight supports Normal and Bold; italic: true selects the italic face.
var headingFont = new PdfFont("Times New Roman", 24, PdfFontWeight.Bold, italic: true);
TrueType .ttf fonts can be loaded from a file path or through PdfFont.FromFile(path, size). PdfFont.GetBytes(path) reads a font file into bytes for use with the fontBytes constructor argument. The current writer embeds TrueType font bytes; OpenType CFF .otf files are not supported. Font measurement for embedded fonts is approximate, so precise line breaks may differ from the selected font's actual metrics.
var localFont = PdfFont.FromFile("fonts/Example.ttf", size: 14);
page.Elements.Add(new PdfText("Using a local TTF", localFont, PdfColor.Black, x: 48, y: 96));
Wrapped paragraph: PdfParagraph
PdfParagraph wraps text to its required width. Options include PdfTextAlignment (Left, Center, Right, Justified), lineSpacing, spaceBefore, spaceAfter, firstLineIndent, leftIndent, and rightIndent. allowPageBreak defaults to true; when enabled, long paragraphs continue onto generated pages using the source page's size, layout, and margins.
page.Elements.Add(new PdfParagraph(
"A paragraph wraps at the requested width and can continue on later pages.",
new PdfFont("Helvetica", 14),
new PdfColor(55, 66, 78),
x: 48,
y: 150,
width: 500,
alignment: PdfTextAlignment.Left,
lineSpacing: 1.2,
spaceAfter: 12,
firstLineIndent: 16,
allowPageBreak: true));
Mixed formatting: PdfRichText and PdfTextRun
Use an ordered list of PdfTextRun values for different fonts or colors within one wrapped text block.
page.Elements.Add(new PdfRichText(
new[]
{
new PdfTextRun("Total: ", new PdfFont("Helvetica", 18)),
new PdfTextRun("$125.00", new PdfFont("Helvetica", 18, PdfFontWeight.Bold), new PdfColor(20, 120, 75))
},
x: 48,
y: 220,
width: 400,
alignment: PdfTextAlignment.Right));
Tables: PdfTable, PdfTableRow, and PdfTableCell
A PdfTable takes an origin and total width, plus optional table border color/width, repeatHeader, allowPageBreak, and zOrder. Rows are added with AddRow; each row has a fixed pixel height, optional background, IsHeader, and PageBreakBefore/PageBreakAfter flags. Cells are added with AddCell and can specify content, per-side pixel padding, borders, horizontal and vertical alignment, background, and RowSpan/ColumnSpan.
Rows remain intact when a table continues onto another page. Rows marked IsHeader = true are repeated on continuation pages when RepeatHeader is enabled. allowPageBreak must be enabled for this pagination behavior. At present, the table width is divided evenly into columns based on the maximum cell span in a row; individually sized columns are not yet configurable.
var table = new PdfTable(
x: 48,
y: 280,
width: 500,
borderColor: new PdfColor(110, 130, 150),
borderWidth: 1,
repeatHeader: true,
allowPageBreak: true);
var header = new PdfTableRow(height: 32, backgroundColor: new PdfColor(31, 75, 120), isHeader: true);
var whiteBold = new PdfFont("Helvetica", 12, PdfFontWeight.Bold);
header.AddCell(new PdfTableCell(
padding: new PdfMargins(6),
backgroundColor: new PdfColor(31, 75, 120))
.Add(new PdfText("Item", whiteBold, new PdfColor(255, 255, 255), x: 0, y: 0)));
header.AddCell(new PdfTableCell(
padding: new PdfMargins(6),
alignment: PdfTextAlignment.Right,
columnSpan: 2)
.Add(new PdfText("Amount", whiteBold, new PdfColor(255, 255, 255), x: 0, y: 0)));
table.AddRow(header);
var row = new PdfTableRow(height: 30, backgroundColor: new PdfColor(245, 248, 252));
row.AddCell(new PdfTableCell(padding: new PdfMargins(6))
.Add(new PdfText("Service", new PdfFont("Helvetica", 12), PdfColor.Black, x: 0, y: 0)));
row.AddCell(new PdfTableCell(padding: new PdfMargins(6), columnSpan: 2, alignment: PdfTextAlignment.Right)
.Add(new PdfText("$125.00", new PdfFont("Helvetica", 12), PdfColor.Black, x: 0, y: 0)));
table.AddRow(row);
page.Elements.Add(table);
Cell content can contain PdfText, PdfParagraph, PdfRichText, images, lines, and rectangles. The cell applies its own padding and alignment to the content. Row spans occupy their columns in following rows; ensure the remaining cells fit the table's computed column count.
Lines: PdfLine
PdfLine draws a separator or signature rule from (startX, startY) to (endX, endY). Coordinates and thickness are pixels.
page.Elements.Add(new PdfLine(48, 740, 250, 740, new PdfColor(90, 110, 130), thickness: 2));
Rectangles: PdfRectangle
PdfRectangle draws a positioned box with optional fillColor, borderColor, and borderWidth. Use zOrder to place it behind or in front of other elements.
page.Elements.Add(new PdfRectangle(
x: 40,
y: 40,
width: 515,
height: 760,
fillColor: new PdfColor(248, 250, 252),
borderColor: new PdfColor(70, 110, 150),
borderWidth: 2,
zOrder: -1));
Sample project
The NuGet package includes a runnable consumer sample in samples/PdfHelper.Sample. It demonstrates images, standard and optional local TrueType fonts, text and rich text, wrapped paragraphs, tables with repeated headers, shapes, page orientation, custom page dimensions, and element rotation. To try a local font, place a licensed testFont.ttf beside the sample project; otherwise the sample uses Helvetica.
After unpacking the package, run:
dotnet run --project samples/PdfHelper.Sample/PdfHelper.Sample.csproj
Maintainer and license
Created and maintained by m2nj. Package ownership and copyright: m2nj, 2026. Use and redistribution are governed by the custom license in License.txt; it is not an OSI-approved open-source license. Review the terms before adopting or redistributing the package.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- System.Drawing.Common (>= 8.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 128 | 10/1/2026 |