c# - ITrextSharp using multiple font styles together. ie Bold, Underlined, italic... etc -
i attempting use itextsharp.dll
(not sure version) write dynamic pdf. going great until needed write phrase bold , underlined. appears itextsharp's font class not allow this. allows bold/italic, not bold/underline, italic/ underline, or three. cannot combine underlined other style. seems rather silly not allow font both underlined , else. have looked everywhere , see nothing mentions it. know way around or missing obvious here?
i build font so.
itextsharp.text.font myfont = new font(itextsharp.text.font.fontfamily.times_roman, 9, itextsharp.text.font.bolditalic, basecolor.black);
you can see 3rd parameter integer signifying fontstyle
font, there no enums available make underlined , bold ,underlined , italic, or three. there has way this. find hard believe itextsharp not account underlined , bold text. ideas?
if @ definition bolditalic
you'll see:
public const int bolditalic = bold | italic;
this shows how combine these styles using bitwise |
(or
) operator. of course free redefine these want you'll see them used this:
var myfont = new font(itextsharp.text.font.fontfamily.times_roman, 9, font.bold | font.underline, basecolor.black);
edit
looking @ source, bold
1
, underline
4
, when |
them 5
same value posted. can test every single combination of 5 styles using code below.
//create test file on our desktop var testfile = system.io.path.combine(environment.getfolderpath(environment.specialfolder.desktop), "test.pdf"); //possible styles var styles = new dictionary<string, int>() { { "normal" , itextsharp.text.font.normal }, { "bold" , itextsharp.text.font.bold }, { "italic" , itextsharp.text.font.italic }, { "underline" , itextsharp.text.font.underline }, { "strikethru", itextsharp.text.font.strikethru } }; //standard itext bootstrap using (var fs = new filestream(testfile, filemode.create, fileaccess.write, fileshare.none)) { using(var doc = new document()) { using (var writer = pdfwriter.getinstance(doc, fs)) { doc.open(); //we're going try every possible unique combination of constants, store //previously used ones in dictionary var used = new dictionary<int, string>(); //fixed-number combination hack, create 5 nested loops. foreach (var in styles) { foreach (var b in styles) { foreach (var c in styles) { foreach (var d in styles) { foreach (var g in styles) { //bitwise or values var k = a.value | b.value | c.value | d.value | g.value; //if didn't use or'd value if (!used.containskey(k)) { //get of unique names exclude duplicates var names = new string[] { a.key, b.key, c.key, d.key, g.key }.distinct().orderby(s => s).tolist(); //normal "default" , although normal | bold totally valid //messes brain when see it. remove normal description //when used else. part optional if (names.count() > 1 && names.contains("normal")) { names = names.where(n => n != "normal").tolist(); } //merge our names comma-separated string var v = string.join(", ", names); //store don't use again used.add(k, v); //create font using loop's value var myfont = new itextsharp.text.font(itextsharp.text.font.fontfamily.times_roman, 12, k, basecolor.black); //add our document doc.add(new paragraph(k.tostring() + "=" + v, myfont)); } } } } } } doc.close(); } } }
this code produces text:
Comments
Post a Comment