문제

I'm using a HTML Rich-Text Editor to help me create a template for dynamic PDF reports, and it's working fine except that it doesn't change the font-face.

This editor is using the font tag instead of CSS styles, and I would welcome any way to programmatically change the font tags to equivalent tags using styles instead.

HTML (yes its messy, its from a WYSIWYG editor):

<div>&nbsp;
<br>
  <div align="center">
    <font size="5">
      <b>
        <br>
          <div align="center">
            <font font-face="Times New Roman" size="5">
              <b>Example 
              <font size="6">Chamber 
              <font size="5">
                <font size="4">Website</font>
              </font></font>Quotes</b>
            </font>
            <font face="Times New Roman">
              <br>
                <font face="Times New Roman">
                  <br>~
                  <font face="Times New Roman" color="#0000FF">
                    <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <u>&nbsp;[!PlanName]&nbsp;</u></b>
                  </font>
                  <font face="Times New Roman">
                    <br>~
                    <font face="Times New Roman" color="#0000FF">
                      <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>
                    </font>
                    <font face="Times New Roman" color="#0000FF">
                      <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                      <font color="#000000">Deductible&nbsp;
                      $[!PlanDeductible]:&nbsp;&nbsp;</font></b>
                    </font>
                    <font face="Times New Roman" color="#B0B0FF">
                    [!PlanRate]
                    <br>
                      <font face="Times New Roman">/~/~</font>
                      <br>
                        <br>
                          <br>
                            <font face="Courier New" size="1">
                            Copyright Example.com</font>
                            <br>
                              <br>
                                <font face="Arial">test</font>
                                <br></br>
                              </br>
                            </br>
                          </br>
                        </br>
                      </br>
                    </br></font></br>
                  </font></br>
                </font>
              </br>
            </font>
          </div>
        </br>
      </b>
    </font>
  </div>
</br></div>

C#:

public static byte[] ConvertHtmlToPdf(string html)
{
    html = HtmlPostProcessor.Process(html);
    byte[] fileData = null;
    string tempPath = ConfigurationManager.AppSettings["TempDirectory"];
    string tempPDFFile = Path.Combine(tempPath, Guid.NewGuid() + ".pdf");
    Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);

    using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))
    {
        PdfWriter.GetInstance(document, fs);
        using (StringReader stringReader = new StringReader(html))
        {
            List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null );
            document.Open();
            foreach (IElement item in parsedList)
            {
                document.Add(item);
            }
            document.Close();
        }
    }

    FileStream generatedPDF = File.Open(tempPDFFile, FileMode.Open);
    fileData = new byte[(int)generatedPDF.Length]; 
    int result = generatedPDF.Read(fileData, 0, (int)generatedPDF.Length);

    generatedPDF.Close();

    File.Delete(tempPDFFile);

    return fileData;
}

EDIT

I've been using iTextSharp version 5.1.1.0.

도움이 되었습니까?

해결책

The simplest, brute-force way is to call FontFactory.RegisterDirectories() before calling HTMLWorker.ParseToList(). Be warned, however - the method attempts to register/map all fonts on the running system.

So if you're running this in ASP.NET, for example, you probably want to put the call in global.asax.

EDIT: Working example using FontFactory.RegisterDirectories() and the HTML you provided above:

FontFactory.RegisterDirectories();
using (Document document = new Document()) {
  PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  List<IElement> objects = HTMLWorker.ParseToList(
    new StringReader(html), null);
  foreach (IElement element in objects) {
    document.Add(element);
  }
}

Just substitute Response.OutputStream with the Stream of your choice. Result PDF file from above.

다른 팁

HTMLWorker is obsolete. You need to switch to the new XMLWorker class. But if you want to use the HTMLWorker, you can define a global style for it as well:

StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma");
//...
//and then
var parsedHtmlElements = HTMLWorker.ParseToList(data, styles);

http://hivelocity.dl.sourceforge.net/project/itextsharp/itextsharp/iTextSharp-5.4.1/itextsharp-all-5.4.1.zip

there you can get the last version of iTextSharp(5.4.1) Library

http://hivelocity.dl.sourceforge.net/project/itextsharp/xmlworker/xmlworker-5.4.1/itextsharp.xmlworker-all-5.4.1.zip

and here is the XML worker... it only works with the same version of iTextSharp (5.4.1)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top