如何包含code39字体ttf文件并使用它

本文关键字:文件 ttf 何包含 code39 字体 | 更新日期: 2023-09-27 17:49:39

我使用code39来显示条形码。但由于某些原因,我无法使用下面的code39字体。我已将ttf文件包含在字体文件夹中。

@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/fonts")

    <table class="table">
        <tr>
            @{
                string size = "0px";
                int co = Model.BarCodeItems.FirstOrDefault().Columns;
                if (co <= 2)
                {
                    size = "90px";
                }
                if (co > 2 || co < 5)
                {
                    size = "70px";
                }
                if (co >= 5)
                {
                    size = "40px";
                }
                int count = 0;
            }
            @foreach (var items in Model.BarCodeItems)
            {

                <td style="text-align:center">
                    <p style="font-family:Code39r;font-size:@size"> @items.BarCode</p>
                    <p> @items.BarCode</p>
                    @*<ol class="" style="width:5px">@items.BarCode.Replace("*", "")</ol>*@
                </td>
                count++;
                if (count == @items.Columns)
                {
                <tr></tr>
                    count = 0;
                }


            }
            </tr>
        </table>

下面是我的bundle配置,我已经将它添加到bundle配置中,并在我的页面中呈现它。

public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));
        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
        "~/Scripts/jquery-ui-{version}.js"));
        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
          "~/Content/themes/base/jquery.ui.datepicker.css"));

        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/bootstrap").Include(
            "~/Content/bootstrap.min.css"));
        bundles.Add(new StyleBundle("~/fonts").Include(
            "~/fonts/Code39r.ttf"));

    }

如何包含code39字体ttf文件并使用它

您必须不是通过StyleBundle包含字体引用(它不是纯文本文件text/css,而是二进制文件,绑定了一个minimier,它不会被理解,并且会被错误地包含在带有<style src="...">标记的CSS中(,而是通过@font-face包含在CSS中。例如,要将其嵌入HTML代码中:

<style>
    @@font-face {
        font-family: Code39r; src: url('@Url.Content("~/fonts/Code39r.ttf")'); 
    } 
</style>

注意,@font-face必须转义为@@font-face(因为Razor将识别@(。如果你在CSS中使用它,你不能(除非你会做一些欺骗(使用Url.Content()解析路径,那么你指定的路径必须相对于你的CSS文件,例如:

@font-face { font-family: Code39r; src: url('../fonts/Code39r.ttf'); }