如何使用 itextsharp 减少或删除段落之间的线条?
How to decrease or remove the lines that are between the paragraphs with 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 来做,但它不起作用。我该如何解决这个问题?
根据我在测试中所知道的……这条线……
phrase.SetLeading(0, 0);
没有达到您的预期。在我有限的研究中,这似乎应该设置领先,但正如这里所指出的,它似乎没有像预期的那样设置领先。
什么是可行的,将前导设置为单个段落,例如...
parrafo2.Leading = XX;
我有以下代码,我在其中使用 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 来做,但它不起作用。我该如何解决这个问题?
根据我在测试中所知道的……这条线……
phrase.SetLeading(0, 0);
没有达到您的预期。在我有限的研究中,这似乎应该设置领先,但正如这里所指出的,它似乎没有像预期的那样设置领先。
什么是可行的,将前导设置为单个段落,例如...
parrafo2.Leading = XX;