【问题标题】:Why is the customization of my PDF fonts using iTextSharp failing?为什么使用 iTextSharp 自定义我的 PDF 字体失败?
【发布时间】:2015-04-08 19:06:28
【问题描述】:

我想通过加粗一些文本、使用不同的字体和颜色来装饰 PDF 文件中的各个段落。我以为我找到了如何做到这一点的代码(如下),但它不起作用 - 所有文本都是相同的字体、颜色(黑色)和大小。为什么到目前为止,我为美化 PDF 文件而付出的虚拟努力却徒劳无功?这是我的代码:

using (var ms = new MemoryStream())
{

    using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))
    {
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {
            doc.Open();

            // Mimic the appearance of Duckbill_Platypus.pdf
            var docTitle = new Paragraph("Duckbilled Platypi - they're not what's for dinner");
            var titleFont = FontFactory.GetFont("Courier", 18, BaseColor.BLACK);
            docTitle.Font = titleFont;
            doc.Add(docTitle);

            var subTitle = new Paragraph("Baby, infant, toddler, and perhaps 'Terrible 2s' Platypi are called Platypups");
            var subtitleFont = FontFactory.GetFont("Times Roman", 13, BaseColor.BLACK);
            subTitle.Font = subtitleFont;
            doc.Add(subTitle);

            var importantNotice = new Paragraph("Teenage platypi are sometimes called Platydude[tte]s");
            var importantNoticeFont = FontFactory.GetFont("Courier", 13, BaseColor.RED);
            importantNotice.Font = importantNoticeFont;
            doc.Add(importantNotice);

            ListColumns lc;
            for (int i = 0; i < listOfListItems.Count; i++)
            {
                lc = listOfListItems[i];
                sb.AppendLine(String.Format(@"<p>Request date is {0}; Payee Name is {1}; Remit Address or Mail Stop is {2}; Last 4 of SSN or ITIN is {3}; 204 Submitted or on file is {4}; Requester Name is {5}; Dept or Div Name is {6}; Phone is {7}; Email is {8}</p>",
                    lc.li_requestDate, lc.li_payeeName, lc.li_remitAddressOrMailStop, lc.li_last4SSNDigitsOrITIN, lc.li_204SubmittedOrOnFile, lc.li_requesterName, lc.li_deptDivName, lc.li_phone, lc.li_email));
            }

            String htmlToRenderAsPDF = sb.ToString();

            //XMLWorker also reads from a TextReader and not directly from a string
            using (var srHtml = new StringReader(htmlToRenderAsPDF))
            {
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
            }

            doc.Close();
        }
    }
    try
    {
        var bytes = ms.ToArray();
        var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "iTextSharpTest.pdf");
        File.WriteAllBytes(testFile, bytes);
    }
    catch (Exception ex)
    {
        String exMsg = ex.Message;
        ; // what is "ex" here?
    }

我正在设置两种大小的字体(18 和 13)、两种颜色(黑色和红色)、两种字体(“Courier”和“Times Roman”),但我在这里为 PDF 狂热所花费的所有时间似乎还没有结束徒劳(向 Vachel Lindsay 和 Abraham Lincoln 道歉)。

【问题讨论】:

    标签: c# sharepoint-2010 pdf-generation itextsharp


    【解决方案1】:

    创建Paragraph时应使用该字体:

    Font f = new Font(FontFamily.COURIER);
    Paragraph p = new Paragraph("text", f);
    document.add(p);
    

    或者您应该等到设置好字体后再添加内容:

    Paragraph p = new Paragraph();
    Font f = new Font(FontFamily.COURIER);
    p.setFont(f);
    p.addText("text");
    document.add(p);
    

    在你的代码中,你有这样的东西:

    Paragraph p = new Paragraph("text");
    Font f = new Font(FontFamily.COURIER);
    p.setFont(f);
    document.add(p);
    

    使用setFont() 设置字体时,您为将添加到段落中的文本设置字体,而不是为已存储在段落中的文本设置字体。

    例如:

    Paragraph p = new Paragraph("font 1 ");
    p.setFont(new Font(FontFamily.COURIER);
    p.add("font 2");
    document.add(p);
    

    这将以默认字体添加文本font 1,并在 Courier 中添加 font 2。

    【讨论】:

    • 我没有看到你的第一点;在每种情况下,我先创建一个段落,然后创建一个字体,然后将字体分配给段落,然后再将段落添加到文档中。如果我在声明而不使用某些东西,我会收到警告,我很确定。
    • 如果你设置好字体后没有给Paragraph添加内容,你并没有真正使用字体...
    • 我现在发现我忽略了您的问题中的某些内容。我会相应地更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2019-05-31
    相关资源
    最近更新 更多