Skip to content
PDF Sharp Xamarin.Forms library for PDF generation
C# Smalltalk
Branch: master
Clone or download
Pull request Compare This branch is 1 commit behind groege:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.nuspec nuspec file update Jul 2, 2018
PdfSharp.Xamarin.Forms.Droid Assembly renderer support added Jun 28, 2018
PdfSharp.Xamarin.Forms.UWP Assembly renderer support added Jun 28, 2018
PdfSharp.Xamarin.Forms.iOS Assembly renderer support added Jun 28, 2018
PdfSharp.Xamarin.Forms Search icon BackgroundColor added Jul 2, 2018
PdfSharp.Xamarin.Sample
PdfSharpCore.Charting
PdfSharpCore Xamarin.Forms, Microsoft.Net.Compilers, Microsoft.NetCore* projects u… Jun 27, 2018
Screenshots
.gitignore Initial commit Aug 20, 2016
PdfSharp.Xamarin.Forms.sln
README.md readme updated for new changes Jun 28, 2018

README.md

PdfSharp.Xamarin.Forms

PdfSharp.Xamarin.Forms is a Xamarin.Forms library for converting any Xamarin.Forms UI into PDF. It uses PdfSharp, which is a partial port of PdfSharpCore.

Supported Platforms

  • UWP
  • Android
  • iOS

Screenshots (see all)

App vs PDF

Usage

  • Init : For each platform you need to init seperately: PdfSharp.Xamarin.Forms.{Platform}.Platform.Init()
  • Generate : var pdf = PDFManager.GeneratePDFFromView(yourView)
  • Save : DependencyService.Get<IPdfSave>().Save(pdf, "pdfName.pdf")

Features

  • Custom Fonts (You should provide Font Types and font files via IPDFHandler)
  • Image rendering
  • Custom renderer ( You can write your own renderer for your customView)
  • Paper size & orientation support
  • Do not render option : by using pdf:PdfRendererAttributes.ShouldRender="False" you can ignore that view in PDF

Limitations

  • Images renders only Jpeg format (It converts PNG to JPEG automatically)
  • ListView does not renders automatically. You should write a renderer.

ListView Rendering

  • Due ListView Cell is not accesible from parent, you should implement a PdfListViewRendererDelegate for the ListView.
  <ContentPage  xmlns:pdf="clr-namespace:PdfSharp.Xamarin.Forms;assembly=PdfSharp.Xamarin.Forms">
  	<ListView pdf:ListRendererDelegate="{StaticResource YourRendererDelegate}" .../>
  </ContentPage>

Renderer:

	public class PDFSampleListRendererDelegate: PdfListViewRendererDelegate
	{
		public override void DrawCell(ListView listView, int section, int row, XGraphics page, XRect bounds, double scaleFactor)
		{
			base.DrawCell(listView, section, row, page, bounds, scaleFactor);
		}

		public override void DrawFooter(ListView listView, int section, XGraphics page, XRect bounds, double scaleFactor)
		{
			base.DrawFooter(listView, section, page, bounds, scaleFactor);
		}

		public override double GetFooterHeight(ListView listView, int section)
		{
			return base.GetFooterHeight(listView, section);
		}
	}

Custom PDF Renderer

Its possible to write your own renderer, it will use it while renderering your View.

Register : PDFManager.RegisterRenderer(typeof(Label), typeof(PDFCustomLabelRenderer))

Render:

	public class PDFCustomLabelRenderer : PdfRendererBase<Label>
	{
		public override void CreatePDFLayout(XGraphics page, Label label, XRect bounds, double scaleFactor)
		{
			XFont font = new XFont(label.FontFamily ?? GlobalFontSettings.FontResolver.DefaultFontName, label.FontSize * scaleFactor);
			Color textColor = label.TextColor != default(Color) ? label.TextColor : Color.Black;

			if (label.BackgroundColor != default(Color))
				page.DrawRectangle(label.BackgroundColor.ToXBrush(), bounds);

			if (!string.IsNullOrEmpty(label.Text))
				page.DrawString(label.Text, font, textColor.ToXBrush(), bounds,
					new XStringFormat()
					{
						Alignment = label.HorizontalTextAlignment.ToXStringAlignment(),
						LineAlignment = label.VerticalTextAlignment.ToXLineAlignment(),
					});
		}
	}
You can’t perform that action at this time.