14

I have created a document in an open office with a multi-line form field:

multi-line form field

The issue I am having is when the dynamic content exceeds the initial size of the multi-line text box:

multi-line text box dynamic content

Sure I can re-size the Text Box in the original template but the dynamic content may be from 1 to 50 lines and I want the text after the Text Box to be close to the last line of dynamic content.

Can someone suggest a way to solve this?

Zoltán Balogh
  • 188
  • 2
  • 11
wal
  • 17,409
  • 8
  • 74
  • 109
  • 2
    Your form is base on AcroForm technology. There is no way AcroForm field can adapt to the size of the content in AcroForm technology. You can only make the content adapt to the field by setting the font size to 0. If that is not an option for you, then your requirement can't be met. You should abandon the use of AcroForm technology and define the template in another format. – Bruno Lowagie Jul 27 '17 at 13:14
  • @BrunoLowagie `and define the template in another format` <-- could you explain further? – wal Jul 28 '17 at 05:00
  • @BrunoLowagie added some bounty – wal Jul 29 '17 at 03:19
  • 2
    Have you considered building your PDF using iText instead of iText merely filling a template? That would not be WYSIWYG but you would have more dynamic design options concerning variable parts. – mkl Jul 29 '17 at 09:32
  • @mkl would require a rebuild each time business wants to change the wording ? – wal Jul 29 '17 at 13:01
  • It is easy to load texts from an external resource. You merely have to remember doing that when implementing the task. It even is possible to put parts of the formatting there. The more dynamical the solution shall be, though, the more programming is necessary. For a very dynamic solution you might consider using a good reporting tool. – mkl Jul 29 '17 at 16:30
  • @BrunoLowagie, why don't you create a PDF-Exportable Crystal Report template? I mean, you can provide simple FORMS to user type entries (based on panels and tabs) and, after, create a PDF file using the export capability of Crystal. In this case, you can provide preview to the user, that will export simply pressing EXPORT button. – David BS Aug 03 '17 at 16:57
  • 2
    @DavidBS I am currently writing a tutorial explaining how to convert HTML+CSS to PDF. Creating a template in HTML is really easy, but for those who disagree, we are also working on a designer to create this HTML and CSS. It's strange to see that a bounty was added for this question. The answer to the specific question is "AcroForm templates are NOT DYNAMIC. You are asking something that is impossible." The implicit question is "Can you recommend a tool or template technology?" Such a question is off-topic on Stack Overflow. – Bruno Lowagie Aug 04 '17 at 09:41
  • @BrunoLowagie I did not ask a question about AcroForm directly (perhaps indirectly) - More simply was that business gives you a template (as a pdf) and then you need to inject dynamic content into it - how do you do that ? The template creator needs to leave a market for the dynamic content whether that is a AcroForm or some other placeholder (e.g. some text like `$(FieldName)`) – wal Aug 04 '17 at 12:06
  • If the PDF template is based on AcroForm technology, you have to think of that PDF as digital paper. Do you remember paper forms? If there is room to fill out 5 children, and you have 6 children, you can't magically change the paper form to adapt itself to the data. The same is true for AcroForm templates. If the PDF template is based on XFA technology (XFA = the XML Forms Architecture), you have a dynamic form. An XFA template consists of XML wrapped in PDF. You can inject XML into this PDF, and the form will adapt to the content. XFA is deprecated in PDF 2.0, so I would no longer advise it. – Bruno Lowagie Aug 04 '17 at 12:11
  • @wal Given what I wrote in the previous comment, can you please elaborate and explain what you are talking about? It would help if you would point at a specific paragraph or clause in the PDF specification (either ISO-32000-1 or ISO-32000-2) to tell us what you mean. If you cannot point at such a paragraph or clause, we can all conclude that your question is unanswerable. **Summarized:** please update your question so that people can actually answer it. – Bruno Lowagie Aug 04 '17 at 12:15
  • @BrunoLowagie do i know what i'm talking about with respect to PDFs? no. do i know the PDF spec? no. - hence the reason for my stackoverflow question. i'm looking for a means to an end - perhaps there is a different way - e.g. programmatically searching for a piece of text and doing a find/replace (which also respects new line characters and pushing existing content down) - prior to asking this q i searched existing SO q's and found nothing of this nature surprisingly - if in your professional educated opinion it can't be done using PDFs then thats good enough for me – wal Aug 04 '17 at 12:22
  • *Perhaps there is a different way - e.g. programmatically searching for a piece of text and doing a find/replace.* That is a horrible idea in the context of PDF. *Prior to asking this q i searched existing SO q's and found nothing of this nature surprisingly.* Well, that doesn't surpise *me* because it can't be done in PDF. That's not what PDF was designed for. Read pages 159-160 of [this chapter](https://manning-content.s3.amazonaws.com/download/d/3645210-8560-4e6d-9b03-3f9aca1921a5/samplechapter6.pdf) if you aren't convinced yet. – Bruno Lowagie Aug 04 '17 at 12:25
  • @BrunoLowagie `If the original document was written using Microsoft Word, change the Word document, and make the PDF from the new version of the Word document` - remarkably this is the path i went down since asking this question - not because the template was in Word but because using `docx4j` i was able to manipulate a Word doc as required and then use the same library to convert the docx to pdf. nb, find/replace text in a docx with multi-line content was non-trivial (and realistically far from perfect) – wal Aug 04 '17 at 12:34
  • Any chance you can post a link to the template? If the template is relatively simple, the comment by @BrunoLowagie suggesting the use of a HTML template is a _very_ good idea , and someone should be able to provide you with a good example of how to get this done. – kuujinbo Aug 04 '17 at 16:09
  • @kuujinbo i just constructed the template for the purpose of asking the question on stackoverflow. it looks like the accepted answer is going to be `don't template in pdf; do the template in html and convert to pdf` – wal Aug 04 '17 at 21:52

1 Answers1

-1

I have once coded a solution to set the width and height of a textbox programmatically based on the characters supplied. I think this was a school assignment a long time ago.

This can be done with both VBA in a code behind or probably with a macro even. Or with VB.net.

I don't have the code I used way back when, but basically determine the maximum width you are able to provide in character width, the preferred width. Determine the pixel requirement per character for that width. This becomes the textbox width.

Divide your total string character count by your preferred width character count. Round up, calculate the pixel height per character. And use this value times the rounded result for the textbox height. Dirty but it should work.

Any chance you can change to a label and set AutoSize to True? You can fix the width and let the height auto adjust. This should be done before converting to a pdf. In fact all of the sizing should be resovled before pdf conversion.

Another Down voter without a comment, should not be allowed.

htm11h
  • 1,739
  • 8
  • 47
  • 104
  • I have not down voted yet but I am tempted to. The reason? Well, the question originally is about having a *PDF* template and manipulating it to hold different amounts of text in some field. As discussed in the comments to the question, this procedure is not adequate for PDFs. Yes, you can change PDF field sizes but the text surrounding the field is not reflowed; enlarging PDF fields, therefore, will result in the field overlapping text. Thus, calculating desired field dimensions and updating the PDF field accordingly does not solve the issue. – mkl Oct 09 '17 at 21:37
  • The comments to the question eventually turned towards filling the original Word document (from which the PDF template was created) and transforming the filled document (with updated field sizes) to PDF. This might be where your answer can be of help but you in no way indicate that your answer does not refer to the question as is but to the variant developed in the comments. – mkl Oct 09 '17 at 21:41
  • It is downvoted because it is a poor answer. Your answer does not address moving/pushing content *underneath* the expanded text down which is 90% of the problem. Your answer details how to generically resize something that is a container for text - it provides no code sample (or details) specific to PDF technology and is vague at best – wal Oct 10 '17 at 00:17
  • Somewhat valid points, but the complexity of the OP did not indicate a coding solution was considered. That was the direction of my answer, had there been follow up, I could have perhaps spent a bit of time to conjure up some code, but there was no effort by the OP to pursue that. So not a bad answer just not the one the OP was looking for. Converting to a PDF is trivial and not an element of the solution that is needed once the sizing issue is resolved. – htm11h Oct 10 '17 at 13:20