0

I need to write a paragraph to a PDF file I'm generating. Said paragraph is comprised of a bolded first word in red, some "regular" red text after that, then a link (blue), some more regular red text, and finally another link (also blue). I think I can use Chunks to do that, something like this (pseudocode):

Chunk boldChunk = new Chunk("Important:"); // add font via GetFont?
boldChunk.SetBold();
boldChunk.SetColor(red);

Chunk beginChunk = new Chunk("Form must be filled out in ");
beginChunk.SetColor(red);

Chunk underlinedAdobeReader = new Chunk("Adobe Reader");
underlinedAdobeReader.SetUnderline(0.5f, -1.5f);
underlinedAdobeReader.SetColor(blue);
underlinedAdobeReader.SetHRef("http://www.adobe.com");

Chunk middleChunk = new Chunk("or Acrobat Professional 
8.1 or above. To save completed forms, Acrobat Professional is required. For 
technical and accessibility assistance, contact the ");
middleChunk.SetColor(red);

Chunk underlinedJefe = new Chunk("Chief Platypus");
underlinedJefe.SetUnderline(0.5f, -1.5f);
underlinedJefe.SetColor(blue);
underlinedJefe.SetHref("http://twaincentral.azurewebsites.net/");

doc.Add(boldChunk);
doc.Add(beginChunk);
doc.Add(underlinedJefe);
doc.Add(middleChunk);
doc.Add(underlinedCampusOffice);

....but that seems borderline outlandish; Is there a way I can "cut to the chase" and assign some HTML to the doc, as in:

String composite = "<strong>Important:</strong> Form must be filled out in <a href="http://www.adobe.com" target="_blank">Adobe Reader</a> 
or Acrobat Professional 
8.1 or above. To save completed forms, Acrobat Professional is required. For 
technical and accessibility assistance, contact the <a href="http://twaincentral.azurewebsites.net/" target="_blank">Chief Platypus</a>");
doc.Add(composite);

I also want to restrict this paragraph to West of the Mississippi (that is to say, the left half of the page), but that is a separate issue.

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    If your HTML is very simple you could [try something like this](http://stackoverflow.com/a/8327462/231316) – Chris Haas Apr 08 '15 at 22:32
  • I'll try that, but how complex is too complex? My HTML (shown at the end of my post above) contains a strong and two anchor tags. Come to think of it, I also need the text to be red (other than the links/hrefs). I guess I could use spans with inline color styles set... – B. Clay Shannon-B. Crow Raven Apr 08 '15 at 22:37
  • 1
    That example uses the `HTMLWorker` which is obsolete and doesn't support much (if any) CSS. Honestly, you'll just need to see if it works with your code. If it doesn't, then I'd try [switching to `XmlWorker`](http://stackoverflow.com/a/25164258/231316) which should get you want you want. – Chris Haas Apr 08 '15 at 22:41
  • I find it odd that XMLWorker (apparently) supports CSS more fully than HTMLWorker does. – B. Clay Shannon-B. Crow Raven Apr 08 '15 at 22:46
  • Why is that odd? `HTMLWorker` grew organically and is full of *spaghetti code* (it was impossible to further maintain it). XML Worker was built from scratch with the specific goal to be a generic XML tool, with XHTML as the format of choice to test the functionality. – Bruno Lowagie Apr 09 '15 at 07:32
  • @BrunoLowagie: It seems odd to me because when I think of CSS, I think of it being married to HTML, not XML. – B. Clay Shannon-B. Crow Raven Apr 09 '15 at 14:40

0 Answers0