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

Free Support Forum - aspose.com

Aspose.pdf for .net : use Position or Justify not working

I use aspose.net PDF V18.12.0, i want to write a long text in a particular position on the page :

  • in the simple version, i only use a textfragment but the textfragment.position is ignore ?
  • when i use a textparagraph, the position is correct but with left alignment, only a part of my text is print, in justify alignment , only the fisrt word are write

Is there anything wrong with my code ?

    _pdfDocument = new Document()
    { PageInfo = new PageInfo { Margin = new MarginInfo(28, 28, 28, 42) }};

    _pdfPage = _pdfDocument.Pages.Add();

    TextFragment textFragment = new TextFragment("Ibi victu recreati et quiete, postquam abierat timor, vicos opulentos adorti" +
    " equestrium adventu cohortium, quae casu propinquabant, nec resistere " +
    "planitie porrecta conati digressi sunt retroque concedentes omne iuventutis " +
    "robur relictum in sedibus acciverunt.Ibi victu recreati et quiete, postquam abierat timor, vicos " +
    "opulentos adorti equestrium adventu cohortium, quae casu propinquabant, nec resistere planitie porrecta " +
    "conati digressi sunt retroque concedentes omne iuventutis robur relictum in sedibus acciverunt.");

    textFragment.Position = new Position(200, 500);

    // Set text properties
    textFragment.TextState.FontSize = 12;
    textFragment.TextState.Font = FontRepository.FindFont("Calibri");
    textFragment.HorizontalAlignment = HorizontalAlignment.Justify;

    // Position ignore ?
    _pdfPage.Paragraphs.Add(textFragment);

    TextParagraph parbloc = new TextParagraph();
    parbloc.Rectangle = new Rectangle(50, 100, 500, 400);

    // With Left => Text is partially print
    // with Justify => only the first words are printed
    parbloc.HorizontalAlignment = HorizontalAlignment.Left; 
    parbloc.FormattingOptions = new TextFormattingOptions(TextFormattingOptions.WordWrapMode.ByWords);
    parbloc.VerticalAlignment = VerticalAlignment.Top;
    parbloc.AppendLine(textFragment);

    TextBuilder textBuilder = new TextBuilder(_pdfPage);
    textBuilder.AppendParagraph(parbloc);

    // Step 5
    _pdfDocument.Save("D:\\PDF.pdf");

@JMVESCOVI

Thank you for contacting support.

We have been able to notice the problem with the code snippet shared by you and a ticket with ID PDFNET-45816 has been logged in our issue management system for further investigations. However, you may use below code snippet to achieve your requirements. This table wrapper approach allows you to write long text in a particular position on the page.

string sampleString = "Ibi victu recreati et quiete, postquam abierat timor, vicos opulentos adorti" +
" equestrium adventu cohortium, quae casu propinquabant, nec resistere " +
"planitie porrecta conati digressi sunt retroque concedentes omne iuventutis " +
"robur relictum in sedibus acciverunt.Ibi victu recreati et quiete, postquam abierat timor, vicos " +
"opulentos adorti equestrium adventu cohortium, quae casu propinquabant, nec resistere planitie porrecta " +
"conati digressi sunt retroque concedentes omne iuventutis robur relictum in sedibus acciverunt.";

Document document = new Document();
Aspose.Pdf.Page page = document.Pages.Add();
Table table = new Table();
// Specify position of table
table.Left = 40;
table.Top = 500;
table.ColumnWidths = ("300");
// Create text fragment
TextFragment tf = new TextFragment(sampleString);
tf.TextState.Font = FontRepository.FindFont("Arial");
tf.TextState.FontSize = 9;
tf.TextState.LineSpacing = 2f;
tf.TextState.HorizontalAlignment = HorizontalAlignment.Left;

FloatingBox floatingBox = new FloatingBox();
Aspose.Pdf.Row row = table.Rows.Add();
Cell cell = row.Cells.Add();
page.Paragraphs.Add(floatingBox);
cell.Paragraphs.Add(tf);
floatingBox.Paragraphs.Add(table);
document.Save(dataDir + "Workaround_18.12.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.