简体   繁体   中英

How to display "Times New Roman font" in PDF?

I want to change the font of the content to 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)));
    }
}

You could set at basefont something like this

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM