Setting BaseFont parameters back to default after bold

时间秒杀一切 提交于 2019-12-24 14:38:38

问题


I am using absolute positioning when writing text in a PDF document using iTextSharp. It can only deal with BaseFont and it is not possible to set a Bold decoration on a base font.

I read in a post that this was the way to set the font to bold:

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

That worked but created a another problem. I don't know how to set these parameters back to my old default (none-bolded font).

Do you know how?

TIA Søren D.


回答1:


the answer is very simple: you need to save the state before you change it, and restore the state after you've added the text:

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

The changes you make to the character spacing, color, line width and rendering mode will only be valid between the SaveState() and RestoreState() sequence.



来源:https://stackoverflow.com/questions/23086454/setting-basefont-parameters-back-to-default-after-bold

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!