PdfSharp is a .NET cross-platform PDF processing framework that uses the MIT open-source license, allowing both personal and commercial use without restrictions on closed-source or open-source projects. PdfSharp is maintained by the community and does not have any paid purchase options.
The author appreciates it for the following reasons:
- Lightweight,
- Simple to operate,
- Cross-platform, with no System.Drawing errors,
- Completely free under the MIT open-source license.
Namespace:
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Snippets.Font;
First, let's write a custom font resolver:
The official default resolver, SegoeWpFontResolver, provides system fonts, so we can wrap an additional layer around it. If a custom font is provided, it will return the custom font; otherwise, it will use the default font lookup logic.
public class MyFontResolver : IFontResolver
{
private readonly Dictionary<string, XFontSource> _fonts = new();
private readonly SegoeWpFontResolver _baseResolver = new SegoeWpFontResolver();
public string AddFont(string fileName)
{
XFontSource fontSource = XFontSource.GetOrCreateFrom(File.ReadAllBytes(fileName));
_fonts.Add(fontSource.FontName, fontSource);
return fontSource.FontName;
}
public byte[]? GetFont(string faceName)
{
if (_fonts.TryGetValue(faceName, out var xFontSource))
{
return xFontSource.Bytes;
}
return _baseResolver.GetFont(faceName);
}
public FontResolverInfo? ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if (_fonts.TryGetValue(familyName, out var xFontSource))
{
return new FontResolverInfo(familyName, isBold, isItalic);
}
return _baseResolver.ResolveTypeface(familyName, isBold, isItalic);
}
}
Next, create a new PDF file, add a page, and edit it:
Using
new PdfDocument();
ornew PdfDocument("my.pdf");
both create a new file and do not operate on the source PDF. This can lead to the source PDF file being cleared. Please see the later sections for the correct editing method.
static void Main()
{
// Load custom font file
var fontResolver = new MyFontResolver();
GlobalFontSettings.FontResolver = fontResolver;
var fontName = fontResolver.AddFont("my.ttf");
var document = new PdfDocument();
document.Info.Title = "Title Test";
document.Info.Subject = "Testing";
// Insert a page
PdfPage page = document.AddPage();
// Graphics drawing object
XGraphics gfx = XGraphics.FromPdfPage(page);
// Draw lines
var width = page.Width;
var height = page.Height;
gfx.DrawLine(XPens.Red, 0, 0, width, height);
gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a shape
var r = width / 5;
gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - r, height / 2 - r, 2 * r, 2 * r));
// Write text
var font = new XFont(fontName, 20, XFontStyleEx.BoldItalic);
gfx.DrawString("Test", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
var filename = "new.pdf";
document.Save(filename);
}
If you need to add a new page to an existing PDF, you can import the PDF file like this:
var document = PdfReader.Open("test.pdf");
For adding a new page, you would use:
PdfPage page = document.AddPage();
If you want to edit a previous page:
PdfPage page = document.Pages[0];
文章评论