ImageMagick is a powerful image processing library that supports over 100 major file formats (excluding sub-formats). Using Magick.NET, you can use ImageMagick in C#/VB.NET/.NET Core applications without the need to install ImageMagick on the server or desktop.
Project address: https://github.com/dlemstra/Magick.NET
The reasons I recommend it are not only its cross-platform capabilities and open-source nature, but also its simple and user-friendly API. You can reference it by searching for ImageMagick on NuGet.
using ImageMagick;
using Microsoft.IO;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Snippets.Font;
namespace ConsoleAppTest
{
internal class Program
{
private static readonly RecyclableMemoryStreamManager MemoryManager = new();
static async Task Main()
{
var document = new PdfDocument();
document.Info.Title = "标题测试";
document.Info.Subject = "测试";
// Insert a page
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
// Load gif file
using var image = new MagickImage(File.Open("D:/a.gif", FileMode.Open));
// Convert and store png in memory
var memoryStream = MemoryManager.GetStream();
image.Write(memoryStream,MagickFormat.Png);
// Reset stream pointer to the beginning position
memoryStream.Seek(0, SeekOrigin.Begin);
XImage img = XImage.FromStream(memoryStream);
// Image is drawn at the position on the page, coordinates 0,0
gfx.DrawImage(img, new XPoint(0, 0));
var filename = "new.pdf";
document.Save(filename);
}
}
For the usage of PdfSharp, please refer to: https://www.whuanle.cn/archives/21233
Printed to PDF:
文章评论