更改文本行间距 [英] Changing text line spacing

查看:150
本文介绍了更改文本行间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个仅包含文本的PDF文档,其中所有文本都是相同的磅值和字体系列,但每个字符可能是不同的颜色。使用下面的代码片段似乎一切正常,但线之间的默认空间略大于我认为的理想值。有没有办法控制这个? (仅供参考,在下面的代码中键入ColoredText只包含一个字符串及其颜色。另外,我分别处理换行符的原因是,由于某种原因,如果它在一个Chunk中,它不会导致换行。)

I'm creating a PDF document consisting of text only, where all the text is the same point size and font family but each character could potentially be a different color. Everything seems to work fine using the code snippet below, but the default space between the lines is slightly greater than I consider ideal. Is there a way to control this? (FYI, type "ColoredText" in the code below merely contains a string and its color. Also, the reason I am treating the newline character separately is that for some reason it doesn't cause a newline if it's in a Chunk.)

谢谢,
Ray

Thanks, Ray

List<byte[]> pdfFilesAsBytes = new List<byte[]>();
iTextSharp.text.Document document = new iTextSharp.text.Document();
MemoryStream memStream = new MemoryStream();
iTextSharp.text.pdf.PdfWriter.GetInstance(document, memStream);
document.SetPageSize(isLandscape ? iTextSharp.text.PageSize.LETTER.Rotate() : iTextSharp.text.PageSize.LETTER);
document.Open();
foreach (ColoredText coloredText in coloredTextList)
{
    Font font = new Font(Font.FontFamily.COURIER, pointSize, Font.NORMAL, coloredText.Color);
    if (coloredText.Text == "\n")
       document.Add(new Paragraph("", font));
    else
        document.Add(new Chunk(coloredText.Text, font));
}
document.Close();
pdfFilesAsBytes.Add(memStream.ToArray());


推荐答案

根据PDF规范,基线之间的距离两行称为前导。在iText中,默认前导是字体大小的1.5倍。例如:默认字体大小为12磅,因此默认前导值为18。

According to the PDF specification, the distance between the baseline of two lines is called the leading. In iText, the default leading is 1.5 times the size of the font. For instance: the default font size is 12 pt, hence the default leading is 18.

您可以更改段落的前导使用其他构造函数之一。例如,参见: public Paragraph(float leading,String string,Font font)

You can change the leading of a Paragraph by using one of the other constructors. See for instance: public Paragraph(float leading, String string, Font font)

您还可以使用其中一种方法更改前导设置领先:

You can also change the leading using one of the methods that sets the leading:

paragraph.SetLeading(fixed, multiplied);

第一个参数是固定的前导:如果你想要领先15,无论哪个字体是使用时,你可以选择fixed = 15和multiplied = 0.

The first parameter is the fixed leading: if you want a leading of 15 no matter which font size is used, you can choose fixed = 15 and multiplied = 0.

第二个参数是一个因素:例如,如果你想要前导是字体大小的两倍,你可以选择fixed = 0和multiplied = 2.在这种情况下,字体大小为12的段落的前导将为24,对于字体大小为10,它将为20,并且子将为。

The second parameter is a factor: for instance if you want the leading to be twice the font size, you can choose fixed = 0 and multiplied = 2. In this case, the leading for a paragraph with font size 12 will be 24, for a font size 10, it will be 20, and son on.

您还可以组合固定和成倍领先。

You can also combine fixed and multiplied leading.

这篇关于更改文本行间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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