简体   繁体   中英

Font style is not preserved when converting HTML to PDF using ItextSharp

I'm trying to convert HTML to PDF, but the font style isn't being properly applied to the PDF. Below is my code (using itextsharp.dll ):

Document document = new Document();
FileStream fs = new FileStream(fileName, FileMode.Create);
PdfWriter.GetInstance(document, fs);
document.Open();
HTMLWorker htmlWorker = new HTMLWorker(document);
string content = radEditorCollector.Content;
if (string.IsNullOrWhiteSpace(content))
{
    content = AppConstants.LetterNotConfigured;
}
htmlWorker.Parse(new StringReader(content));
document.Close();
fs.Close();
//Create document list of each debtor
PdfReader pdfReader = new PdfReader(fileName);
readerList.Add(pdfReader);

You can edit your font with pdfStamper . Here is little idea. You can manage with what you need.

PdfReader pdfReader = new PdfReader(fileName);
PdfStamper pdfStamper = new PdfStamper(pdfReader, fs);
PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
pdfContentByte.SetColorFill(BaseColor.BLUE);
pdfContentByte.SetFontAndSize(baseFont, 8);
pdfContentByte.BeginText();
pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Here is your settings", 400, 600, 0);
pdfContentByte.EndText();
pdfStamper.Close();
readerList.Add(pdfReader);

Update:

Change only tag settings. Use before close.

pdfStamper.AcroFields.SetFieldProperty("YOUR_TAG", "textfont", 
baseFont, null);

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