【问题标题】:How do you underline text with dashedline in ITEXT PDF如何在 ITEXT PDF 中用虚线为文本添加下划线
【发布时间】:2015-05-29 10:08:51
【问题描述】:

我在“一些文字”下划线了

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

可以用虚线(不是段落)在“某些文本”下划线吗?

谢谢

【问题讨论】:

    标签: itextsharp


    【解决方案1】:

    This answer 告诉你怎么做,但不幸的是没有提供任何代码,所以我在下面提供了它。

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

    首先你需要继承PdfPageEventHelper:

    private class UnderlineMaker : PdfPageEventHelper {
    }
    

    然后您想要覆盖OnGenericTag() 方法:

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

    text 变量将保存您稍后设置的任意值。这是一个完整的 cmets 工作示例:

    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 事件中解析出来。

    【讨论】:

    • 我来不及回答 ;-) 无论如何我都会在沙箱中发布我的示例 ;-) 这是需要 Java 示例的人的 URL:itextpdf.com/sandbox/events/DashedUnderline 尽管 Chris ' 答案更加详尽,因此他值得称赞。请注意,您可以通过从rect.Bottom 中减去一些用户单位来移动行的位置。
    • 谢谢你们,这就是我要找的。​​span>
    猜你喜欢
    • 2013-02-21
    • 2014-04-18
    • 2012-09-07
    • 2011-05-20
    • 1970-01-01
    • 2012-04-18
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多