首页 > 解决方案 > 如何使用 itextsharp 减少或删除段落之间的行?

问题描述

我有以下代码,我在其中使用 itextsharp 创建了一个 pdf。我给你举个例子:

byte[] pdf = null;
using (MemoryStream stream = new System.IO.MemoryStream())
{
//Document pdfDoc = new Document(PageSize.A4, 25, 25, 65, 15);//izquierda,derecha,superior, inferior
float myWidth = 227f;
float myHeight = 842f;


var pgSize = new Rectangle(myWidth, myHeight);
using (var pdfDoc = new Document(pgSize, 2, 2, 1, 30))
{
    var pdfWriter = PdfWriter.GetInstance(pdfDoc, stream);
    pdfDoc.Open();
    PdfPTable table = new PdfPTable(3);
    table.WidthPercentage = 100;
    table.SpacingBefore = 10f;
    //0=Left, 1=Centre, 2=Right
    table.HorizontalAlignment = 0;
    widths = new float[] { 10, 80, 10 };
    table.SetWidths(widths);

    cell = new PdfPCell();
    Chunk chunk = new Chunk("", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK));
    cell = new PdfPCell();
    cell.Border = 0;
    cell.PaddingBottom = 0;
    cell.AddElement(chunk);
    table.AddCell(cell);


    Image image = Image.GetInstance(img_firma);
    image.ScaleAbsolute(185f, 105f);
    cell = new PdfPCell(image);
    cell.HorizontalAlignment = Element.ALIGN_LEFT;
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    cell.PaddingBottom = 0;
    //cell.FixedHeight = 100;
    cell.Border = 0;
    //cell.AddElement(image);
    table.AddCell(cell);

    cell = new PdfPCell();
    chunk = new Chunk("", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK));
    cell = new PdfPCell();
    cell.Border = 0;
    cell.PaddingBottom = 0;
    cell.AddElement(chunk);
    table.AddCell(cell);


    Paragraph parrafo1 = new Paragraph();//titulo
    parrafo1.Add(new Chunk("Hello", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
    parrafo1.Alignment = Element.ALIGN_CENTER;
    Paragraph parrafo2 = new Paragraph();
    parrafo2.Add(new Chunk("Bye", FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
    parrafo2.Alignment = Element.ALIGN_CENTER;

//在这里我尝试删除中间的行,在这里我尝试使用 SetLeading 删除中间的行

    Paragraph phrase = new Paragraph();
    phrase.SetLeading(0, 0);
    phrase.Add(parrafo1);
    phrase.Add(parrafo2);

    cell = new PdfPCell();
    //Paragraph valorcelda = new Paragraph();
    //valorcelda.Add(new Chunk(text_pie, FontFactory.GetFont("Arial", 7, Font.NORMAL, BaseColor.BLACK)));
    //valorcelda.Alignment = Element.ALIGN_CENTER;
    cell.PaddingTop = 0;
    cell.Border = 0;
    cell.FixedHeight = 0;
    cell.Colspan = 3;
    cell.AddElement(phrase);
    table.AddCell(cell);

    pdfDoc.Add(table);



    pdfDoc.Close();
    pdf = stream.ToArray();
}
}

但是当生成 pdf 时,它显示的文本 hello 和 bye 非常分开。我试过用 SetLeading 来做,但它不起作用。我怎么能解决这个问题?

在此处输入图像描述

标签: c#itext

解决方案


从我的测试中可以看出......这条线......</p>

phrase.SetLeading(0, 0);

没有做你期望的事情。在我有限的研究中,这似乎应该设置领先,但正如这里所指出的,它似乎没有像预期的那样设置领先。

什么会起作用,将引导设置为单个段落,例如......</p>

parrafo2.Leading = XX;

推荐阅读