簡體   English   中英

使用ItextSharp將HTML轉換為PDF時,不保留字體樣式

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

我正在嘗試將HTML轉換為PDF,但是字體樣式未正確應用於PDF。 下面是我的代碼(使用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);

您可以使用pdfStamper編輯字體。 這是個小主意。 您可以根據需要進行管理。

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

更新:

僅更改標簽設置。 關閉前使用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM