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

Free Support Forum - aspose.com

Position for TextFragment

Hello,
I am struggling to correctly position my text on PDF. While setting position for text fragment does it consider relative x,y coordinates or absolute. I am having a simple page with Margin of 36 on all sides. I want to center position my text. Can you provide me some code snippet for that.
Regards,
Sandeep

@sgarg.saba

Could you please ZIP and attach your input, problematic and expected output PDF files here for our reference? Please also share the code example that you are using to generate the PDF. We will investigate the issue and provide you more information on it along with code example.

Please see below code and my expectation is this text come in middle of PDF though currently it is not in proper middle.
string dataDir = @“C:”;

        // Initialize document object
        Document document = new Document();
        
        // Add page
        Page page = document.Pages.Add();
        page.SetPageSize(PageSize.PageLetter.Width,PageSize.PageLetter.Height);
        page.Background= Color.AliceBlue;
        page.PageInfo.Margin = new MarginInfo(36, 36, 36, 36);
        //document.PageInfo.
        // Add text to new page

        // Calculate title size and wrapping for optimal space usage
        var sizeWidth = page.PageInfo.Width - page.PageInfo.Margin.Left - page.PageInfo.Margin.Right;
        var sizeHeight = page.PageInfo.Height - page.PageInfo.Margin.Top - page.PageInfo.Margin.Bottom;
        var coverText = "Hello World";
        var coverStyle = new TextState
        {
            Font = FontRepository.FindFont(DefaultFont),
            FontSize = 45,
            ForegroundColor = Color.Black,
            //IsUnicode = true,   
        };
        var titleWidth = coverStyle.MeasureString(coverText);
        var wrapLines =(int) Math.Ceiling(titleWidth / sizeWidth);
        var titleHeight = 45 * wrapLines;

        // Render the title
        var title = new TextFragment(coverText)
        {
            //PositioningType = PositioningType.ParagraphRelative,
            //ReferenceParagraphID = root.ID,
            Position = new Position(sizeWidth > titleWidth
                    ? ((sizeWidth - titleWidth) / 2)
                    
                    : 0, (
                          (sizeHeight > titleHeight
                              ? (sizeHeight - 45 * wrapLines) / 2
                              : 0))
                ),
            WrapLinesCount = wrapLines,
        };
        title.TextState.ApplyChangesFrom(coverStyle);
        title.TextState.HorizontalAlignment = sizeWidth < titleWidth
            ? Aspose.Pdf.HorizontalAlignment.Center
            : Aspose.Pdf.HorizontalAlignment.Left;

        page.Paragraphs.Add(title);

        //page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World!"));
        // Save updated PDF
        document.Save(dataDir + "HelloWorld_out.pdf");

@sgarg.saba

We suggest you please use the following code example to insert the text in the center of PDF page. Hope this helps you.

// Initialize document object
Document document = new Document();

// Add page
Page page = document.Pages.Add();
page.SetPageSize(PageSize.PageLetter.Width, PageSize.PageLetter.Height);
page.Background = Aspose.Pdf.Color.AliceBlue;
page.PageInfo.Margin = new MarginInfo(36, 36, 36, 36);


var textStamp = new Aspose.Pdf.TextStamp("Aspose.Pdf for .NET stamping test")
{

    Background = false,
    Opacity = 0.5,
    HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center,
    VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center,
    TextAlignment = Aspose.Pdf.HorizontalAlignment.Center
};

textStamp.TextState.Font = FontRepository.FindFont("Arial");

textStamp.TextState.FontSize = 16.0f;

textStamp.TextState.FontStyle = FontStyles.Bold;

textStamp.TextState.FontStyle = FontStyles.Italic;

textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(121, 177, 0));

page.AddStamp(textStamp);
document.Save(dataDir + "out.pdf");

There is an issue with large text (going outside the pdf ) also how we can set it with multiline and also if dynamic logo is added on top of pdf how we can shift to center of the page excluding logo height.

@sgarg.saba

Could you please attach your expected output PDF file here for our reference? We will then provide you more information on it.