c# - 如何使用 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 来做,但它不起作用。我怎么能解决这个问题?
解决方案
从我的测试中可以看出......这条线......</p>
phrase.SetLeading(0, 0);
没有做你期望的事情。在我有限的研究中,这似乎应该设置领先,但正如这里所指出的,它似乎没有像预期的那样设置领先。
什么会起作用,将引导设置为单个段落,例如......</p>
parrafo2.Leading = XX;
推荐阅读
- docker - Gitlab CI - 错误:作业失败:执行程序需要 OSType=linux,但 Docker 引擎仅支持 OSType=windows
- r - R:动态更新公式
- three.js - 如何将精灵定位在画布标注的特定点?
- python-3.x - Pycharm 自动导入不适用于 f 字符串中的变量
- asp.net - vb.net 和 asp.net 映射选项
- docker - 评估 Hadoop 性能所需的数据大小
- angular - 类型错误:this.root_ 为空
- python - 哪种距离测量最适合具有不同值范围的特征?
- uwamp - 以静音模式启动 uWAmp
- python - 定期或在事件触发时执行函数