We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

create PDF MethodAccessException

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

10 Replies

KC Karthikeyan Chandrasekar Syncfusion Team July 30, 2018 12:28 PM UTC

Hi Marco, 
We have tried to reproduce the issue with the provided code snippet in Xamarin Forms. But could not able to reproduce the issue. So created the working sample which refers the following NuGet packages. 

Syncfusion.Xamarin.Core 16.2.0.42 
Syncfusion.Xamarin.Pdf 16.2.0.42 
Syncfusion.Xamarin.Compression 16.2.0.42 
Xamarin.Forms 3.1.0.637273 
 
The required assemblies for creation of PDF in Xamarin can be referred using Syncfusion.Xamarin.Pdf NuGet package, which automatically refer Syncfusion.Xamarin.Compression. Please find the sample for the same from below location, 
 
Can you try with this approach and let us know if there is any concerns. 
Regards, 
Karthikeyan 



MA Marco July 30, 2018 08:23 PM UTC

I tried to remove the compression.NetStandard nuget package and tried your Code.

on the line:
PdfBrush brush = PdfBrushes.Red;

i got the following exception:
Unhandled Exception:
System.MethodAccessException: <Timeout exceeded getting exception details> 

I forgot to mention, that i'm using a NETStandard xamarin app.

I tried to run your solution, but there were just too many Errors, so it couldn't compile.
i had to install syncfusion core, readd the RTL Assembly, install the sfbarcode thingy and there were still 13 Errors, so i gave up.

is the pdf only available for portable or also for .netstandard?

Thank you



KC Karthikeyan Chandrasekar Syncfusion Team August 1, 2018 10:25 AM UTC

Hi Marco, 
Essential PDF library is available in .NET Standard also, you can get the NuGet with the below name. 
 
 
 
Regards, 
Karthikeyan 



MA Marco August 7, 2018 02:19 PM UTC

Thank you 
now it works.


KC Karthikeyan Chandrasekar Syncfusion Team August 7, 2018 02:22 PM UTC

Hi Marco, 
We are happy to know that the solution works for you. You may contact Syncfusion support anytime for further assistance, as always we will be happy to assist you. 

Regards, 
Karthikeyan 



LE LEO August 12, 2018 08:58 PM UTC

Hello,

I've tried to run this demo, but got the exception:

{Java.IO.FileNotFoundException: /storage/emulated/0/Syncfusion/Output.pdf: open failed: ENOENT (No such file or directory) ---> Android.Systems.ErrnoException: open failed: ENOENT (No such file or directory)
   --- End of inner exception stack trace ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <7802aa64ad574c33adca332a3fa9706a>:0
  at Java.Interop.JniPeerMembers+JniInstanceMethods.FinishCreateInstance (System.String constructorSignature, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0004f] in <7802aa64ad574c33adca332a3fa9706a>:0
  at Java.IO.FileOutputStream..ctor (Java.IO.File file) [0x0007a] in <2ea27230a5464cd294f5acb17cd0ebb4>:0
  at RTLDemo.Droid.SaveAndroid.Save (System.String fileName, System.String contentType, System.IO.MemoryStream stream) [0x0005f] in C:\lixo\PdfSample890967754\PdfSample\RTLDemo(2)\RTLDemo\RTLDemo.Android\SaveAndroid.cs:42
  --- End of managed Java.IO.FileNotFoundException stack trace ---
java.io.FileNotFoundException: /storage/emulated/0/Syncfusion/Output.pdf: open failed: ENOENT (No such file or directory)
 at libcore.io.IoBridge.open(IoBridge.java:452)
 at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
 at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
 at md58432a647068b097f9637064b8985a5e0.ButtonRenderer_ButtonClickListener.n_onClick(Native Method)
 at md58432a647068b097f9637064b8985a5e0.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30)
 at android.view.View.performClick(View.java:5714)
 at android.widget.TextView.performClick(TextView.java:10926)
 at android.view.View$PerformClick.run(View.java:22589)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:7325)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
 at libcore.io.Posix.open(Native Method)
 at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
 at libcore.io.IoBridge.open(IoBridge.java:438)
 ... 14 more
}

Can you help me ?


KC Karthikeyan Chandrasekar Syncfusion Team August 13, 2018 11:50 AM UTC

Hi Leo, 
This error may occurs under below scenarios. 
  1. If the PDF assembly is signed and Compression assembly is not signed. We suggest you to add the assemblies from the Nuget to resolve this.
  2. If the application do not have permission to use the storage of the phone, you may change this in the project properties settings.
 
Please try this and let me know if it helps. 
Regards, 
Karthikeyan 



KC Karthikeyan Chandrasekar Syncfusion Team August 14, 2018 07:34 AM UTC

Hi Leo, 
Based on the stack trace, we suspect that the issue may occurs due to “disable of application storage permission”. So kindly make ensure whether you have enabled the application storage permission. We have modified the sample to enable the storage permission of the application. Please find the sample for the same from below location, 

Regards, 
Karthikeyan 



DA David August 31, 2020 05:29 PM UTC

Descarga la prueba y parecía que todo iba bien, porque el celular pedía permiso de escritura, pero cuando acepté me salió el siguiente error

Android.OS.FileUriExposedException: file: ///storage/emulated/0/Syncfusion/Output.pdf expuesto más allá de la aplicación a través de Intent.getData ()


SL Sowmiya Loganathan Syncfusion Team September 1, 2020 12:38 PM UTC

Hi David,   
  
Thank you for contacting Syncfusion support.   
  
We have analyzed the reported issue. If the Android SDK version and target version 23 and above, then we have to use the FileProvider class to give access to the particular file or folder to make them accessible for other apps. So kindly try the provided steps in the below KB or try to run the attached sample in the below KB and let us know the result.   

Regards,  
Sowmiya Loganathan 


Loader.
Live Chat Icon For mobile
Up arrow icon