Skip to content

Bold / italic style lost when writing as both RTF and HTML #245

Open

Description

@manfromarce

When using the following code the bold and italic styles are not written to RTF:

using iTextSharp.text;

var htmlFilePath = "test.html";
var rtfFilePath = "test.rtf";
var pdfFilePath = "test.pdf";

var document = new Document(PageSize.A4);
var htmlFileStream = new FileStream(htmlFilePath, FileMode.Create);
var htmlWriter = html.HtmlWriter.GetInstance(document, htmlFileStream);
var rtfFileStream = new FileStream(rtfFilePath, FileMode.Create);
var rtfWriter = rtf.RtfWriter2.GetInstance(document, rtfFileStream);

document.AddAuthor(System.Environment.UserName);
document.Open();
var fontTitle = FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD);
var parTitle = new Paragraph("This is a new document", fontTitle);
var phrase = new Phrase();
var boldChunk = new Chunk("Bold, ", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD));
var italicChunk = new Chunk("italic, ", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.ITALIC));
var underlineChunk = new Chunk("underlined ", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));
var regularChunk = new Chunk("text", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL));
phrase.Add(boldChunk);
phrase.Add(italicChunk);
phrase.Add(underlineChunk);
phrase.Add(regularChunk);
var parBody = new Paragraph(phrase);
document.Add(parTitle);
document.Add(parBody);

document.Dispose();
htmlFileStream.Dispose();
rtfFileStream.Dispose();

It seems that the HTML writer removes the styles from the font, so the RTF writer finds the regular style only. If the RTF writer is initialized first, the opposite happens and bold/italic are written properly to RTF but not to HTML.
However, the styles are correct if for example the bold span is created like this:

var bold = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
var boldFont = new Font(bold.BaseFont, 12, Font.BOLD);
var boldChunk = new Chunk("Bold", boldFont);

I would expect it to work the same way without setting the bold style twice, but maybe there is something I am doing wrong.

There are no issues if the PDF writer is initialized first, or with other styles such as underline and strikethrough.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions