Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am creating a PDF using iTextSharp.

Part of the process adds straightforward text to the PDF and another part creates an outline of the same text.

For the straighforward text aspect I am using the ShowTextAligned method in iTextSharp; which requires a Basefont and has no way of setting the style.

For the outline I am creating a GDI GraphicsPath to get points, which are then translated into PDF curves/lines etc. The AddString method requires a Drawing.FontStyle

Now my issue is that I need to set the style of AddString to the same one being used by the BaseFont. I.E if the Basefont is rendering Regular, i need to set the AddString fontstyle to regular

How do I determine what style is being used by the BaseFont in ShowTextAligned?

Further info:

I have also tried using ColumnText; which allows you to set an iTextSharp Font, along with it's style. Though using this method results in a font that is rendered with the approximate style; I.E a font usually in regular and set to bold gives a slightly malformed bold font.

When I say i need to pass in the BaseFont to ShowTextAligned, What I mean to say is that in order to use this method you have to set the font and size first using SetFontAndSize of the PdfContentByte - it is this method that requires the BaseFont.

I know I must be missing something obvious.. I just can't see the wood for the trees :)

You can try defining your base font beforehand and use it throughout your PDF doc.

See iTextSharp - Working with Fonts

Sample code:

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 12, Font.ITALIC, Color.RED);

The above lines create a BaseFont object and uses the built-in constant values to set the font family and encoding. It also specifies false for embedding the font within the PDF document. A new Font object is created using the BaseFont object, and further setting the font size in points, the style and the color - again, using iTextSharp's constants for these values.

For more info on BaseFont, see its class definition.

This doesn't resolve the issue I have though - that I have no idea what style is being used by the ShowTextAligned method; which takes in a BaseFont and not a Font. Or am I missing something? Ultimately I would like to know what style is being employed by the BaseFont. To give a further example. If I ShowTextAligned with a BaseFont using Comic Sans MS it results in Bold style being used. The Cooper Black uses Regular. This can be clarified by creating a PDF, bringing it into Illustrator and typing the same text; play with the style and see what matches. – Darren Wainwright Oct 2, 2012 at 16:39
content.BeginText();
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
content.SetFontAndSizebaseFont, 12);
BaseColor baseColor = new BaseColor(255, 0, 0);
content.SetColorFill(baseColor);
//or use predefined colors 
content.SetColorFill(BaseColor.RED);
// use ShowTextAligned method
content.EndText();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.