How to set parameters back to the default value?

How do I set parameters back to a default non-bolded font?

31st May 2016
admin-marketing

I am using absolute positioning when writing text in a PDF document using iTextSharp. It only have a single BaseFont instance for a regular font and there is no Bold version of that font. Therefore, it is not possible to set a Bold font with the setFontAndSize() method.

I read in a post that this was an alternative 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 works, but creates another problem. I don't know how to set these parameters back to my old default (non-bolded font).

 

Posted on StackOverflow on Apr 15, 2014 by sdalby

The answer is very simple: you need to save the state before you change the rendering mode, and restore the state after you've added the text. In iText 7 your code will look like this (C# and Java terminology is identical):

canvas.saveState();
canvas.setCharacterSpacing(1);
canvas.setFillColorRgb(66, 00, 00);
canvas.setLineWidth((float)0.5);
canvas.setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.FILL_STROKE);
// add the text using the changed state
canvas.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.

Click this link if you want to see how to answer this question in iText 5.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now