如何在ITEXT PDF中使用虚线标注文本 [英] How do you underline text with dashedline in ITEXT PDF

查看:224
本文介绍了如何在ITEXT PDF中使用虚线标注文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过

var  par = new Paragraph();
par.Add(new Chunk("Some text", CreateFont(12, Font.UNDERLINE))); 
document.Add(par);

有可能只用虚线(不是段落)强调一些文字吗?

It is possible underline just "Some text" with dashed line (not the paragraph)?

谢谢

推荐答案

这个答案告诉你如何做,但遗憾的是没有提供任何代码,所以我在下面提供了。

This answer tells you how to do it but unfortunately doesn't provide any code so I've provided it below.

据我所知,只需设置一个简单的属性,就没有直接的方法可以在iTextSharp中实现这一点。相反,你需要使用 PageEvent 并自己手动绘制线。

To the best on my knowledge there isn't a direct way to achieve this in iTextSharp by just setting a simple property. Instead, you need to use a PageEvent and manually draw the line yourself.

首先你需要继承 PdfPageEventHelper

private class UnderlineMaker : PdfPageEventHelper {
}

然后你要覆盖 OnGenericTag()方法:

public override void OnGenericTag(PdfWriter writer, Document document, iTextSharp.text.Rectangle rect, string text) {
}

text 变量将保留一个您稍后设置的任意值。这是一个包含注释的完整工作示例:

The text variable will hold an arbitrary value that you set later on. Here's a full working example with comments:

private class UnderlineMaker : PdfPageEventHelper {
    public override void OnGenericTag(PdfWriter writer, Document document, iTextSharp.text.Rectangle rect, string text) {
        switch (text) {
            case "dashed":
                //Grab the canvas underneath the content
                var cb = writer.DirectContentUnder;

                //Save the current state so that we don't affect future canvas drawings
                cb.SaveState();

                //Set a line color
                cb.SetColorStroke(BaseColor.BLUE);

                //Set a dashes
                //See this for more details: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfContentByte.html#setLineDash(float)
                cb.SetLineDash(3, 2);

                //Move the cursor to the left edge with the "pen up"
                cb.MoveTo(rect.Left, rect.Bottom);

                //Move to cursor to the right edge with the "pen down"
                cb.LineTo(rect.Right, rect.Bottom);

                //Actually draw the lines (the "pen downs")
                cb.Stroke();

                //Reset the drawing states to what it was before we started messing with it
                cb.RestoreState();
                break;
        }

        //There isn't actually anything here but I always just make it a habit to call my base
        base.OnGenericTag(writer, document, rect, text);
    }
}

以下是它的实现:

//Create a test file on the desktop
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

//Normal iTextSharp boilerplate, nothing special here
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.Open();

            //Bind our helper class
            writer.PageEvent = new UnderlineMaker();

            //Create a new paragraph
            var p = new Paragraph();

            //Add a normal chunk
            p.Add(new Chunk("This is a "));

            //Create another chunk with an arbitrary tag associated with it and add to the paragraph
            var c = new Chunk("dashed underline test");
            c.SetGenericTag("dashed");
            p.Add(c);

            //Add the paragraph to the document
            doc.Add(p);

            doc.Close();
        }
    }
}

如果你想得到花哨的话你可以将一个分隔的字符串传递给 SetGenericTag(),如 dashed-black-2x4 或其他东西,然后解析出来 OnGenericTag event。

If you wanted to get fancy you could pass a delimited string to SetGenericTag() like dashed-black-2x4 or something and parse that out in the OnGenericTag event.

这篇关于如何在ITEXT PDF中使用虚线标注文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆