问题
Update:
After some discussion, we decided to go with TeX, specifically the windows compatible MiKTeX. We realised that even if we could get the dynamic lengthed table formatted by micromanaging the layout (which doesn't seem possible or is as tedious as calculating row height for each row), there are other dynamic controls like large text boxes that we would also need to micromanage so we decided to generate the whole doc on the fly. With that in mind, going to TeX was the obvious choice because of it's power and our prior experience with it and now after a week, I'm glad we went that way because all our reports are dynamically generated and the code behind is clean and minimal.
Original:
I've got a pdf form template generated using LiveCycle and I want to fill it in (pdfstamper) and add some tables (pdfptable), however it's proven to be more difficult than I initially thought.
I open a pdf and use pdfstamper to edit the static fields:
using (var outputPDF1 = new MemoryStream())
{
var pdfReader = new PdfReader(pdf);
var pdfStamper = new PdfStamper(pdfReader, outputPDF1);
var pdfFields = pdfStamper.AcroFields;
pdfFields.SetField("Field1", "Value1");
This is straight forward and clear.
Then I attempt to add a table that will go from a set location over several pages. There is an attempt of this here that is 3 years old with a much older version of itextsharp and it's very manual.
The crux of that code is to use GetOverContent to insert a ColumnText with the generated table however this requires knowing the table height and manually cutting the table to size
var cb = pdfStamper.GetOverContent(1);
var ct = new ColumnText(cb);
ct.Alignment = Element.ALIGN_CENTER;
ct.SetSimpleColumn(36, 36, PageSize.A4.Width-36, PageSize.A4.Height-300);
ct.AddElement(table);
ct.Go();
There is another answer Itextsharp: Adjust 2 elements on exactly one page which is limited to a single page table. It can be extendable but it seems like you'd have to calculate headers/footers as well on subsequent pages.
And various unanswered questions in the same general direction:
adding a dynamic Table to an pdf template
itextsharp place Pdfptable at desired position
So my question is, what is the current best way to statically create a pdf using a WYSIWYG editor and modify it to add dynamic sized content such as a table or arbitrary lengthed text? I don't want to generate one from scratch if we can use a WYSIWYG LiveCycle to get the template working, but if getting a table formatted for a pdf template takes more effort than generating the whole thing on the fly, then i'd rather generate the whole pdf on the fly using itextsharp.
回答1:
First this: your code isn't ever going to work if you don't flatten the form. In a document created with LiveCycle Designer, the PDF acts as a container of XML. A PdfPTable can be used to create PDF syntax, which is very different from XML.
Secondly: in your code sample, you're adding a table OVER the existing content of the first page. Is this your intention? Why not create a separate document with a table, and then merge the document created from scratch with the filled-out and flattened form? Are you assuming that the existing content will reflow? In that case, you don't understand PDF, please do some more reading.
Suppose you are using this code:
var cb = pdfStamper.GetOverContent(1);
var ct = new ColumnText(cb);
ct.Alignment = Element.ALIGN_CENTER;
ct.SetSimpleColumn(36, 36, PageSize.A4.Width-36, PageSize.A4.Height-300);
ct.AddElement(table);
ct.Go();
If this works fine for you, except when the table doesn't fit one page, why don't you insert a second page? You're referring to an example where there's a line:
int status = ct.go();
You should use the value of status to find out if the table fits the column or not. As long as the table isn't rendered completely, you should insert pages, and add parts of the table to those new pages.
Of course: your design would be much better if you adapted your form, rather than inventing some 'hybrid' approach.
来源:https://stackoverflow.com/questions/12364926/how-to-insert-a-pdfptable-into-an-existing-pdf-template