简体   繁体   English

如何使用itext和java阅读pdf并获取表格单元格高度

[英]how read pdf using itext and java and get table cell height

First I have created a pdf using itext and java and put a table and tableCell 首先,我使用itext和java创建了一个pdf文件,并放置了一个table和tableCell

 PdfPTable table = new PdfPTable(2);
        table.setWidths(new int[]{1, 2});
        PdfPCell cell;
        table.addCell("Address:");
        cell = new PdfPCell(new Phrase(""));
        cell.setFixedHeight(60);
        table.addCell(cell);  

I have another Program Which read this pdf File 我有另一个程序读取此pdf文件

  PdfReader reader = new PdfReader("path_of_previously_created_pdf");

Now i want to get TableCell cell and want to Change cell height cell.setFixedHeight(new_Fixed_Height); 现在我想获取TableCell单元格并想要更改单元格高度。cell.setFixedHeight(new_Fixed_Height);

It is possible... if Yes . 可能...如果是。 How?? 怎么样?? Thanx in advance 提前感谢

If your PDF contains just that simple 1x2 table, it of course would be possible to implement something that gives you the PDF with a cell hight of you choice. 如果您的PDF仅包含一个简单的1x2表,那么当然可以实现一些使您拥有选择的单元格的PDF。

But I assume it eventually is meant to contain more. 但我认为它最终将包含更多内容。 Already the code you provided via your google drive included more (more table cells plus form elements), and that code, too, does look unfinished concerning the PDF construction. 您通过google驱动器提供的代码已经包含了更多的代码(更多的表格单元格和表单元素),并且该代码对于PDF的构造也未完成。 Thus,... 从而,...

The direct answer 直接答案

It is not possible. 这不可能。

First of all the table and cell objects you have while creating the PDF are not present as such in the resulting file, they merely are drawn as a number of lines and some text (or whatever you put into the cells). 首先,在创建PDF时拥有的tablecell对象在结果文件中并不存在,它们只是以多行和一些文本(或您放入单元格中的任何内容)绘制。

Thus, you cannot even retrieve the cells you want to change, let alone change it. 因此,您甚至无法检索要更改的cells ,更不用说更改它了。

The twisted answer 扭曲的答案

You could, of course, try and parse the page content stream to find the commands for drawing lines, find those ones among them which were drawn for the cell you are interested in, and try to derive the original cell dimension attributes from the line coordinates. 当然,您可以尝试解析页面内容流以找到用于绘制线条的命令,在其中找到针对您感兴趣的cell绘制的命令,然后尝试从线坐标中得出原始cell尺寸属性。 Afterwards you can attempt to move everything below the cell down to create the extra space you want. 之后,您可以尝试将单元格下面的所有内容向下移动以创建所需的额外空间。

Depending on the information you have (Do you know the approximate position of the cell? If not, do you at least know some unique content of it?) reading the current cell height will include some guesswork and much coding because unfortunately the iText parser framework does not yet support parsing path operations. 根据您所拥有的信息(您知道单元格的大概位置吗?如果不是,您至少知道单元格的一些独特内容吗?)读取当前单元格高度将包括一些猜测和大量编码,因为不幸的是,iText解析器框架尚不支持解析路径操作。

Essentially you have to enhance the classes in the PDF parser package to also process and emit events for PDF path operators (if you know your way around in iText and the PDF specification that should not take more than a week or two) and create an appropriate event listener to find the lines surrounding the cell position you already know (not more than one day of work). 从本质上讲,您必须增强PDF解析器包中的类,以便为PDF路径运算符处理和发出事件(如果您知道iText和PDF规范中的过程应该不超过一两个星期),并创建一个适当的事件侦听器,以查找围绕您已经知道的单元格位置的行(不超过一天的工作时间)。 Some iText code analysis will show how the fixed cell height and the distance of the surrounding lines relate. 某些iText代码分析将显示固定单元格高度与周围线条的距离之间的关系。

Most likely, though, this is the smaller part of your work. 不过,这很可能是您工作的一小部分。 The bigger part is actually manipulating the page content: 实际上,较大的部分是操纵页面内容:

If you are lucky, all your page content is located in a single content stream. 如果幸运的话,所有页面内容都位于单个内容流中。 In that case you merely have to analyse all the page content again but this time to actually change it. 在那种情况下,您只需要再次分析所有页面内容,但这一次实际进行更改。 The easiest way would be to enhance the classes in the parser package once again (because they already do much of the necessary math and book-keeping) to signal every command from the content stream with normalized coordinates (this might take a week or two). 最简单的方法是再次增强解析器包中的类(因为它们已经完成了许多必要的数学和簿记工作),以使用规范化的坐标来指示内容流中的每个命令(这可能需要一两个星期) 。 Based on this information signaled to you built an all new content stream in which you leave everything above your cell, move down everything below, and stretch everything crossing the line on which the bottom border of your cell lies (another week maybe). 根据通知您的信息,您将构建一个全新的内容流,在该流中,您将所有内容保留在单元格上方,将所有内容向下移动,并拉伸所有内容,使其越过单元格底部边界所在的线(可能是另一周)。

If you are less lucky you have to fight with multiple included form xobjects crossing the line. 如果运气不佳,则必须与多个包含在内的表单xobjects进行对抗。 As those xobjects may be used from other streams also, you cannot change them but have to either change a copy or include the xobject content in your newly created stream. 由于这些xobject也可以从其他流中使用,因此您不能更改它们,而必须更改副本或将xobject内容包括在新创建的流中。

Then what about images crossing the line? 那么图像越界呢? or interesting patterns? 还是有趣的模式? In that case stretching the cell will utterly distort everything. 在这种情况下,拉伸细胞会完全扭曲一切。

And then there are annotations, eg your form fields. 然后是注释,例如您的表单字段。 You need to shift and stretch them, too. 您也需要移动和拉伸它们。

Thus, while this approach is possible to follow, please be aware that (depending on how generic the solution has to become) its implementation will take someone knowing iText and PDF some months. 因此,尽管可以采用这种方法,但是请注意(取决于解决方案的通用性),其实施将需要花费一些时间来了解iText和PDF的人员。

An alternative approach 另一种方法

You say in a comment 您在评论中说

I am working on Pdf Form.I have created itext form using TextField(MULTILINE TEXT) once. 我正在使用Pdf Form。我曾经使用TextField(MULTILINE TEXT)创建了一个itext表单。 After read this pdf and fill up the form but when the content increases it shows scroll Bar and content hide. 阅读此pdf文件并填写表格后,但是当内容增加时,它会显示滚动条和内容隐藏。 My problem is Once i print the pdf it did't print hide content. 我的问题是,一旦我打印pdf,它就不会打印隐藏内容。

Why don't you simply for each set of data build an individual PDF with all the cells big enough for the form contents of the respective data set and copy the field values into this new PDF. 您为什么不简单地为每个数据集构建一个单独的PDF,使其所有单元格都足够容纳相应数据集的表单内容,然后将字段值复制到该新PDF中。 This is a fairly simple approach, yet flexible enough to not waste too much space but at the same time not hide content. 这是一种相当简单的方法,但足够灵活,不会浪费太多空间,但同时不会隐藏内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM