如何显示" Times New Roman字体"用PDF格式?

时间:2016-05-03 09:37:09

标签: c# itextsharp

我想将内容的字体更改为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)));
    }
}

1 个答案:

答案 0 :(得分: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();