粗体后将BaseFont参数设置回默认值

时间:2014-04-15 14:18:38

标签: itextsharp itext

使用iTextSharp在PDF文档中编写文本时,我使用绝对定位。它只能处理BaseFont,并且无法在基本字体上设置粗体装饰。

我在帖子中读到这是将字体设置为粗体的方法:

 pdfContentByte.SetCharacterSpacing(1);
 pdfContentByte.SetRGBColorFill(66, 00, 00);  
 pdfContentByte.SetLineWidth((float)0.5);                   
 pdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);

这有效但却造成了另一个问题。我不知道如何将这些参数设置回旧的默认值(无粗体字体)。

你知道吗?

TIA SørenD。

1 个答案:

答案 0 :(得分:0)

答案非常简单:在更改之前需要保存状态,并在添加文本后恢复状态:

pdfContentByte.SaveState();
pdfContentByte.SetCharacterSpacing(1);
pdfContentByte.SetRGBColorFill(66, 00, 00);  
pdfContentByte.SetLineWidth((float)0.5);                   
pdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
// add the text using the changed state
pdfContentByte.RestoreState();

您对字符间距,颜色,线宽和渲染模式所做的更改仅在SaveState()RestoreState()序列之间有效。