We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to convert a listview (from mysql database) to pdf?

Is there like a tutorial or video?
I read the getting started.  I saw options for for text, images, and table, but I'm not sure how to move forward on it.



14 Replies

SL Sowmiya Loganathan Syncfusion Team August 9, 2019 01:29 PM UTC

Hi Sheryl,  
 
At present it is not possible to Export ListView to PDF with templates. However we can export raw collection of data to PDF in table format using PDFGrid . Please find the sample for the same from below, 
 
 
Please try the above sample in your end and let us know if it satisfies your requirement. 
 
Regards, 
Sowmiya L 
 



SH Sheryl August 11, 2019 11:09 PM UTC

I looked at the sample it's working fine but I want to know how to use the syncfusion pdfgrid for my project.  Is there a better way to understand it.  I saw sflistview, but I already am using a listview from Xamarin.  I think I need to put the data in a pdf table or pdfgrid.  Where to start?


SL Sowmiya Loganathan Syncfusion Team August 13, 2019 01:03 PM UTC

Hi Sherly, 

We can create table on PDF using PdfGrid and we can enter the data manually or from an external data source to PdfGrid. Please refer the below links which will be helpful for you to understand about the  PdfGrid, 

Please let us know if you need any further assistance on this. 

Regards, 
Sowmiya L 



SH Sheryl August 18, 2019 07:15 PM UTC

KB: https://www.syncfusion.com/kb/9141/how-to-create-table-in-a-pdf-file-from-datatable 
 this one doesn't work.... pdf is empty when i run it.
 void filebuttonHandle_Clicked(object sender, EventArgs e)
        {
            
            //Create a new PDF document.
            PdfDocument doc = new PdfDocument();

            //Add a page.
            PdfPage page = doc.Pages.Add();

            //Create a PdfGrid.
            PdfGrid pdfGrid = new PdfGrid();

            //Create a DataTable
            DataTable dataTable = new DataTable();

            //Add columns to the DataTable
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Name");

            //Add rows to the DataTable
            dataTable.Rows.Add(new object[] { "E01", "Clay" });
            dataTable.Rows.Add(new object[] { "E02", "Thomas" });
            dataTable.Rows.Add(new object[] { "E03", "George" });
            dataTable.Rows.Add(new object[] { "E04", "Stefan" });
            dataTable.Rows.Add(new object[] { "E05", "Mathew" });

            //Assign data source
            pdfGrid.DataSource = dataTable;

            //Draw grid to the page of PDF document
            pdfGrid.Draw(page, new PointF(10, 10));

            //Save the PDF document to stream.
            MemoryStream stream = new MemoryStream();
            doc.Save(stream);
            //Close the document.
            doc.Close(true);
            stream.Position = 0;
            //Save the stream as a file in the device and invoke it for viewing
            Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
            



SH Sheryl August 19, 2019 03:04 AM UTC

Could you explain this part from one of the links you sent? I changed the string, I was trying to understand it.
/Add values to list
            List<object> data = new List<object>();
            Object row1 = new { Date = "RealDate", LabelfromPost = "Clay"};
            Object row2 = new { Date = "E02", LabelfromPost = "Thomas" };
            Object row3 = new { Date = "E03", LabelfromPost = "Andrew" };
            Object row4 = new { Date = "E04", LabelfromPost = "Paul" };
            Object row5 = new { Date = "E05", LabelfromPost = "Gray" };
            data.Add(row1);
            data.Add(row2);
            data.Add(row3);
            data.Add(row4);
            data.Add(row5);
            //Add list to IEnumerable
            IEnumerable<object> dataTable = data;

            
            //Assign data source.
            pdfGrid.DataSource = dataTable;


on this statement: how add more columns, just comma?  Object row1 = new { Date = "RealDate", LabelfromPost = "Clay", Value="here", Place="location"};  ???
 List<object> data = new List<object>();
            Object row1 = new { Date = "RealDate", LabelfromPost = "Clay"};   


SK Surya Kumar Syncfusion Team August 19, 2019 01:27 PM UTC

Hi Sheryl, 

Please note that the number of elements in the object collection refers to the number of columns in the Grid. 
Object row1 = new { Date = "RealDate", LabelfromPost = "Clay"}; 
Number of columns = 2 
The number of object created denotes the number of rows in the grid. 
Object row1 = new { Date = "RealDate", LabelfromPost = "Clay"}; 
Object row2 = new { Date = "E02", LabelfromPost = "Thomas" }; 
Object row3 = new { Date = "E03", LabelfromPost = "Andrew" }; 
Object row4 = new { Date = "E04", LabelfromPost = "Paul" }; 
Object row5 = new { Date = "E05", LabelfromPost = "Gray" }; 
Number of rows = 5 


Let us know if you need any further information. 

Regards, 
Surya Kumar 



SH Sheryl August 20, 2019 05:09 AM UTC

How do I get the string entered by the user, save in UserSettings?  It says I'm not supposed to use it like that "Settings.location".  The datas entered are coming from mysql databases.
Object row1 = new { Date = "RealDate", Settings.location = "Clay"}; 




SK Surya Kumar Syncfusion Team August 21, 2019 02:11 PM UTC

Hi Sheryl, 

We can only use one object property name while creating the object collection, So you need to try modifying the property name from data in mysql according to this usage. 

Regards, 
Surya Kumar 



SH Sheryl August 22, 2019 12:05 AM UTC

Yes I understand 1 object... what i'm asking is, is it possible for the Date not to be hard coded string but a variable.  For example Data1=Settings.raindata1; to hold a value of string.  How can i pass data thru it.  you know what I mean?

Object row1 = new { Data1 = "RealDate", LabelfromPost = "Clay"}; 
Object row2 = new { Data1 = "E02", LabelfromPost = "Thomas" }; 





SL Sowmiya Loganathan Syncfusion Team August 22, 2019 12:07 PM UTC

Hi Sheryl, 

We can assign a data as a variable (holds a value of string) while creating object collection. Please refer the below code snippet for more details, 

void DrawTable() 
{ 
//List of Columns 
List<Customer> collection = new List<Customer>(); 
 
Customer customer = new Customer(); 
customer.Date = DateTime.Now.ToString(); 
customer.LabelfromPost = "Thomas"; 
collection.Add(customer); 
 
//Add values to list 
List<object> data = new List<object>(); 
for (int i = 0; i < collection.Count; i++) 
{ 
    Object row = new { Date = collection[i].Date, LabelfromPost = collection[i].LabelfromPost }; 
    data.Add(row); 
} 
//Add list to IEnumerable 
IEnumerable<object> tableData = data; 
//Assign data source 
pdfGrid.DataSource = tableData;} 
 
class Customer 
{ 
    private string m_date; 
    private string m_label; 
    public string Date 
    { 
        get 
        { 
            return m_date; 
        } 
        set 
        { 
            m_date = value; 
        } 
    } 
    public string LabelfromPost 
    { 
        get 
        { 
            return m_label; 
        } 
        set 
        { 
            m_label = value; 
        } 
    }   
} 


Please find the sample for the same from below link, 


Please try the above solution in your end and let us know if it satisfies your requirement.  

Regards, 
Sowmiya L 




SH Sheryl August 26, 2019 10:03 PM UTC

this works but If i omit the for loop, the whole list on the database shows up on pdf even ID, how do i select or reference certain data i want to print?

        void filebuttonHandle_Clicked(object sender, EventArgs e)
        {
            //Create a new PDF document.
            PdfDocument doc = new PdfDocument();
            //Add a page.
            PdfPage page = doc.Pages.Add();
            //Create a PdfGrid.
            PdfGrid pdfGrid = new PdfGrid();
            //Add list to IEnumerable 
            IEnumerable<Post> tableData = posts;
            //Assign data source 
            pdfGrid.DataSource = tableData;
            //Draw grid to the page of PDF document.
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(10, 10));
            //Save the PDF document to stream.
            MemoryStream stream = new MemoryStream();
            doc.Save(stream);
            //Close the document.
            doc.Close(true);
            stream.Position = 0;
            //Save the stream as a file in the device and invoke it for viewing
            Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
        }

************************************* how to reference data from my mysql called post in the foreach loop?
     //Add values to list foreach (var p in posts)             
   {
///*******this line is not working for me syntax...
                Post row = new {Date = p.CDateTime, p.drain1Lbl = p.drain1vol };
                   Post.Add(row);
//***********
                 }

    public partial class HistoryLogPage : ContentPage
    {
        List<Post> posts;    
  public HistoryLogPage()
        {
InitializeComponent();
          }

void filebuttonHandle_Clicked(object sender, EventArgs e)
        {
            //Create a new PDF document.
            PdfDocument doc = new PdfDocument();
            //Add a page.
            PdfPage page = doc.Pages.Add();
            //Create a PdfGrid.
            PdfGrid pdfGrid = new PdfGrid();

//this line is not working for me syntax...

        //Add values to list                  

      foreach (var p in posts)

                {

                Post row = new {Date = p.CDateTime, p.drain1Lbl = p.drain1vol };
                   Post.Add(row);

                 }

            //Add list to IEnumerable 
            IEnumerable<Post> tableData = posts;
            //Assign data source 
            pdfGrid.DataSource = tableData;
            //Draw grid to the page of PDF document.
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(10, 10));
            //Save the PDF document to stream.
            MemoryStream stream = new MemoryStream();
            doc.Save(stream);
            //Close the document.
            doc.Close(true);
            stream.Position = 0;
            //Save the stream as a file in the device and invoke it for viewing
            Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
}
}




SL Sowmiya Loganathan Syncfusion Team August 27, 2019 01:41 PM UTC

Hi Sheryl, 

How do I select or reference certain data I want to print? 
We can select or refer particular row of data from table (with SQLiteConnection), which wants to print in PDF by using the below query. 

var row = conn.Table<Post>().FirstOrDefault(x => x.rain1vol == "CellValue"); 

But while adding values to list, we can only select the certain data using for loop.  
how to reference data from my mysql called post in the foreach loop? 
//this line is not working for me syntax... 
//Add values to list                   
 foreach (var p in posts) 
   { 
                Post row = new {Date = p.CDateTime, p.drain1Lbl = p.drain1vol }; 
                  Post.Add(row); 
 
    } 
Please add values to the object of the Post class variables before performing the loop. Please refer the below code snippet for your reference, 


//List of Columns  
List<Post> collection = new List<Post>(); 
Post post = new Post(); 
post.CDateTime = value; 
post.rain1vol = value; 
 
collection.Add(post); 
 
//Add values to list  
List<object> data = new List<object>(); 
for (int i = 0; i < collection.Count; i++) 
{ 
    Object row = new { Date = collection[i].CDateTime, Experiences = collection[i].rain1vol }; 
    data.Add(row); 
} 
//Add list to IEnumerable  
IEnumerable<object> tableData = data; 
 
//Assign data source  
pdfGrid.DataSource = tableData; 

Or else you can get the whole data source from SQL by using the below query, 

string dbPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Database.db"); 
 
//Initiate SQLite connection 
SQLiteConnection connection = new SQLiteConnection(dbPath); 
 
//Assign data source  
pdfGrid.DataSource = connection.Table<Post>().OrderBy(x => x.CDateTime).ToList(); 

You can also get list of data using below code, 
var posts = connection.Table<Post>().ToList(); 

Please try the above solution in your end and let us know if it solves the issue.  


Regards, 
Sowmiya L 



SH Sheryl August 28, 2019 05:30 AM UTC

//Assign data source  
pdfGrid.DataSource = connection.Table().OrderBy(x => post.CDateTime).ToList();

.SQLIteException has been thrown - no such table: PostI like to use this query to achieve what i needed to do but it's throwing an exception
 public partial class HistoryLogPage : ContentPage
    {
         List posts;

        public HistoryLogPage()
        {

            InitializeComponent();
}

protected override void OnAppearing()
        {
            base.OnAppearing();
            SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
            {
             
                conn.CreateTable();
                posts = conn.Table().ToList();
                postListView.ItemsSource = posts;
                conn.Close();
            }
        }

void filebuttonHandle_Clicked(object sender, EventArgs e)
        {

            //Create a new PDF document.
            PdfDocument doc = new PdfDocument();

            //Add a page.
            PdfPage page = doc.Pages.Add();

            //Create a PdfGrid.
            PdfGrid pdfGrid = new PdfGrid();

            //Assign data source 
            string dbPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Database.db");

            //Initiate SQLite connection 
            SQLiteConnection connection = new SQLiteConnection(dbPath);

            //Assign data source  
            pdfGrid.DataSource = connection.Table().OrderBy(x => x.CDateTime).ToList();

            //Draw grid to the page of PDF document.
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(10, 10));
            //Save the PDF document to stream.
            MemoryStream stream = new MemoryStream();
            doc.Save(stream);
            //Close the document.
            doc.Close(true);
            stream.Position = 0;
            //Save the stream as a file in the device and invoke it for viewing
            Xamarin.Forms.DependencyService.Get().SaveAndView("Output.pdf", "application/pdf", stream);

        }
    }


SL Sowmiya Loganathan Syncfusion Team August 28, 2019 11:11 AM UTC

Hi Sherly,  

We have tried the sample with the provided code snippet end and we were unable to reproduce the mentioned issue. Please find the sample from below link which has been tried to reproduce the issue in our end, 


Please try the above sample in your end and let us know if it solves the issue.  

Regards, 
Sowmiya L 


Loader.
Live Chat Icon For mobile
Up arrow icon