How to get the font path in the Assets folder












0















I create a PDF document using itextsharp and I need to connect the fonts from the Assets folder.
At the moment everything works when I throw fonts in the memory of the smartphone.



System.String ttf = Android.OS.Environment.ExternalStorageDirectory + 
"/arial.ttf";
var baseFont = BaseFont.CreateFont(tttt, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
var font = new Font(baseFont, Font.DEFAULTSIZE, Font.NORMAL);

doc.Add(new Paragraph(inf1, font));


But probably no one has these fonts in the memory of the smartphone :)



I tried to get the path to the Assets folder using the following code:



public static AssetManager Assets { get; private set; }

string ttf;
using (var asset = Assets.Open("arial.ttf"))
{
var fontStream = new MemoryStream();
asset.CopyTo(fontStream);
}


or another option:



string content;
using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))
{
content = sr.ReadToEnd();
}


but in these cases there is a problem:
object reference not set to an instance of an object...
on line



using (var asset = Assets.Open("arial.ttf")) 


and



using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))


Maybe there is some analog of the folder path, similar to the path to the Font in Windows ?



Environment.GetFolderPath(Environment.SpecialFolder.Fonts);


or maybe I can somehow use the resulting TypeFace



Android.Graphics.Typeface tf = Android.Graphics.Typeface.CreateFromAsset(Application.Context.Assets, "arial.ttf"); 


Tell me, please, how do I set the necessary fonts in a PDF document or how to find the path to the fonts ttf, that would be inserted into the line BaseFont :)










share|improve this question

























  • Try whether "file:///android_asset/fileName" is the right path

    – Leo Zhu - MSFT
    Jan 3 at 7:49











  • @LeoZhu Doesn't find such a path :( And similar too

    – Игорь Ибрагимов
    Jan 5 at 14:58


















0















I create a PDF document using itextsharp and I need to connect the fonts from the Assets folder.
At the moment everything works when I throw fonts in the memory of the smartphone.



System.String ttf = Android.OS.Environment.ExternalStorageDirectory + 
"/arial.ttf";
var baseFont = BaseFont.CreateFont(tttt, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
var font = new Font(baseFont, Font.DEFAULTSIZE, Font.NORMAL);

doc.Add(new Paragraph(inf1, font));


But probably no one has these fonts in the memory of the smartphone :)



I tried to get the path to the Assets folder using the following code:



public static AssetManager Assets { get; private set; }

string ttf;
using (var asset = Assets.Open("arial.ttf"))
{
var fontStream = new MemoryStream();
asset.CopyTo(fontStream);
}


or another option:



string content;
using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))
{
content = sr.ReadToEnd();
}


but in these cases there is a problem:
object reference not set to an instance of an object...
on line



using (var asset = Assets.Open("arial.ttf")) 


and



using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))


Maybe there is some analog of the folder path, similar to the path to the Font in Windows ?



Environment.GetFolderPath(Environment.SpecialFolder.Fonts);


or maybe I can somehow use the resulting TypeFace



Android.Graphics.Typeface tf = Android.Graphics.Typeface.CreateFromAsset(Application.Context.Assets, "arial.ttf"); 


Tell me, please, how do I set the necessary fonts in a PDF document or how to find the path to the fonts ttf, that would be inserted into the line BaseFont :)










share|improve this question

























  • Try whether "file:///android_asset/fileName" is the right path

    – Leo Zhu - MSFT
    Jan 3 at 7:49











  • @LeoZhu Doesn't find such a path :( And similar too

    – Игорь Ибрагимов
    Jan 5 at 14:58
















0












0








0








I create a PDF document using itextsharp and I need to connect the fonts from the Assets folder.
At the moment everything works when I throw fonts in the memory of the smartphone.



System.String ttf = Android.OS.Environment.ExternalStorageDirectory + 
"/arial.ttf";
var baseFont = BaseFont.CreateFont(tttt, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
var font = new Font(baseFont, Font.DEFAULTSIZE, Font.NORMAL);

doc.Add(new Paragraph(inf1, font));


But probably no one has these fonts in the memory of the smartphone :)



I tried to get the path to the Assets folder using the following code:



public static AssetManager Assets { get; private set; }

string ttf;
using (var asset = Assets.Open("arial.ttf"))
{
var fontStream = new MemoryStream();
asset.CopyTo(fontStream);
}


or another option:



string content;
using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))
{
content = sr.ReadToEnd();
}


but in these cases there is a problem:
object reference not set to an instance of an object...
on line



using (var asset = Assets.Open("arial.ttf")) 


and



using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))


Maybe there is some analog of the folder path, similar to the path to the Font in Windows ?



Environment.GetFolderPath(Environment.SpecialFolder.Fonts);


or maybe I can somehow use the resulting TypeFace



Android.Graphics.Typeface tf = Android.Graphics.Typeface.CreateFromAsset(Application.Context.Assets, "arial.ttf"); 


Tell me, please, how do I set the necessary fonts in a PDF document or how to find the path to the fonts ttf, that would be inserted into the line BaseFont :)










share|improve this question
















I create a PDF document using itextsharp and I need to connect the fonts from the Assets folder.
At the moment everything works when I throw fonts in the memory of the smartphone.



System.String ttf = Android.OS.Environment.ExternalStorageDirectory + 
"/arial.ttf";
var baseFont = BaseFont.CreateFont(tttt, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
var font = new Font(baseFont, Font.DEFAULTSIZE, Font.NORMAL);

doc.Add(new Paragraph(inf1, font));


But probably no one has these fonts in the memory of the smartphone :)



I tried to get the path to the Assets folder using the following code:



public static AssetManager Assets { get; private set; }

string ttf;
using (var asset = Assets.Open("arial.ttf"))
{
var fontStream = new MemoryStream();
asset.CopyTo(fontStream);
}


or another option:



string content;
using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))
{
content = sr.ReadToEnd();
}


but in these cases there is a problem:
object reference not set to an instance of an object...
on line



using (var asset = Assets.Open("arial.ttf")) 


and



using (StreamReader sr = new StreamReader(Assets.Open("arial.ttf")))


Maybe there is some analog of the folder path, similar to the path to the Font in Windows ?



Environment.GetFolderPath(Environment.SpecialFolder.Fonts);


or maybe I can somehow use the resulting TypeFace



Android.Graphics.Typeface tf = Android.Graphics.Typeface.CreateFromAsset(Application.Context.Assets, "arial.ttf"); 


Tell me, please, how do I set the necessary fonts in a PDF document or how to find the path to the fonts ttf, that would be inserted into the line BaseFont :)







fonts xamarin.android itext android-assets






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 14:34







Игорь Ибрагимов

















asked Jan 2 at 16:16









Игорь ИбрагимовИгорь Ибрагимов

12




12













  • Try whether "file:///android_asset/fileName" is the right path

    – Leo Zhu - MSFT
    Jan 3 at 7:49











  • @LeoZhu Doesn't find such a path :( And similar too

    – Игорь Ибрагимов
    Jan 5 at 14:58





















  • Try whether "file:///android_asset/fileName" is the right path

    – Leo Zhu - MSFT
    Jan 3 at 7:49











  • @LeoZhu Doesn't find such a path :( And similar too

    – Игорь Ибрагимов
    Jan 5 at 14:58



















Try whether "file:///android_asset/fileName" is the right path

– Leo Zhu - MSFT
Jan 3 at 7:49





Try whether "file:///android_asset/fileName" is the right path

– Leo Zhu - MSFT
Jan 3 at 7:49













@LeoZhu Doesn't find such a path :( And similar too

– Игорь Ибрагимов
Jan 5 at 14:58







@LeoZhu Doesn't find such a path :( And similar too

– Игорь Ибрагимов
Jan 5 at 14:58














1 Answer
1






active

oldest

votes


















0














The problem with your above code in my understanding when you say it gives you object reference not set to an instance of an object. error, which means that you are not assigning a value to it and its null what you need to do to overcome that is simple;



After declaring the property



 public AssetManager AppAssets { get; private set; }


In your class constructor assign it a value:



AppAssets= this.ApplicationContext.Assets; // In an activity
AppAssets= this.Activity.ApplicationContext.Assets; // In a fragment


Once you do this the object reference error should go away,



Goodluck revert in case of queries.






share|improve this answer
























  • It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

    – Игорь Ибрагимов
    Jan 5 at 14:55











  • Can you explain your end goal and then maybe I can help you better?

    – G.hakim
    Jan 7 at 5:58











  • This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

    – Игорь Ибрагимов
    Jan 8 at 11:50













  • Ignore the exception what is your end goal what are you trying to achieve here

    – G.hakim
    Jan 8 at 12:16













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009662%2fhow-to-get-the-font-path-in-the-assets-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The problem with your above code in my understanding when you say it gives you object reference not set to an instance of an object. error, which means that you are not assigning a value to it and its null what you need to do to overcome that is simple;



After declaring the property



 public AssetManager AppAssets { get; private set; }


In your class constructor assign it a value:



AppAssets= this.ApplicationContext.Assets; // In an activity
AppAssets= this.Activity.ApplicationContext.Assets; // In a fragment


Once you do this the object reference error should go away,



Goodluck revert in case of queries.






share|improve this answer
























  • It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

    – Игорь Ибрагимов
    Jan 5 at 14:55











  • Can you explain your end goal and then maybe I can help you better?

    – G.hakim
    Jan 7 at 5:58











  • This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

    – Игорь Ибрагимов
    Jan 8 at 11:50













  • Ignore the exception what is your end goal what are you trying to achieve here

    – G.hakim
    Jan 8 at 12:16


















0














The problem with your above code in my understanding when you say it gives you object reference not set to an instance of an object. error, which means that you are not assigning a value to it and its null what you need to do to overcome that is simple;



After declaring the property



 public AssetManager AppAssets { get; private set; }


In your class constructor assign it a value:



AppAssets= this.ApplicationContext.Assets; // In an activity
AppAssets= this.Activity.ApplicationContext.Assets; // In a fragment


Once you do this the object reference error should go away,



Goodluck revert in case of queries.






share|improve this answer
























  • It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

    – Игорь Ибрагимов
    Jan 5 at 14:55











  • Can you explain your end goal and then maybe I can help you better?

    – G.hakim
    Jan 7 at 5:58











  • This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

    – Игорь Ибрагимов
    Jan 8 at 11:50













  • Ignore the exception what is your end goal what are you trying to achieve here

    – G.hakim
    Jan 8 at 12:16
















0












0








0







The problem with your above code in my understanding when you say it gives you object reference not set to an instance of an object. error, which means that you are not assigning a value to it and its null what you need to do to overcome that is simple;



After declaring the property



 public AssetManager AppAssets { get; private set; }


In your class constructor assign it a value:



AppAssets= this.ApplicationContext.Assets; // In an activity
AppAssets= this.Activity.ApplicationContext.Assets; // In a fragment


Once you do this the object reference error should go away,



Goodluck revert in case of queries.






share|improve this answer













The problem with your above code in my understanding when you say it gives you object reference not set to an instance of an object. error, which means that you are not assigning a value to it and its null what you need to do to overcome that is simple;



After declaring the property



 public AssetManager AppAssets { get; private set; }


In your class constructor assign it a value:



AppAssets= this.ApplicationContext.Assets; // In an activity
AppAssets= this.Activity.ApplicationContext.Assets; // In a fragment


Once you do this the object reference error should go away,



Goodluck revert in case of queries.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 7:13









G.hakimG.hakim

4,97411136




4,97411136













  • It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

    – Игорь Ибрагимов
    Jan 5 at 14:55











  • Can you explain your end goal and then maybe I can help you better?

    – G.hakim
    Jan 7 at 5:58











  • This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

    – Игорь Ибрагимов
    Jan 8 at 11:50













  • Ignore the exception what is your end goal what are you trying to achieve here

    – G.hakim
    Jan 8 at 12:16





















  • It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

    – Игорь Ибрагимов
    Jan 5 at 14:55











  • Can you explain your end goal and then maybe I can help you better?

    – G.hakim
    Jan 7 at 5:58











  • This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

    – Игорь Ибрагимов
    Jan 8 at 11:50













  • Ignore the exception what is your end goal what are you trying to achieve here

    – G.hakim
    Jan 8 at 12:16



















It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

– Игорь Ибрагимов
Jan 5 at 14:55





It seems to have helped. But I realized that StreamReader won't really help me, because I need to get the path to the file, not read It :)

– Игорь Ибрагимов
Jan 5 at 14:55













Can you explain your end goal and then maybe I can help you better?

– G.hakim
Jan 7 at 5:58





Can you explain your end goal and then maybe I can help you better?

– G.hakim
Jan 7 at 5:58













This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

– Игорь Ибрагимов
Jan 8 at 11:50







This result in the read path of the stream: iTextSharp.text.DocumentException: <Timeout exceeded getting exception details>. Therefore, I think that a different approach is needed.

– Игорь Ибрагимов
Jan 8 at 11:50















Ignore the exception what is your end goal what are you trying to achieve here

– G.hakim
Jan 8 at 12:16







Ignore the exception what is your end goal what are you trying to achieve here

– G.hakim
Jan 8 at 12:16






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009662%2fhow-to-get-the-font-path-in-the-assets-folder%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules