PdfSharp.Maui is a Microsoft.Maui library for converting any Maui.View into PDF. It usesPdfSharp.
By following (Demo) folder, you will see screenshots, and PDF view generated from.
- Init :
var pdfManager = new PdfManager()- Generate :
var pdf = pdfManager.GeneratePdfFromView(view, PageOrientation.Landscape, PageSize.A4, PdfStyleUniversal)- Save :
Utils.PdfSave.Save(pdf, "name.pdf","location")
- Universal style and Platform Specific(WYSIWYG) Pdf rendering
- Custom Fonts (You should provide Font while creating
PdfManager)- Image rendering. (It also supports
pngimages)- Custom renderer (Write your own renderer for your own custom view)
- Paper size & orientation support
- Do not render option : by using
pdf:PdfRendererAttributes.ShouldRender="False"you can ignore the view from rendering- Gradient background color support (
LinearGradientBrushandSolidColorBrush)
- It does not support
RadialGradientBrushyet.pngimages will be converted intojpegin android.
Due to template style of these view library only renders as basic listview. Therefore you should implement your own delegate classes derived from
PdfListViewRendererDelegateorPdfCollectionViewRendererDelegate. You will find the sample list view delegate atPdfSharp.Maui.Sample/CustomListViewRendererDelegate.cs
Its possible to write your own renderer, for your own custom view. You should mark your class with
PdfRendererattribute, then you have to create PdfManager by giving the assembly.
[PdfRenderer(ViewType = typeof(YourView))]
public class PdfCustomViewRenderer : PdfRendererBase<YourView>
{
public override void CreatePDFLayout(XGraphics page, YourView view, XRect bounds, double scaleFactor)
{
XFont font = new XFont(GlobalFontSettings.DefaultFontName, 14 * scaleFactor);
page.DrawRectangle(view.GetBackgroundBrush(), bounds);
page.DrawString("Sample text in the page", font, XBrushes.Black, bounds, XStringFormats.Center);
}
}