How to make PdfContentByte as a piece in iTextSharp

In the case of Chunk, we can specify the text and font to be used. For example, we can set the bold font and font size in the case of Chunk. Although in the case of PdfContentByte I try to set the text in bold to

PdfContentByte cb = writer.DirectContent; cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,"INVOICE",386,596, 0); 

using

 cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); 

But the text that is displayed is too Black, is there any way to render the text as Chunk text to use a font like

  Font contentBoldHead = FontFactory.GetFont("Arial-BoldMT", 14, Font.BOLD); 

in PdfContentByte.

Any suggestions really help.

+4
source share
3 answers

To add to what @calum said, the solution is to just use bold. SetTextRenderingMode creates artificial bold, so it looks bad for you.

 cb.SetFontAndSize(FontFactory.GetFont(FontFactory.HELVETICA_BOLD).BaseFont, 20); cb.BeginText(); cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "INVOICE", 386, 596, 0); cb.EndText(); 
+10
source

You can use PdfContentByte.SetFontAndSize (font, size) to set the font to be used in ShowText () and ShowTextAligned ()

0
source
  • If possible (bold font matches the one you are using), @Chris's solution would be preferable.

  • If the bold font does not match the one you are using and you must use the artificial offset function that iText provides, you can use the ColumnText.ShowTextAligned method instead of the PdfContentByte.ShowTextAligned method.

See an example below:

 Phrase phrase = new Phrase(); Font font = new Font(basefont, 14); Chunk chunk = new Chunk("INVOICE", font); //you may change the 2nd parameter to adjust the weight of boldness chunk.SetTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.35f, null); phrase.Add(chunk); ColumnText.ShowTextAligned(cb, PdfContentByte.ALIGN_CENTER, phrase, 386, 596, 0); 

Also check the link:

http://what-when-how.com/itext-5/adding-text-at-absolute-positions-itext-5/

0
source

Source: https://habr.com/ru/post/1381326/


All Articles