2

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.

EDIT

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 :)

1
  • not sure why the downvote. An explanation might help. Nov 18, 2012 at 12:49

2 Answers 2

2

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.

1
  • 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. Oct 2, 2012 at 16:39
0

You can use SetColorFill method. Like this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.