PDFsharp Library is a .NET library for processing PDF files. You create PDF pages using drawing routines known from GDI+ (WinForms). Almost anything that can be done with GDI+ will also work with PDFsharp. Only basic text layout is supported by PDFsharp, and page breaks are not created automatically. The same drawing routines can be used for screen, PDF, or meta files.
staticvoidTest1() { // Create a new PDF document. var document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; document.Info.Subject = "Just a simple Hello-World program.";
// Create an empty page in this document. var page = document.AddPage(); //page.Size = PageSize.Letter;
// Get an XGraphics object for drawing on this page. var gfx = XGraphics.FromPdfPage(page);
// Draw two lines with a red default pen. var width = page.Width.Point; var height = page.Height.Point; gfx.DrawLine(XPens.Red, 0, 0, width, height); gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a circle with a red pen which is 1.5 point thick. 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)); // Create a font. var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
// Draw the text. gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.Center); // Save the document... var filename = PdfFileUtility.GetTempPdfFullFileName("samples/HelloWorldSample"); document.Save(filename); // ...and start a viewer. PdfFileUtility.ShowDocument(filename); }
staticvoidTest2() { GlobalFontSettings.FontResolver = new CustomFontResolver(); GlobalFontSettings.FallbackFontResolver = new FailsafeFontResolver(); // Create a new PDF document. var document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; document.Info.Subject = "Just a simple Hello-World program.";
// Create an empty page in this document. var page = document.AddPage(); //page.Size = PageSize.Letter;
// Get an XGraphics object for drawing on this page. var gfx = XGraphics.FromPdfPage(page);
// Draw two lines with a red default pen. var width = page.Width.Point; var height = page.Height.Point; gfx.DrawLine(XPens.Red, 0, 0, width, height); gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a circle with a red pen which is 1.5 point thick. 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));
// Create a font. var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
// Draw the text. gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black, new XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.Center);
gfx.DrawString("你好 PDF Sharp!", font, XBrushes.Black, new XRect(0, 25, page.Width.Point, page.Height.Point), XStringFormats.Center);
// Save the document... var filename = PdfFileUtility.GetTempPdfFullFileName("samples/HelloWorldSample"); document.Save(filename); // ...and start a viewer. PdfFileUtility.ShowDocument(filename); }
static MemoryStream GenQRImgMem(string qrstr,int size) { MemoryStream memory = new MemoryStream(); var qrobj = new QRCodeGenerator(); var qr = qrobj.CreateQrCode(qrstr, ECCLevel.M); var info = new SKImageInfo(size, size); using (var surface = SKSurface.Create(info)) { var canvas = surface.Canvas;
// 输出到文件 using (var image = surface.Snapshot()) using (var data = image.Encode(SKEncodedImageFormat.Png, 100)) { data.SaveTo(memory); memory.Position = 0; } } return memory; } staticvoidTest3() { // Create a new PDF document. var document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; document.Info.Subject = "Just a simple Hello-World program.";
// Create an empty page in this document. var page = document.AddPage(); //page.Size = PageSize.Letter;
// Get an XGraphics object for drawing on this page. var gfx = XGraphics.FromPdfPage(page);
// Draw two lines with a red default pen. var width = page.Width.Point; var height = page.Height.Point; gfx.DrawLine(XPens.Red, 0, 0, width, height); gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a circle with a red pen which is 1.5 point thick. 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));
// Create a font. var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
// Draw the image int qr_size = 300; var imgMem = GenQRImgMem("你好 PDF Sharp!", qr_size); int x = (int)(page.Width.Point - qr_size) / 2; int y = (int)(page.Height.Point - qr_size) / 2; gfx.DrawImage(XImage.FromStream(imgMem), x, y);
// Save the document... var filename = PdfFileUtility.GetTempPdfFullFileName("samples/HelloWorldSample"); document.Save(filename); // ...and start a viewer. PdfFileUtility.ShowDocument(filename); }