【问题标题】:Error when using font Times New Roman bold in pdfsharp在pdfsharp中使用字体Times New Roman粗体时出错
【发布时间】:2018-06-28 07:16:40
【问题描述】:

我使用 pdfSharp 将文本插入 PDF 文件,但是当我使用字体 Times New Roman 粗体时,出现了一些问题。

这是我的代码:

using PdfSharp.Pdf;
using PdfSharp.Drawing;
PdfSharp.Pdf.PdfDocument pdf = new PdfSharp.Pdf.PdfDocument();
pdf.Info.Title = filePrint;
PdfSharp.Pdf.PdfPage pdfPage = pdf.AddPage();
pdfPage.Size = PdfSharp.PageSize.A4;
using (XGraphics graph = XGraphics.FromPdfPage(pdfPage))
{
     XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
     XFont fontBold = new XFont("Times New Roman", 10, XFontStyle.Bold, options);
     graph.DrawString("Đây là đoạn text bold", fontBold, XBrushes.Black, new XRect(75.6, 56, 30, 30), XStringFormats.TopLeft);
}

那么我的结果是这样的:

请帮助我找到解决此错误的方法。

【问题讨论】:

    标签: c# pdfsharp


    【解决方案1】:

    您可以另外使用 MigraDoc。

    关注this 文章: 你可以试试这个:

    using MigraDoc.DocumentObjectModel;
    using MigraDoc.Rendering;
    using PdfSharp.Drawing;
    using PdfSharp.Pdf;
                    using (var document = new PdfDocument())
                    {
                        PdfPage page = document.AddPage();
                        XGraphics graph = XGraphics.FromPdfPage(page);
    
                        graph.MUH = PdfFontEncoding.Unicode;
                        graph.MFEH = PdfFontEmbedding.Always;
    
                        // You always need a MigraDoc document for rendering.
                        Document doc = new Document();
                        Section section = doc.AddSection();
    
                        Font font = new Font("Times New Roman", 12);
    
                        foreach (var line in textFileLines)
                        {
                            Paragraph paragraph = section.AddParagraph();
                            paragraph.AddFormattedText(line, font);
                        }
    
                        //save pdf document
                        PdfDocumentRenderer renderer = new PdfDocumentRenderer();
                        renderer.Document = doc;
                        renderer.RenderDocument();
                        renderer.Save(output);
                    }
    

    【讨论】:

    • 除了 pdfSharp 之外,您还使用其他任何东西吗?因为当我将您的代码复制到我的程序时出现了一些错误:D
    • 感谢您的回答,问题是我的电脑字体错误。我尝试在另一台计算机上构建和运行这个应用程序,它运行良好
    • @Duonglv.谢谢。凸轮。我认为您的计算机中缺少 Times New Roman 字体?
    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多