We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add large text to pdf files

I am using the code below to add large text tothe existing pdf file. However, I do not see expected output. Only latter part of text is seen on the added page and not the whole text.Please advise
protected static File textToPdf(final String text) {
try {
File tempFile = File.createTempFile(ASPOSE_TEXT, Constants.FileType.PDF.getExtension());
Document doc = new Document();
com.aspose.pdf.Page page = doc.getPages().insert(1);
double heigth = PageSize.getA4().getHeight();
double width = PageSize.getA4().getWidth();
double margin = 0.5f * RESOLUTION_72;
page.setPageSize(width, heigth);
MarginInfo marginInfo = new MarginInfo(margin, margin, margin, margin);
page.getPageInfo().setMargin(marginInfo);
// create text paragraph
TextParagraph paragraph = new TextParagraph();
paragraph.getFormattingOptions().setWrapMode(2);
TextFragment textFragment = new TextFragment(text);
textFragment.setText(text);

        textFragment.getTextState().setFont(HELVETICA);
        textFragment.getTextState().setFontSize(11f);
        paragraph.appendLine(textFragment);
        paragraph.setPosition(new Position(X_INDENT, heigth - 30f));
        TextBuilder textBuilder = new TextBuilder(page);
        textBuilder.appendParagraph(paragraph);
        doc.save(tempFile.getAbsolutePath());
        return tempFile;
    } catch (Exception ex) {
        throw new DocumentServiceException(ex);
    }
}

aspose.zip (155.9 KB)

@hardeep17

Thank you for contacting support.

There are some undeclared variables in your code snippet. Kindly share SSCCE code along with the method call so that we may try to reproduce and investigate it in our environment. Before sharing requested code, please ensure using Aspose.PDF for Java 19.4.

Updated Code:

protected static File textToPdf(final String text) {
try {
File tempFile = File.createTempFile(“aText”, “.pdf”);
Document doc = new Document();
com.aspose.pdf.Page page = doc.getPages().insert(1);
double heigth = PageSize.getA4().getHeight();
double width = PageSize.getA4().getWidth();
double margin = 0.5f * 72;
page.setPageSize(width, heigth);
MarginInfo marginInfo = new MarginInfo(margin, margin, margin, margin);
page.getPageInfo().setMargin(marginInfo);
// create text paragraph
TextParagraph paragraph = new TextParagraph();
paragraph.getFormattingOptions().setWrapMode(2);
TextFragment textFragment = new TextFragment(text);
textFragment.setText(text);

        textFragment.getTextState().setFont(FontRepository.findFont("Helvetica", FontStyles.Regular));
        textFragment.getTextState().setFontSize(11f);
        paragraph.appendLine(textFragment);
        paragraph.setPosition(new Position(11f, heigth - 30f));
        TextBuilder textBuilder = new TextBuilder(page);
        textBuilder.appendParagraph(paragraph);
        doc.save(tempFile.getAbsolutePath());
        return tempFile;
    } catch (Exception ex) {
        throw ex;
    }
}

@hardeep17

We have attached generated PDF file for your kind reference where all of the text exists and not just latter part of it. Would you please also share the text you are adding, along with generated PDF document so that we may investigate further. textToPdf_19.4.pdf

I have already attached the text file in my zip. Attaching again.Kindly refer to that
W Select fields on ListRead actions.txt

If you look at text file it is quite long but the output only shows only part of itaspose.zip (155.9 KB)

@hardeep17

Thank you for elaborating it further.

We have been able to notice the problem with large text. A ticket with ID PDFJAVA-38549 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

However, you may try below code snippet as a workaround until the ticket is investigated.

protected static void textToPdf(final String text) throws Exception {
try {
Document doc = new Document();
com.aspose.pdf.Page page = doc.getPages().add();
double height = PageSize.getA4().getHeight();
double width = PageSize.getA4().getWidth();
double margin = 0.5f * 72;
page.setPageSize(width, height);
MarginInfo marginInfo = new MarginInfo(margin, margin, margin, margin);
page.getPageInfo().setMargin(marginInfo);
TextFragment textFragment = new TextFragment(text);
textFragment.setText(text);
textFragment.getTextState().setFont(FontRepository.findFont("Helvetica", FontStyles.Regular));
textFragment.getTextState().setFontSize(11f);
page.getParagraphs().add(textFragment);
doc.save("text2pdf_19.4.pdf");
} catch (Exception ex) {
    throw ex;
    }   
}

We are sorry for the inconvenience.

@hardeep17

We would like to share with you that the earlier logged ticket has been resolved. Creating TextParagraph or using TextBuilder, we can put all the text on the chosen page and also it will be placed on the specific coordinated where the left bottom position is also chosen by you here “ paragraph.setPosition(new Position(11f, heigth - 30f));” . All text is placed outside the visible part of the page: PlacedText.png. PlacedText.png (21.2 KB)

To add the large text in PDF document, started from selected page and with continue placing it when whole page will be filled - use the following code:

Document doc = new Document();
            com.aspose.pdf.Page page = doc.getPages().insert(1);
            double height = PageSize.getA4().getHeight();
            double width = PageSize.getA4().getWidth();
            double margin = 0.5f * 72;
            page.setPageSize(width, height);
            MarginInfo marginInfo = new MarginInfo(margin, margin, margin, margin);
            page.getPageInfo().setMargin(marginInfo);

// create text paragraphs
            String[] lines = text.split(System.getProperty("line.separator"));
            for (String line : lines)
            {
                TextFragment textFragment = new TextFragment(line);
                textFragment.getTextState().setFont(FontRepository.findFont("Helvetica", FontStyles.Regular));
                textFragment.getTextState().setFontSize(11f);
                TextFormattingOptions tfo = new TextFormattingOptions();
                tfo.setWrapMode(TextFormattingOptions.WordWrapMode.ByWords);
                textFragment.getTextState().setFormattingOptions(tfo);
                page.getParagraphs().add(textFragment);
            }
            System.out.println(tempFile.getAbsolutePath());
            doc.save(tempFile.getAbsolutePath()); 

out_21_5_AllText.pdf (773.9 KB)

Please try the above code snippet with 21.5 version of the API and let us know in case you face any issue.