【问题标题】:How to display "Times New Roman font" in PDF?如何在 PDF 中显示“Times New Roman 字体”?
【发布时间】:2016-05-03 09:37:08
【问题描述】:

我想把内容的字体改成TIMES NEW roman

iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(textSharpDocument);
#pragma warning restore 612, 618
String[] content = letterContent.Split(new string[] { AppUtil.GetAppSettings(AppConfigKey.ReceiptLetterPDFSeparator) }, StringSplitOptions.None);

//Add Content to File
if (content.Length > AppConstants.DEFAULT_PARAMETER_ZERO)
{
    string imageFolderPath = AppUtil.GetAppSettings(AppConfigKey.UploadedImageFolderPath);
    for (int counter = 0; counter < content.Length - 1; counter++)
    {
        textSharpDocument.Add(new Paragraph());
        if (!String.IsNullOrEmpty(imageUrlList[counter]))
        {
            string imageURL = imageFolderPath + imageUrlList[counter];
            textSharpDocument.Add(new Paragraph());
            string imagePath = AppUtil.GetAppSettings(AppConfigKey.ParticipantCommunicationFilePhysicalPath) + imageUrlList[counter];
            imagePath = imagePath.Replace(@"\", "/");
            if (File.Exists(imagePath))
            {
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(imageURL);
                imageLocationIDs[counter] = imageLocationIDs[counter] != null ? AppUtil.ConvertToInt(imageLocationIDs[counter], AppConstants.DEFAULT_PARAMETER_ONE) : AppUtil.ConvertEnumToInt(AppImageLocation.Left);

                if (imageLocationIDs[counter] == AppUtil.ConvertEnumToInt(AppImageLocation.Left))
                    png.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
                if (imageLocationIDs[counter] == AppUtil.ConvertEnumToInt(AppImageLocation.Right))
                    png.Alignment = iTextSharp.text.Image.ALIGN_RIGHT;
                if (imageLocationIDs[counter] == AppUtil.ConvertEnumToInt(AppImageLocation.Center))
                    png.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
                textSharpDocument.Add(png);
            }
        }
        hw.Parse(new StringReader(content[counter]));
        hw.EndElement("</html>");
        hw.Parse(new StringReader(AppUtil.GetAppSettings(AppConfigKey.ReceiptLetterPDFSeparator)));
    }
}

【问题讨论】:

    标签: c# itextsharp


    【解决方案1】:

    你可以在 basefont 上设置类似这样的东西

    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    Font times = new Font(bfTimes, 12, Font.ITALIC, Color.BLACK);
    
    Document doc = new Document();
    PdfWriter.GetInstance(doc, new FileStream("c:\\Font.pdf", FileMode.Create));
    doc.Open();
    doc.Add(new Paragraph("This is a test using Times Roman", times));
    doc.Close();
    

    【讨论】:

    • online doc.Add(new Paragraph("This is a test using Times Roman", times));而不是“这是一个使用 Times Roman 的测试”,我有一个名为:字符串类型的字母内容的 html 内容,它会转换文本吗?我正在使用 htmlworker 类来解析内容。请看一下{ iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(textSharpDocument); . . . . hw.Parse(new StringReader(content[counter])); } 请查看并相应回复
    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    相关资源
    最近更新 更多