iText + HTMLWorker - 如何更改默认字体?

时间:2011-02-08 16:01:13

标签: java html character-encoding itext

我必须从HTML源创建PDF文件。目前,我正在处理有关输出文件中特殊(抛光)字符的问题,正是因为它们缺乏。

HTML来源:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table width="100%" border="0.5" align="center" cellpadding="0" style="border-collapse:collapse; border:1px solid black; font-family:Arial, Helvetica, sans-serif; font-size:16px">
  <tr>
    <td align="center" ><b>Test: ąęłóćńśŁÓŃĆŻŹąśżźłęó</b></td>
  </tr>
</table>

Java源代码:

Document document = new Document(PageSize.A4, 38, 38, 50, 38);  
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("iTextExample.pdf"));  
document.open();  
HTMLWorker htmlWorker = new HTMLWorker(document);  
htmlWorker.parse(new StringReader(readFileAsString("index.html")));  
document.close();


public static String readFileAsString(String filePath) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
    try {
        long len = new File(filePath).length();
        if (len > Integer.MAX_VALUE) {
            throw new IOException("File " + filePath + " too large, was " + len + " bytes.");
        }
        byte[] bytes = new byte[(int) len];
        dis.readFully(bytes);
        return new String(bytes, "UTF-8");
    } finally {
        dis.close();
    }
}

我的问题是:如何将默认字体(Helvetica)更改为例如。 Arial Bold整个PDF文档?

我测试了许多与StyleSheet相关的示例,但没有一个可行。我必须更改默认字体,因为没有抛光字符 - 这是我希望能够解决的解决方案。

感谢您的帮助!

修改:

My FontProvider:  
    class defaultFontProvider extends FontFactoryImp {

    private String _default;

    public defaultFontProvider(String def) {
        _default = def;
    }

    public Font getFont(String fontName, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached) {
        if (fontName == null || size == 0) {
            fontName = _default;
        }

        return super.getFont(fontName, encoding, embedded, size, style, color, cached);
    }
    }

上面的代码嵌入了arial.ttf,这是好的,但我如何将它作为整个文档的默认字体(而不是Helvetica)。

然后..

        Map<String,Object> providers = new HashMap<String, Object>();

        defaultFontProvider dfp = new defaultFontProvider("arial.ttf");


        providers.put(HTMLWorker.FONT_PROVIDER, dfp);

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.setProviders(providers);

6 个答案:

答案 0 :(得分:2)

想法#1

立刻想到一个答案:改变iText。具体而言,Font.getCalculatedBaseFont,第644行。

String fontName = BaseFont.HELVETICA;

实际上,除非你也改变字体的创建方式,否则我认为这不会起作用......第712行

cfont = BaseFont.createFont(fontName, encoding, false);

除非字体是“Base 14”之一,否则您必须提供字体文件的路径,而不是简单的字体名称。

另一种选择:XSLT

转换输入,以便将字体定义添加到包含文本的任何节点的样式。

最后:注册fontProvider

您可以坐在FontFactoryImp的顶部,只需将空白字符串映射到您选择的字体。

class DefaultFontProvider extends FontFactoryImp {
  private String default;
  public DefaultFontProvider(String def) {
    default = def;
  }

  // I believe this is the correct override, but there are quite a few others.
  public Font getFont(String fontname, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached) {
    if (fontName == null || fontName.size() == 0) {
      fontName = default;
    }
    return super.getFont(fontName, encoding, embedded, size, style, color, cached);
  }
}


Map<String,Object> providers = new HashMap<String, Object)();
providers.put(HTMLWorker.FONT_PROVIDER, new DefaultFontProvider("Arial Bold"));

myHTMLWorker.setProviders(providers);

这让我觉得这是最具技术理念的想法。它是为刚刚发布的5.0.6版iText编写的。以前的版本通过setInterfaceProps()设置字体提供程序。在这一点上,“提供者”更像是一个名称变化。我怀疑5.1中将不再是这种情况。

PS:FontFactoryImp还有两位您可能感兴趣的公开成员:defaultEncodingdefaultEmbedding。您应该能够将defaultEncoding调整为更适合波兰语的内容。我推荐“Identity-H”(又名BaseFont.IDENTITY_H),但这会强制所有字体都是嵌入式子集,因此忽略defaultEmbedding,并使文件比字体不大完全嵌入。


两个可能的问题:

  1. 明确请求“Helvetica”。

    可以肯定的是,我建议将System.out.println(("Requested font: " + fontName);填充到getFont函数的开头。这将让您查看所有字体调用,并确保正确替换所有字体。如果是这种情况,您可以测试它并将其替换为_default

  2. 您的fontFactory可能找不到“Arial Bold”的任何内容,因此又回到了默认值(Helvetica)。

    我认为您需要致电dfp.registerDirectories()。这样可以找出几个不同操作系统上的所有字体,并让你通过字体名称而不是路径引用它们(这是FontFactoryImp应该首先做的事情)。

答案 1 :(得分:1)

在你的样式标签中,也许你可以使用CSS3来改变字体:

<style>
@font-face {
font-family: myFont;
src: url(Filename);
}
</style>

不确定这是不是你要问的。

答案 2 :(得分:1)

它不会使用默认字体,而只会使用您要应用的标记。

E.g. <Body>

在html标签中添加样式属性

<tag style="font-family: Arial Unicode MS, FreeSans; font-size:16px; font-weight: normal; >Здраво दी फोंट डाउनलोड Ravi Parekh! </tag>

注意:系统应在XMLWorkerFontProvider

中找到ARIALUNI
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes(Charset.forName("UTF-8"))), null,Charset.forName("UTF-8"), new XMLWorkerFontProvider("/fonts/"));

样品:SamplePDF

答案 3 :(得分:0)

如果要在pdf中使用不同的字体,请包含font packge 我有一个问题,一些土耳其字符不打印pdf所以我包括pdf字体包,我的问题解决了,

您可以在此处查看示例代码: - here

希望帮助你

答案 4 :(得分:0)

您可以将样式表添加到HTML解析器。那应该可以解决字体和变音符号的问题,但是在选择字体时必须做出很好的选择。

           HttpContext.Current.Response.ContentType = "application / pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(name));
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Page.RenderControl(hw);
           
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(header.Length > 7 ? header.Length > 14 ? header.Length > 21 ? PageSize.A3.Rotate() : PageSize.A3 : PageSize.A4.Rotate() : PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            string sylfaenpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\sylfaen.ttf";
            FontFactory.Register(sylfaenpath, "sylfaen");

            htmlparser.SetStyleSheet(GenerateStyleSheet());

            PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            HttpContext.Current.Response.Write(pdfDoc);
            HttpContext.Current.ApplicationInstance.CompleteRequest();


    private static StyleSheet GenerateStyleSheet()
    {
        StyleSheet css = new StyleSheet();

        css.LoadTagStyle("body", "face", "sylfaen");
        css.LoadTagStyle("body", "encoding", "Identity-H");
        css.LoadTagStyle("body", "size", "13pt");
        
        css.LoadTagStyle("h1", "size", "30pt");
        css.LoadTagStyle("h1", "style", "line-height:30pt;font-weight:bold;");
        css.LoadTagStyle("h2", "size", "22pt");
        css.LoadTagStyle("h2", "style", "line-height:30pt;font-weight:bold;margin-top:5pt;margin-bottom:12pt;");
        css.LoadTagStyle("h3", "size", "15pt");
        css.LoadTagStyle("h3", "style", "line-height:25pt;font-weight:bold;margin-top:1pt;margin-bottom:15pt;");
        css.LoadTagStyle("h4", "size", "13pt");
        css.LoadTagStyle("h4", "style", "line-height:23pt;margin-top:1pt;margin-bottom:15pt;");
        css.LoadTagStyle("hr", "width", "100%");
        css.LoadTagStyle("a", "style", "text-decoration:underline;");
        return css;
    }

答案 5 :(得分:-1)

protected void pdfButton_Click(object sender,EventArgs e)         {             列表showCourses = new List();             CourseManager aCourseManager = new CourseManager();             int departmentId = Convert.ToInt16(departmentDropDownList.Text);             int semesterId = Convert.ToInt16(semesterDropDownList.Text);             showCourses = aCourseManager.GetScheduleCoursesByDepartmentIdAndSemester(departmentId,semesterId);

        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        string pdfFilePath = Server.MapPath("CoursePdf.pdf");
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
        doc.Open(); //Open Document to write
        iTextSharp.text.Font font8 = FontFactory.GetFont("ARIAL", 7);
        string heading = " \t\t                                          Course Schedule Details for Department: " +
                                departmentDropDownList.SelectedItem;
        Paragraph reportHeading = new Paragraph(heading);
        if (showCourses != null)
        {
            PdfPTable PdfTable = new PdfPTable(6);
            PdfPCell PdfPCell = null;
            PdfPCell = new PdfPCell(new Phrase(new Chunk("Course Code", font8)));
            PdfTable.AddCell(PdfPCell);
            PdfPCell = new PdfPCell(new Phrase(new Chunk("Course Name", font8)));
            PdfTable.AddCell(PdfPCell);
            PdfPCell = new PdfPCell(new Phrase(new Chunk("Semester Name", font8)));
            PdfTable.AddCell(PdfPCell);
            PdfPCell = new PdfPCell(new Phrase(new Chunk("Course Credit", font8)));
            PdfTable.AddCell(PdfPCell);
            PdfPCell = new PdfPCell(new Phrase(new Chunk("Assign To", font8)));
            PdfTable.AddCell(PdfPCell);
            PdfPCell = new PdfPCell(new Phrase(new Chunk("Schedule", font8)));
            PdfTable.AddCell(PdfPCell);

            foreach (ShowCourse aCourse in showCourses)
            {
                PdfPCell = new PdfPCell(new Phrase(new Chunk(aCourse.courseCode, font8)));
                PdfTable.AddCell(PdfPCell);
                PdfPCell = new PdfPCell(new Phrase(new Chunk(aCourse.courseName, font8)));
                PdfTable.AddCell(PdfPCell);
                PdfPCell = new PdfPCell(new Phrase(new Chunk(aCourse.semesterName, font8)));
                PdfTable.AddCell(PdfPCell);
                PdfPCell = new PdfPCell(new Phrase(new Chunk((aCourse.credit).ToString(), font8)));
                PdfTable.AddCell(PdfPCell);
                PdfPCell = new PdfPCell(new Phrase(new Chunk(aCourse.teacherName , font8)));
                PdfTable.AddCell(PdfPCell);
                PdfPCell = new PdfPCell(new Phrase(new Chunk(aCourse.schedule, font8)));
                PdfTable.AddCell(PdfPCell);
            }
            PdfTable.SpacingBefore = 15f; // Give some space after the text or it m
            doc.Add(reportHeading); // add paragraph to the document
            doc.Add(PdfTable); // add pdf table to the document
            doc.Close();
            // string pdfPath = Server.MapPath("~/SomePDFFile.pdf");
            WebClient client = new WebClient();
            Byte[] buffer = client.DownloadData(pdfFilePath);
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.BinaryWrite(buffer);
        }

    }