Add rectangle into pdfpcell itextsharp

流过昼夜 提交于 2019-12-08 02:02:40

问题


How can I add a rectangle with certain width, height and background color into a PdfPCell using itextsharp?

Something like this:

                    PdfPCell cell = new PdfPCell();

                    Rectangle rectangle = new Rectangle();
                    rectangle.Width = 50f;
                    rectangle.BackgroundColor = BaseColor.RED;

                    cell.AddElement(cell);

回答1:


The simple answer is: draw the Rectangle as a Form XObject (PdfTemplate), wrap it inside an Image object, and add that image to the table.

However: there are several ways to do this, and there may be only one way that results in the desired output. That's why I've made you an example: rectangle_in_cell.pdf

Take a close look at this PDF. In the top margin you see a line that measures 120 pt in length. In the different tables, you see three rectangles that were created as a rectangle measuring 120 by 80 pt. Only one rectangle seems to have the correct size.

When adding objects to a table, iText often resizes the content to make it fit into a cell. The RectangleInCell example shows you the differences in code between the three approaches. It's written in Java, but I'm sure you'll be able to adapt it to C#.



来源:https://stackoverflow.com/questions/21625435/add-rectangle-into-pdfpcell-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!