在pdfsharp中使用字体Times New Roman粗体时出错

时间:2018-06-28 07:16:40

标签: c# pdfsharp

我已经使用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);
}

然后我的结果是这样的:

PDF file image

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

1 个答案:

答案 0 :(得分:0)

您可以单独使用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);
                }