2

I have a requirement to insert Content into the middle of the page in a PDF.

The Content may be a Dynamic Table or an Image.

My Concept was to first split the PDF into 2 parts, then get the new Content that is to be added and append by replacing a place holder field.


the Splitting is called Tiling as per IText and here is an example for the same. http://itextpdf.com/examples/iia.php?id=116

The Code above has 2 drawbacks: 1. It splits the page into 16 parts. but that is part of the example. Still i cant figure out a way to split the file into 2 parts only. 2. secondly the split page is converted to a complete page thus disturbing its proportions.


The Rearranging code is the another problem.

The remaining Content should be re-ordered in append mode. but till yet i have only found codes to add complete new pages rather than just the content.

I have found a code that appends the PDF content by replacing a placeholder:

float[] fieldPosition= pdfTemplate.getAcroFields().getFieldPositions("tableField");
PdfPTable table = buildTable();
PdfContentByte cb = stamper.getOverContent(1);
table.writeSelectedRows(0, -1, fieldPosition[1],fieldPosition[4],cb);

Please help me to solve this requirement.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Fr_nkenstien
  • 1,923
  • 7
  • 33
  • 66

1 Answers1

3

PDF is a presentation format, not an edition format. In other words, it is not designed to allow content insertion, with the original content reflowing gracefully. As a consequence, no tool (at least, none that I know of, and surely not iText) will enable you to achieve what you were given as a requirement.

My advice :

  • refuse the assignment since it's not feasible, or
  • get your hands on the original document, insert the desired extra content, and then convert to PDF.
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • Hi Alexis, i disagree with you. I have already edited the PDF to some extent and i have been told that the same has been done before but the code backup is unavailable. I am still looking to find the complete solution – Fr_nkenstien Aug 23 '12 at 11:46
  • 1
    Editing a PDF file is possible : you can add/remove pages, stamp content over/under, sign, crop, merge, etc... However, no tool that I know of will enable you to modify a PDF file in a way that the original content will reflow and adapt itself to the newly added content. – Alexis Pigeon Aug 23 '12 at 11:53
  • Is there any way to extract the content in byte form?? I have seen that there is a class called PDFContentByte in IText Library. If we can use that to extract the contents in byte format and then append it after the added PDF content byte then that might solve the problem. unfortunately i dont know how to do this currently. – Fr_nkenstien Aug 23 '12 at 13:33
  • 1
    This is **very** low-level, and if you don't know a lot about the PDF syntax, my only recommendation is to give up right here and now, unless you are ready to dive into the 978 pages of the [PDF Reference](http://partners.adobe.com/public/developer/en/pdf/PDFReference.pdf). – Alexis Pigeon Aug 23 '12 at 14:19
  • @VineetVerma have you got solution ? – Cs ツ Jun 12 '13 at 10:02