How to control the pagination when converting a text file to pdf?

Question:

I am trying to converting a text file (.txt) to pdf using Convert.FromText API but it generates more pages than it should, how should I control where to break the page? A sample file (sample.txt) and the code snippet are provided.

````pdftron.SDF.Obj options = objset.CreateDict();``pdftron.PDF.Convert.FromText(pdfdoc, “sample.txt”`` options);

Answer:

This is actually an expected behavior for FromText API. Unless, you explicitly specify where to break the page, our converter will do its own layout/pagination based on the contents. It breaks pages under two conditions:

  1. There is no space for fitting the content within the current page.
  2. It sees a Form Feed character.

Thus, for that particular document, you will need to do two things to make it behave ‘correctly’.

  1. Specify a reasonable page with and height for the pdf.
  2. Modify the text file to insert the Form Feed character at the place where you want the converter to break your page. The revised file with Form Feed characters in the attachment named sample-formfeeds.txt

Codes:

`
pdftron.SDF.Obj options = objset.CreateDict();

options.PutNumber(``"PageWidth"``, 20);
options.PutNumber(``"PageHeight"``, 10);

pdftron.PDF.Convert.FromText(pdfdoc, "sample-formfeeds.txt" options);``
`

Please note that FromText API will apply its default settings if the Options objects doesn’t specify the corresponding settings. Available options and its explanations are listed in the following table:

> Option Name             | Type    | Defaults | Note                                                                                                  |
>-------------------------|---------|----------|-------------------------------------------------------------------------------------------------------|
> BytesPerBite            | Integer | 1024     | In bytes. Use for streaming conversion only.                                                          |
> FontFace                | String  | Arial    | Set the font face used for the conversion.  Default is Courier is the UseSourceCodeFormatting is true |
> FontSize                | Integer | 12.0     | Set the font size used for the conversion.                                                            |
> LineHeightMultiplier    | Double  | 1.15     | Set the line height multiplier used for the conversion.                                               |
> MarginBottom            | Double  | 1.25     | In inches. Set the bottom margin of the page.                                                         |
> MarginLeft              | Double  | 1.25     | In inches. Set the left margin of the page.                                                           |
> MarginRight             | Double  | 1.25     | In inches. Set the right margin of the page.                                                          |
> MarginTop               | Double  | 1.25     | In inches. Set the top margin of the page.                                                            |
> PageHeight              | Double  | 11.0     | In inches. Set the page height.                                                                       |
> PageWidth               | Double  | 8.5      | In inches. Set the page width.                                                                        |
> UseSourceCodeFormatting | Boolean | False    | Set whether to use mono font for the conversion.                                                      |

sample-formfeeds.txt (9.33 KB)

sample.txt (9.25 KB)