Home > Net >  Create a pdf file with the content of a list in C#
Create a pdf file with the content of a list in C#

Time:06-28

I am trying to create a pdf file with the content of a list.

I created the list this way:

            int arrayLength = obj.records.Count;
            List<string> outPutList = new List<string>();
            

            for (int i = 0; i<arrayLength; i  )
            {
                str = "";
                str = obj.records[i].datetime   "         "   obj.records[i].userName   "              "   obj.records[i].inTime   "               "   obj.records[i].outTime;
                outPutList.Add(str);
                Console.WriteLine(str);

I found on internet some sample code in C# that normally allows me to create a pdf with the content I want:

            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);
            float y = 30;

            PdfBrush brush1 = PdfBrushes.Black;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new System.Drawing.Font("Impact", 18f, FontStyle.Regular), true);
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("Rapport", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
            
            y = y   font1.MeasureString("Rapport", format1).Height;
            
            y = y   5;

            
            PdfList list = new PdfList(str);
            
            PdfLayoutResult result = list.Draw(page, 1, y);
            
            y = result.Bounds.Bottom;

            doc.SaveToFile("List.pdf"); 
            doc.Close();
            System.Diagnostics.Process.Start("List.pdf");

It seems that is working, but the pdf only has the last line of the list I created and not the entire content.

Do you know what the problem might be? Why am I getting only the last line and not the whole content of the list?

Thank you for the help.

CodePudding user response:

As I mentioned in the comments, you overwrite the value of the str variable. What you want to do is the following: (I added an extra variable for you to understand the difference between the = and the = operators):

int arrayLength = obj.records.Count;
List<string> outPutList = new List<string>();

// Add new variable as empty string
string content = "";

for (int i = 0; i<arrayLength; i  )
{
    // give str new value
    str = "";
    // give str new value again
    str = obj.records[i].datetime   "         "   obj.records[i].userName   "              "   obj.records[i].inTime   "               "   obj.records[i].outTime;
    outPutList.Add(str);
    Console.WriteLine(str);
    
    // append str to content variable with old en new value
    content  = str;
        

Then you pass the content variable within the initialization of the PdfList instance:

// Pass content variable to PdfList
PdfList list = new PdfList(content);

For a more detailed explanation about this operators, please read https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator

I hope this helps you :)

  • Related