Converting from String to BaseParagraph (C#)

I’ve been given the task of generating PDFs using data stored in an SQL database using ASP.NET and C#. So far, I have managed to store the results of SQL queries into string variables, but when I use these variables to pull the data into a PDF format, I’m given the following error:

Argument 1: cannot convert from ‘string’ to ‘Aspose.Pdf.BaseParagraph’

How should I navigate this challenge? Thank you in advance. Below is the code that gives me this error:

// Add text inside a float box
            Aspose.Pdf.FloatingBox floatNames = new Aspose.Pdf.FloatingBox(500, 500);
            floatNames.VerticalAlignment = VerticalAlignment.Top;
            floatNames.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

            string names = string.Concat(cmdstr_firstName + " " + cmdstr_lastName);        
         
            floatNames.Paragraphs.Add(names);

            resume.Pages[1].Paragraphs.Add(floatNames);

@jcartwvgov,

I think this is the code you are looking for:

private void Logic()
{
    Document doc = new Document();

    // Add text inside a float box
    Aspose.Pdf.FloatingBox floatNames = new Aspose.Pdf.FloatingBox(500, 500);
    floatNames.VerticalAlignment = VerticalAlignment.Top;
    floatNames.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

    string cmdstr_firstName = "FirstName";
    string cmdstr_lastName = "LastName";

    var names = new TextFragment($"{cmdstr_firstName} {cmdstr_lastName}");
    floatNames.Paragraphs.Add(names);

    var page = doc.Pages.Add();
    page.Paragraphs.Add(floatNames);

    doc.Save($"{PartialPath}_output.pdf");
}

Keep in mind you can get an exception when accessing the document.Pages object and you point to a page that does not exist. In your original case, you never created a page and you were using the index[1]. That was throwing an exception as well.

1 Like

Thank you for the help. The Page object declaration was among the code I omitted for privacy. Must have not noticed that it wasn’t included.

When I replace “FirstName” and “LastName” in the code you have written with the variables where the SQL query result is stored, the PDF generated is blank. Is this an issue with variable storage, or is it something with Aspose.PDF that I need to resolve?

@jcartwvgov,

It shouldn’t.
You have to remember that String is an immutable object.
In this case:

string one = "From DB";
string two = one;
one = null;

string two is never set to null, even though string one is set to null.
So there must be something wrong with your code.
The value assigned to a string is set the minute is created, it does not matter where did fetch it from.

So check that you are fetching the data from the DB correctly. If you are, then the string should display correctly.

You can also check the textFragment.Text property to see if the string value is the correct one.

1 Like