How to use custom font asp.net application.
I have a font file with me. I want to add that file as a resource and use it inside the project.
how to do it?
PrivateFontCollection pfc = new PrivateFontCollection();
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream(Server.MapPath("~/Resources/EAN-13.ttf"));
byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata, 0, (int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed (byte* pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length);
}
}
I tried this. But its not working.
You must distinguish between the code that run on server and the code that run on client browser.
The fonts that you have on your computer, on your server, can not be visible on user computer using the code behind just because you loading the fonts.
So you have two options here.
Load the fonts, and render a text in an image using this fonts, then show this image to the user.
Use client side techniques to show this fonts. Some of them are cross browser font embedding, and other can be javascript that can rendered them on client side.
You can google it with "embed custom fonts website".
Also you can check : http://typeface.neocracy.org/
and Is it possible to use custom fonts - using font-face?
Related
I am developing a WPF C# program that shows some texts in a textblock with several fonts.
Font setting must be portable, so i save user selected font files next to program folder to load each time (in a static-name file).
var textFont = new FontFamily("file:///" + AppDomain.CurrentDomain.BaseDirectory + "/themes/tmp/text/#" + textFontName);
1- how to change font file and set fontfamily from it realtime? (i set fontfamily from file but when it replaced with another font, textblock shows empty characters)
Update: a sample of my code: https://github.com/qwerty13/Wpf-local-font-bug
2- how to install missing font and use it realtime? I am using fontreg.exe but it requires Adminstrator access and restarting program to apply.
I want to display a PDF in my WinForms C# application. I have tried using a WebBrowser component, but it displays control bars from Adobe Reader. I have also tried a component Adobe PDF Reader control axAcroPDF, but using it crashed the responsiveness of my form, other components didn't move when resizing the form (I don't know why). What can I do to either change the WebBrowser component to not display the controls, or display the PDF in some other way?
With Magick.NET, you can convert your pdf to an image, and display it on the form. Also you need the GhostScript to be installed on your PC. Something like this:
using (var image = new MagickImage())
{
private MagickReadSettings _settings;
_settings = new MagickReadSettings()
{
FrameCount = 1, // return only one page
};
_settings.FrameIndex = 1; // return only the first page
_settings.Density = new Density(resolution); // set the resolution
image.Read(DocPath, _settings);
image.ColorAlpha(MagickColors.White);
bmp = image.ToBitmap();
}
Or, you can try this, but I have no experience with that. It is free and has some limitations, but may be it will fit your needs.
I'm currently evaluating Xamarin Forms as an alternative to our webbased HTML applications targeting mobile platforms.
Our applications often use graphical symbols embedded in paragraphs of text.
The desired effect looks like this:
Of course the text also has to be able to freely wrap around, including all the symbols. In HTML this is simply achieved like this:
<p>Sample text with <img src="sample.jpg"> embedded</p>
How can I achieve the same effect using Xamarin Forms? I already looked at FormattedStrings which allow formatting of subparagraphs of Labels, however they do not seem to allow embedding of images.
Also please note that the solution is required to support iOS, Android and Windows Phone 8.1 at least.
Obviously being forced to use a WebView almost defeats the point of moving a HTML5 app to Xamarin!
In the Xamarin Forums a similar question has been asked: https://forums.xamarin.com/discussion/1649/text-with-image
To solve the problem Tomasz Cielecki cooked up some sample code here: https://gist.github.com/Cheesebaron/5034440
He then went onto blog about it here:
http://blog.ostebaronen.dk/2013/02/adding-images-to-textview-and-edittext.html
<snip>
I started out with a super simple sample trying to get an Image shown in a TextView. I googled up some solutions and sure, Spannables allow using ImageSpan inside of them! There were nice samples and such, and I came up with this.
ImageSpan in TextView
//Load up your drawable.
var imageSpan = new ImageSpan(this, Resource.Drawable.Icon);
//Set the text of SpannableString from TextView
var spannableString = new SpannableString(textView.Text);
//Add image at end of string
spannableString.SetSpan(imageSpan, textView.Text.Length-1, textView.Text.Length, 0);
Easy, huh? And you can add loads of other Spans to the Spannable, such as StyleSpan, which you can style your fonts with bold, italic and other styles.
Since that was so easy, I quickly tried to do that with an EditText. It also works just fine...
</snip>
Currently, the only way to have symbols and images within text blocks is to use WebView (https://developer.xamarin.com/guides/xamarin-forms/working-with/webview/)
var browser = new WebView();
var htmlSource = new HtmlWebViewSource ();
htmlSource.Html = #"<html><body>
<h1>Xamarin.Forms</h1>
<p>Welcome to WebView.</p>
</body></html>";
browser.Source = htmlSource;
I have a pdf file created with itextsharp with images in the file. I would like to put a hyperlink in the file that if you pick the picture it will open that picture in a picture viewer. I can set a hyperlink to a web address but have no idea how to get it to open a file. Below is the code, yes I know that c:\test.jpg is a bad hardcoded file name but it is just a test. When you click the picture it does nothing but I have no idea how to tell it what to do.
iTextSharp.text.Image pic =TextSharp.text.Image.GetInstance(comment.examplePic);
pic.ScaleToFit(200f, 200f);
Chunk cImage = new Chunk(pic, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "c:\\test.jpg";
doc.Add(pic);
doc.Add(anchor);
A PDF is self-contained. This means that all the resources needed to show the PDF are (usually) stored inside the PDF (exceptions are for instance fonts that can be retrieved from the operating system).
When you have an image that is shown on a PDF page, the bytes of that image are stored in what we call an Image XObject. An XObject is an object that is external to the page, but that is stored as a separate object inside the PDF file.
You are asking to serve the image bytes stored inside this separate object to a viewer on the operating system. This is impossible. I don't know of any viewer that can take those bytes and somehow forward them to an image viewer.
I can think of three possible workarounds. I don't know if any of these workarounds is acceptable to you.
1. Serve the image online
You could put the image on a server and use the code you have in your snippet to link to that online image. Of course: this will only work if the person viewing the document is online and clicks OK when his viewer asks him if it's OK to link to a resources on the internet.
2. Serve the image as an annotation
In this case, you create an annotation for which you create an appearance that renders that same image XObject in the annotation layer (all annotations are shown on top of the page content). You can easily change the visibility status of an annotation to make it invisible (in your case, this would be the default status) or visible (in your case, this would be triggered by a JavaScript action when clicking the link).
There's an example of such an annotation here: Advertisement. If you open advertisement.pdf, you see an image with a button that says "Close this advertisement". Once you click that, the status of the annotation will be changed to invisible. You could do something similar, but the other way round: click a link to make it visible instead of invisible.
This solution doesn't depend on an external viewer, the image is shown in the PDF viewer.
3. Add the image as optional content
Starting with PDF 1.5, PDF supports optional content. See for instance the OptionalContentExample. In this example, we have some questions and answers, but the answers are not visible by default. See layer_actions.pdf. There are links "on / off / toggle" to make the answers visible or invisible.
You could do the same with images: you could add them to a layer that is invisible by default, but that can be made visible if somebody clicks a link. However: this requires a viewer that supports OCG (optional content groups) and the actions to change the status of these OCGs. For instance: if you would try the layer_actions.pdf example in the PDF viewer in Chrome, it won't work, but if you download the PDF and open it in Adobe Reader, you'll see the behavior I described.
Summarized:
You are asking something that is impossible, but there are workarounds. Please post another question if you have chosen a workaround and you don't succeed in making that workaround word (but please take into account that not all viewers support every workaround).
no offence but too much knowledge sometimes makes you ignorant of small things.
simple solution to this problem is here
http://kuujinbo.info/iTextSharp/imageAnchor.aspx
sample code that i implemented works like charm
PdfPCell p1 = new PdfPCell();
p1 = new PdfPCell();
p1.Padding = 0;
p1.Border = 0;
PdfPTable nav = new PdfPTable(1);
nav.WidthPercentage = 100;
nav.SpacingAfter = 12;
navbarImg.Annotation= new Annotation(0, 0, 0, 0, ur);
p1.Image = navbarImg;
nav.AddCell(p1);
_doc.Add(nav);
I don't know how to put all the text styles in dropdownlist in asp.net . can you help me ? I'm new in it
Use System.Drawing.Text.InstalledFontCollection class to list all fonts installed on server.
Note that some these fonts may not be available on client side.
using System.Drawing.Text;
...
InstalledFontCollection inst = new InstalledFontCollection();
foreach (FontFamily fnt in inst.Families)
{
comboBox.Items.Add(fnt.Name);
}
If you want to use the server's fonts and don't care if the client has them, then use Alexander's answer to get a list of available fonts.
If you want to display fonts that the client has available, then you can use flash.
Source:
https://github.com/gabriel/font-detect-js
Demo:
http://font-detect.s3.amazonaws.com/index.html