java设置pdf表格外边框_Itextsharp PDFPTable如何在整个表格周围制作边框

如你所见,PdfPTable不是't have borders, probably because PDF'首先没有表格 . 将边框直接放在PdfPCell上可能更有意义(尽管PDF也不支持这些) . 无论如何,表只是一个单元的集合,所以让他们处理它 .

无论如何,解决方案是在 PdfPTable 类的实例上设置 TableEvent . 为此,您需要IPdfPTableEvent界面的自定义实现 . 以下代码通常应该为您执行此操作(请参阅底部的注释"generally")

class TopBottomTableBorderMaker : IPdfPTableEvent {

private BaseColor _borderColor;

private float _borderWidth;

///

/// Add a top and bottom border to the table.

///

/// The color of the border.

/// The width of the border

public TopBottomTableBorderMaker(BaseColor borderColor, float borderWidth ) {

this._borderColor = borderColor;

this._borderWidth = borderWidth;

}

public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {

//widths (should be thought of as x's) is an array of arrays, first index is for each row, second index is for each column

//The below uses first and last to calculate where each X should start and end

var firstRowWidths = widths[0];

var lastRowWidths = widths[widths.Length - 1];

var firstRowXStart = firstRowWidths[0];

var firstRowXEnd = firstRowWidths[firstRowWidths.Length - 1] - firstRowXStart;

var lastRowXStart = lastRowWidths[0];

var lastRowXEnd = lastRowWidths[lastRowWidths.Length - 1] - lastRowXStart;

//heights (should be thought of as y's) is the y for each row's top plus one extra for the last row's bottom

//The below uses first and last to calculate where each Y should start and end

var firstRowYStart = heights[0];

var firstRowYEnd = heights[1] - firstRowYStart;

var lastRowYStart = heights[heights.Length - 1];

var lastRowYEnd = heights[heights.Length - 2] - lastRowYStart;

//Where we're going to draw our lines

PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];

//I always try to save the previous state before changinge anything

canvas.SaveState();

//Set our line properties

canvas.SetLineWidth(this._borderWidth);

canvas.SetColorStroke(this._borderColor);

//Draw some rectangles

canvas.Rectangle(

firstRowXStart,

firstRowYStart,

firstRowXEnd,

firstRowYEnd

);

//They aren't actually drawn until you stroke them!

canvas.Stroke();

canvas.Rectangle(

lastRowXStart,

lastRowYStart,

lastRowXEnd,

lastRowYEnd

);

canvas.Stroke();

//Restore any previous settings

canvas.RestoreState();

}

}

使用它非常简单,只需将实例绑定到属性:

//Create your name as you normally do

var table = new PdfPTable(3);

//Bind and instance with properties set

table.TableEvent = new TopBottomTableBorderMaker(BaseColor.BLACK, 0.5f);

//The rest is the same

for (var i = 0; i < 6; i++) {

var cell = new PdfPCell(new Phrase(i.ToString()));

cell.HorizontalAlignment = Element.ALIGN_CENTER;

cell.VerticalAlignment = Element.ALIGN_MIDDLE;

cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220);

cell.Border = 0;

cell.BorderColorLeft = BaseColor.BLACK;

cell.BorderWidthLeft = .5f;

cell.BorderColorRight = BaseColor.BLACK;

cell.BorderWidthRight = .5f;

table.AddCell(cell);

}

上面我说"generally"应该有用 . 但是,如果您有表格页眉和/或页脚,那么您需要调整 y 值,因为 table.HeaderRows 和 table.FooterRows .

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题需要用到 iTextSharp 库来解决。首先,你需要安装该库并导入它。然后,你可以使用以下代码获取指定关键字的坐标位置信息: ```c# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; namespace PdfKeywordCoordinates { class Program { static void Main(string[] args) { string filename = @"C:\example.pdf"; // pdf 文件路径 string keyword = "example keyword"; // 指定关键字 using (PdfReader reader = new PdfReader(filename)) { for (int page = 1; page <= reader.NumberOfPages; page++) { ITextExtractionStrategy strategy = new LocationTextExtractionStrategy(); string currentText = PdfTextExtractor.GetTextFromPage(reader, page, strategy); if (currentText.Contains(keyword)) { var kwLocation = new List<RectAndText>(); var renderFilter = new RenderFilter[1]; renderFilter[0] = new RegionTextRenderFilter(new Rectangle(0, 0, 1000, 1000)); var textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter); PdfContentStreamProcessor processor = new PdfContentStreamProcessor(textExtractionStrategy); processor.ProcessContent(reader.GetPageContent(page)); kwLocation = ((LocationTextExtractionStrategy)textExtractionStrategy).GetLocations(); foreach (RectAndText rectAndText in kwLocation) { if (rectAndText.text.Contains(keyword)) { Console.WriteLine("Page: " + page + " X: " + rectAndText.rect.Left + " Y: " + rectAndText.rect.Bottom); } } } } } Console.ReadLine(); } } public class RectAndText { public iTextSharp.text.Rectangle rect; public String text; public RectAndText(iTextSharp.text.Rectangle rect, String text) { this.rect = rect; this.text = text; } } } ``` 这个代码将在指定的 PDF 文件中查找指定的关键字,并输出该关键字在每一页中的坐标位置信息。注意,这个代码是使用 C# 编写的,如果你使用的是 Python,你需要使用 Python 版本的 iTextSharp 库,并使用相应的语法来实现相同的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值