SoftwareDriven.PdfLib.Blazor
2.3.0
dotnet add package SoftwareDriven.PdfLib.Blazor --version 2.3.0
NuGet\Install-Package SoftwareDriven.PdfLib.Blazor -Version 2.3.0
<PackageReference Include="SoftwareDriven.PdfLib.Blazor" Version="2.3.0" />
<PackageVersion Include="SoftwareDriven.PdfLib.Blazor" Version="2.3.0" />
<PackageReference Include="SoftwareDriven.PdfLib.Blazor" />
paket add SoftwareDriven.PdfLib.Blazor --version 2.3.0
#r "nuget: SoftwareDriven.PdfLib.Blazor, 2.3.0"
#addin nuget:?package=SoftwareDriven.PdfLib.Blazor&version=2.3.0
#tool nuget:?package=SoftwareDriven.PdfLib.Blazor&version=2.3.0
Summary
SoftwareDriven.PdfLib.Blazor is a wrapper library for the JavaScript library pdf-lib (https://pdf-lib.js.org).
The library is designed to be used in Blazor WebAssembly client projects and provides an API that mirrors the functions of pdf-lib.
Installation
Add the JS script to your index.html:
<script src="_content/SoftwareDriven.PdfLib.Blazor/js/index.bundle.js"></script>
Create the service 'Blazor.Services.PdfLib' or register it in your DI container of the client project. It consumes IJSRuntime.
Usage
Here is some example code (sampleImg is an array of byte) to draw a PDF:
await using (var pdfDoc = await pdfLib.CreateDocument())
{
await using (var color = await CreateColor(0, 0, 0))
await using (var pdfFont = await pdfDoc.EmbedFont(StandardFonts.Courier))
await using (var pdfPage = await pdfDoc.AddPage())
await using (var pdfImage = await pdfDoc.EmbedImage(PdfImageType.Png, sampleImg))
{
var textOptions = new PdfDrawTextOptions()
{
LineHeight = 12,
FontSize = 12,
Rotate = 90,
};
var svgOptions = new PdfDrawSvgOptions()
{
BorderColor = color,
};
var height = await pdfFont.HeightAtSize(12);
var width = await pdfFont.WidthOfTextAtSize("Hello World", 12);
Console.WriteLine($"H: {height}, W: {width}");
await pdfPage.DrawImage(pdfImage, 100, 400);
await pdfPage.DrawText(null, 50, 50, textOptions);
await pdfPage.DrawText(string.Empty, 50, 50, textOptions);
await pdfPage.SetSize(1000, 1000);
await pdfPage.DrawText("Test", 50, 50, textOptions);
await pdfPage.DrawSvgPath("M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z", 600, 600, new());
textOptions.Rotate = 0;
await pdfPage.SetFont(pdfFont);
await pdfPage.DrawRectangle(0, 0, 100, 100, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 0.1 });
await pdfPage.DrawRectangle(0, 0, 200, 200, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 0.1 });
await pdfPage.DrawRectangle(0, 0, 300, 300, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 0.1 });
await pdfPage.DrawRectangle(0, 0, 400, 400, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 0.1 });
await pdfPage.DrawRectangle(500, 500, 100, 100, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 0.1 });
await pdfPage.DrawEllipse(500, 500, 100, 100, new PdfDrawEllipseOptions() { BorderColor = color, BorderWidth = 0.1 });
await pdfPage.DrawText("LT", 100, 100, textOptions);
textOptions.HorizontalAlignment = HorizontalAlignment.Center;
await pdfPage.DrawText("CT", 100, 115, textOptions);
textOptions.HorizontalAlignment = HorizontalAlignment.Right;
await pdfPage.DrawText("RT", 100, 130, textOptions);
textOptions.HorizontalAlignment = HorizontalAlignment.Left;
textOptions.VerticalAlignment = VerticalAlignment.Center;
await pdfPage.DrawText("LC", 130, 100, textOptions);
textOptions.VerticalAlignment = VerticalAlignment.Bottom;
await pdfPage.DrawText("LB", 160, 100, textOptions);
textOptions = new PdfDrawTextOptions()
{
LineHeight = 12,
FontSize = 8,
MaxWidth = 50,
};
var text = "This is a long text with line breaks.\nLike this one...";
var size = await pdfFont.MeasureText(text, textOptions);
await pdfPage.DrawText(text, 200, 200, textOptions);
await pdfPage.DrawRectangle(200, 200 - size.Height, size.Width, size.Height, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 1 });
textOptions.HorizontalAlignment = HorizontalAlignment.Center;
textOptions.VerticalAlignment = VerticalAlignment.Center;
await pdfPage.DrawText(text, 300, 200, textOptions);
await pdfPage.DrawRectangle(300 - size.Width / 2.0, 200 - size.Height / 2.0, size.Width, size.Height, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 1 });
textOptions.HorizontalAlignment = HorizontalAlignment.Right;
textOptions.VerticalAlignment = VerticalAlignment.Bottom;
await pdfPage.DrawText(text, 400, 200, textOptions);
await pdfPage.DrawRectangle(400 - size.Width, 200, size.Width, size.Height, new PdfDrawRectangleOptions() { BorderColor = color, BorderWidth = 1 });
}
await pdfDoc.Download("Test.pdf");
}
Here is an example code to merge two PDFs (samplePDF is one PDF as a byte array):
await using (var pdfDoc = await pdfLib.CreateDocument())
await using (var pdfDocTemplate = await pdfLib.LoadDocument(samplePdf))
{
var copiedPages = await pdfDoc.CopyPages(pdfDocTemplate, new int[] { 0 });
await pdfDoc.AddPage(copiedPages[0]);
await pdfDoc.AddPage(copiedPages[0]);
await pdfDoc.AddPage(copiedPages[0]);
await pdfDoc.Download("TestMerge.pdf");
}
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. |
-
net8.0
- Microsoft.AspNetCore.Components (>= 8.0.15)
- Microsoft.AspNetCore.Components.Web (>= 8.0.15)
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 |
---|---|---|
2.3.0 | 63 | 4/30/2025 |
2.2.0 | 1,474 | 11/26/2023 |
2.1.0 | 170 | 9/22/2023 |
2.0.15 | 554 | 11/6/2022 |
2.0.14 | 451 | 9/18/2022 |
2.0.13 | 446 | 9/18/2022 |
2.0.12 | 399 | 8/31/2022 |
2.0.11 | 398 | 8/31/2022 |
2.0.10 | 385 | 8/31/2022 |
2.0.9 | 388 | 8/30/2022 |
2.0.8 | 392 | 8/30/2022 |
2.0.7 | 405 | 8/27/2022 |
2.0.6 | 388 | 8/27/2022 |
2.0.5 | 382 | 8/27/2022 |
2.0.4 | 398 | 8/26/2022 |
2.0.3 | 385 | 8/26/2022 |
2.0.2 | 390 | 8/26/2022 |
2.0.1 | 393 | 8/26/2022 |
2.0.0 | 393 | 8/26/2022 |
1.0.0 | 434 | 5/20/2022 |