Hi
I am trying to create a pdf on xamarin.forms.
I Always get the following exception:
Unhandled Exception:
System.MethodAccessException: Method `Syncfusion.Drawing.ColorConverter.FromKnownColor(Syncfusion.Drawing.KnownColor)' is inaccessible from method `Syncfusion.Pdf.Graphics.PdfBrushes.GetBrush(Syncfusion.Drawing.KnownColor)'
when I either set a brush or when I want to save the document to a Memory stream.
I tried two different example codes, but either do not work and i get the same exception.
1)
PdfDocument doc = new PdfDocument();
//Adds a page.
PdfPage page = doc.Pages.Add();
//create a new PDF string format
PdfStringFormat drawFormat = new PdfStringFormat();
drawFormat.WordWrap = PdfWordWrapType.Word;
drawFormat.Alignment = PdfTextAlignment.Justify;
drawFormat.LineAlignment = PdfVerticalAlignment.Top;
//Set the font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f);
//Create a brush.
PdfBrush brush = PdfBrushes.Red;
//bounds
RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height - 20));
//Create a new text elememt
PdfTextElement element = new PdfTextElement("test text", font, brush);
//Set the string format
element.StringFormat = drawFormat;
//Draw the text element
PdfLayoutResult result = element.Draw(page, bounds);
// Draw the string one after another.
result = element.Draw(result.Page, new RectangleF(result.Bounds.X, result.Bounds.Bottom + 10, result.Bounds.Width, result.Bounds.Height));
// Creates a PdfLightTable.
PdfLightTable pdfLightTable = new PdfLightTable();
//Add colums to light table
pdfLightTable.Columns.Add(new PdfColumn("Name"));
pdfLightTable.Columns.Add(new PdfColumn("Age"));
pdfLightTable.Columns.Add(new PdfColumn("Sex"));
//Add row
pdfLightTable.Rows.Add(new string[] { "abc", "21", "Male" });
//Includes the style to display the header of the light table.
pdfLightTable.Style.ShowHeader = true;
//Draws PdfLightTable and returns the rendered bounds.
result = pdfLightTable.Draw(page, new PointF(result.Bounds.Left, result.Bounds.Bottom + 20));
//draw string with returned bounds from table
result = element.Draw(result.Page, result.Bounds.X, result.Bounds.Bottom + 10);
//draw string with returned bounds from table
element.Draw(result.Page, result.Bounds.X, result.Bounds.Bottom + 10);
MemoryStream stream = new MemoryStream();
//Saves the document.
doc.Save(stream);
doc.Close(true);
DependencyService.Get<ISave>().Save("Output.pdf", "application/pdf", stream);
2)
// Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a solid brush
PdfBrush brush = new PdfSolidBrush(new PdfColor(0,0,0,255));
//Set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 36);
//Draw the text
graphics.DrawString("Hello world!", font, brush, new PointF(20, 20));
//create the stream
MemoryStream memoryStream = new MemoryStream();
//save the document into stream
//document.Save(memoryStream); //throws exception
//close the document
document.Close(true);
on 1) i get the exception on Code line 7: PdfBrush brush = PdfBrushes.Red;
on 2) i get it on document.Save
am I missing an Assembly?
Setup:
Xamarin Forms NETStandard 2.0
VS2017e 15.7.5
Syncfusion.Compression.NETStandard 16.2.0.41
Syncfusion.Xamarin.Core 16.2.0.42
Syncfusion..Pdf 16.2.0.42
Xamarin.Forms 3.1.0.637273
Thank you for your help
Yours
Marco Cacciatore