Is it possible to “shrink” a PdfPtable?

戏子无情 提交于 2019-12-12 17:22:36

问题


I am currently working with Itextsharp and I have some trouble with PDfPtables. Sometimes they get too big for a page and, when added to a document, are broken up on multiple pages. Sadly ths rational behviour is not acceptable for some of my superiors - they keep insisting that the table shall be "shrunk" to a page. Is there a way to achieve this? There are some tantalizing hints that i could be possible - but hints are all that i have.

What is the alternative? Perhaps I could delete the fat table from the document and build the table again with smaller Fonts and Cells, but that would be very cumberosme - I would prefer to "zoom out", in lack of a better word.

My current code:

Dim test As PdfContentByte = mywriter.DirectContent
Dim templ = test.CreateTemplate(mywriter.PageSize.Width, mywriter.PageSize.Height)
Table.WriteSelectedRows(0, Table.Rows.Count - 1, 0.0F, mywriter.PageSize.Height, templ)
Dim myimage = Image.GetInstance(templ)
'   myimage.ScaleAbsolute(mywriter.PageSize.Width, mywriter.PageSize.Height)

would scaleabsolute be necessary? myimage.SetAbsolutePosition(0, 0) test.AddImage(myimage)

This code puts something one the page, but it has the height of the page and the width is about a quarter of the page - wi will try to find the bug...


回答1:


Create the table and define a 'total width'. As soon as iText knows the width of the table, you can calculate the height of all the rows. Once you know the height, you can check:

  1. Does the table fit the page? Just add it as is. Maybe using WriteSelectedRows if you don't want to take any page margins into account.
  2. Isn't there enough space on the page? Add the table to a PdfTemplate (there's more than one way to do this), wrap the PdfTemplate inside an Image. Scale the image, and add it to the document.


来源:https://stackoverflow.com/questions/12970736/is-it-possible-to-shrink-a-pdfptable

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