Saltar al pie de página
.NET AYUDA

C# ObservableCollection (Cómo Funciona para Desarrolladores)

En C#, el ObservableCollection es una poderosa estructura de datos que notifica automáticamente a los oyentes cuando se agregan, eliminan o cambian elementos. Es particularmente útil para escenarios de colección de datos dinámicos, como cuando necesita reflejar cambios en tiempo real en su interfaz de usuario o informes. Cuando se combina con IronPDF, esta poderosa colección se puede usar para generar PDF dinámicos basados en datos en vivo.

IronPDF es una robusta biblioteca para generar PDF en C#. Con sus capacidades de conversión de HTML a PDF, es fácil crear PDF de alta calidad, ya sea que necesites generar facturas, informes o cualquier otro documento basado en datos en tiempo real. En este artículo, te mostraremos cómo integrar la clase ObservableCollection con IronPDF, aprovechando la vinculación de datos para generar un PDF que se actualiza dinámicamente a medida que cambian los datos.

Entendiendo ObservableCollection

¿Qué es una colección observable?

ObservableCollection es una clase en C# que implementa la interfaz INotifyCollectionChanged, que proporciona notificaciones cuando se añaden, eliminan o modifican elementos. Se usa comúnmente para la vinculación de datos en aplicaciones de interfaz de usuario como WPF, donde los cambios en la colección automáticamente generan actualizaciones en la interfaz de usuario. A diferencia de otras colecciones como List, una ObservableCollection ofrece notificaciones de eventos incorporadas, lo que la hace perfecta para escenarios donde los datos necesitan reflejarse en tiempo real.

La ObservableCollection maneja automáticamente los cambios en toda la colección, facilitando la gestión y visualización de colecciones de datos dinámicos en su aplicación.

Casos de uso comunes

  • Enlace con componentes de UI: en WPF, por ejemplo, ObservableCollection se utiliza a menudo para vincular datos a controles como ListView, DataGrid y ComboBox. Cuando la colección subyacente cambia, la interfaz de usuario se actualiza automáticamente.
  • Actualizaciones en tiempo real: cuando los datos cambian con frecuencia, ObservableCollection garantiza que la interfaz de usuario esté siempre sincronizada. Por ejemplo, podría usarlo para un informe en vivo donde se agregan nuevas entradas de datos a la colección en tiempo real y el informe se actualiza en consecuencia.
  • Cambios dinámicos: si su aplicación permite a los usuarios agregar, eliminar o modificar datos, se puede utilizar ObservableCollection para reflejar automáticamente esos cambios en la interfaz de usuario u otros componentes.

Trabajar con IronPDF

Descripción general de IronPDF

C# ObservableCollection (Cómo funciona para desarrolladores): Figura 1

Como mencionamos al inicio de este artículo, IronPDF es una biblioteca de generación de PDF en .NET que facilita la creación, modificación y renderizado de documentos PDF. A diferencia de algunas otras bibliotecas de PDF, IronPDF proporciona un conjunto rico de características que simplifican el trabajo con PDF, como convertir HTML a PDF, agregar marcas de agua, manipular PDF existentes y más.

IronPDF también permite a los desarrolladores renderizar PDF desde diferentes fuentes de datos, incluidos HTML, imágenes y texto plano, lo cual puede ser particularmente útil cuando necesita presentar datos dinámicos. Con la API de IronPDF, puede generar PDF de alta calidad, incluyendo diseños detallados, tablas, imágenes y estilos.

Instalación de IronPDF

Para comenzar con IronPDF, necesita instalar la biblioteca a través de NuGet. Puede agregarlo a su proyecto ejecutando el siguiente comando en la Consola del Administrador de Paquetes:

Install-Package IronPdf

Una vez instalado, puede inicializar IronPDF y usar sus potentes características. Para este artículo, nos centraremos en generar PDF a partir de datos dinámicos en una ObservableCollection.

Integración de ObservableCollection con IronPDF

Caso de uso: Generación dinámica de PDFs a partir de ObservableCollection

Imaginemos un escenario donde necesita generar un PDF de factura a partir de una lista de elementos almacenados en un ObservableCollection. A medida que se agregan o eliminan elementos de la colección, el PDF debería regenerarse automáticamente para reflejar el estado actual de los datos.

Para esta tarea, ObservableCollection proporciona una manera fácil de gestionar los elementos en la factura, mientras que IronPDF nos permitirá generar y exportar la factura a formato PDF.

Enlace de datos a contenido PDF

Para vincular los datos en una ObservableCollection a un PDF, necesita recorrer la colección y agregar sus elementos al contenido del PDF. IronPDF proporciona métodos para crear tablas, agregar texto y personalizar el diseño, lo que facilita representar los datos en un formato estructurado.

Por ejemplo, si tiene una factura con varios artículos, puede generar el PDF dinámicamente recorriendo el ObservableCollection y agregando cada artículo a una tabla o lista en el documento. IronPDF permite un alto nivel de personalización, incluyendo el ajuste de fuentes, bordes e incluso la inclusión de imágenes como logotipos o códigos de barras.

Ejemplo uno: Generación de un PDF utilizando ObservableCollection

Veamos un ejemplo simple para demostrar cómo funciona esto. Supongamos que tiene una colección de personas (representadas por la clase Persona) y desea generar un PDF que refleje los detalles de estas personas. Cada vez que se añade una nueva persona a la colección, el PDF se actualizará automáticamente.

Ejemplo de clase de persona

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
Public Class Person
	Public Property Name() As String
	Public Property Age() As Integer
End Class
$vbLabelText   $csharpLabel

En este caso, la clase Persona contiene propiedades básicas como Nombre y Edad. Usaremos esta clase junto con el ObservableCollection para generar un PDF dinámicamente.

Creación de ObservableCollection

using System.Collections.ObjectModel;

var collection = new ObservableCollection<Person>
{
    new Person { Name = "John Doe", Age = 30 },
    new Person { Name = "Jane Smith", Age = 28 }
};
using System.Collections.ObjectModel;

var collection = new ObservableCollection<Person>
{
    new Person { Name = "John Doe", Age = 30 },
    new Person { Name = "Jane Smith", Age = 28 }
};
Imports System.Collections.ObjectModel

Private collection = New ObservableCollection(Of Person) From {
	New Person With {
		.Name = "John Doe",
		.Age = 30
	},
	New Person With {
		.Name = "Jane Smith",
		.Age = 28
	}
}
$vbLabelText   $csharpLabel

Esta ObservableCollection contiene una colección de objetos Persona, cada uno con un Nombre y una Edad. Cuando un elemento es agregado o eliminado, se activan manejadores de eventos, que utilizaremos para actualizar el PDF dinámicamente.

Añadir controladores de eventos para cambios de colección

En este ejemplo, nos suscribimos al evento CollectionChanged de la clase ObservableCollection para regenerar automáticamente el PDF siempre que la colección cambie. Esto es útil si necesita responder a cambios como añadir o eliminar un objeto Persona.

// Subscribe to the ObservableCollection's CollectionChanged event
collection.CollectionChanged += (object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) =>
{
    // Regenerate the PDF whenever the collection changes
    GeneratePersonPDF(collection);
};
// Subscribe to the ObservableCollection's CollectionChanged event
collection.CollectionChanged += (object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) =>
{
    // Regenerate the PDF whenever the collection changes
    GeneratePersonPDF(collection);
};
' Subscribe to the ObservableCollection's CollectionChanged event
AddHandler collection.CollectionChanged, Sub(sender As Object, e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
	' Regenerate the PDF whenever the collection changes
	GeneratePersonPDF(collection)
End Sub
$vbLabelText   $csharpLabel

Añadir gente nueva a la colección

Puede agregar una nueva Persona a la colección y el manejador de eventos activará la regeneración del PDF. Este enfoque maneja automáticamente los cambios en la lista completa de personas.

// Adding a new person to the collection
collection.Add(new Person { Name = "Alice Brown", Age = 32 });
// Adding a new person to the collection
collection.Add(new Person { Name = "Alice Brown", Age = 32 });
' Adding a new person to the collection
collection.Add(New Person With {
	.Name = "Alice Brown",
	.Age = 32
})
$vbLabelText   $csharpLabel

Cada vez que añades una nueva persona a la colección, se regenerará el PDF, incluyendo la nueva entrada.

Propiedad de edad vinculante

También puede vincular la propiedad edad de una Persona a algún sistema externo (como la interfaz de usuario u otras partes de la aplicación) para reflejar automáticamente los cambios.

A continuación se muestra un ejemplo de cómo se vincularía la propiedad Edad en la clase Persona:

public class Person
{
    public string Name { get; set; }
    private int _age;
    public int Age 
    {
        get { return _age; }
        set
        {
            _age = value;
            // Raise property changed event here if using data binding
        }
    }
}
public class Person
{
    public string Name { get; set; }
    private int _age;
    public int Age 
    {
        get { return _age; }
        set
        {
            _age = value;
            // Raise property changed event here if using data binding
        }
    }
}
Public Class Person
	Public Property Name() As String
	Private _age As Integer
	Public Property Age() As Integer
		Get
			Return _age
		End Get
		Set(ByVal value As Integer)
			_age = value
			' Raise property changed event here if using data binding
		End Set
	End Property
End Class
$vbLabelText   $csharpLabel

Uso de ObservableCollection con Binding

Demostremos el uso de vinculación de edad a un elemento de la interfaz de usuario, actualizando el valor de la edad y observando cómo se reflejan los cambios en la colección:

ObservableCollection<Person> people = new ObservableCollection<Person>
{
    new Person { Name = "John", Age = 30 },
    new Person { Name = "Jane", Age = 28 }
};
// Binding the Age of the first person to a UI element (pseudo-code)
someUIElement.Text = people[0].Age.ToString();
ObservableCollection<Person> people = new ObservableCollection<Person>
{
    new Person { Name = "John", Age = 30 },
    new Person { Name = "Jane", Age = 28 }
};
// Binding the Age of the first person to a UI element (pseudo-code)
someUIElement.Text = people[0].Age.ToString();
Dim people As New ObservableCollection(Of Person) From {
	New Person With {
		.Name = "John",
		.Age = 30
	},
	New Person With {
		.Name = "Jane",
		.Age = 28
	}
}
' Binding the Age of the first person to a UI element (pseudo-code)
someUIElement.Text = people(0).Age.ToString()
$vbLabelText   $csharpLabel

Generación del PDF con IronPDF

Ahora que hemos configurado el ObservableCollection y añadido manejadores de eventos, es tiempo de enfocarnos en generar un PDF que refleje la colección dinámica de personas.

using IronPdf;

public void GeneratePersonPDF(ObservableCollection<Person> people)
{
    var pdf = new ChromePdfRenderer();  // Initialize IronPDF's ChromePdfRenderer class
    // Create HTML content representing the people in the collection
    var htmlContent = "<h1>People Information</h1><table border='1' cellpadding='5' cellspacing='0'>" +
                      "<tr><th>Name</th><th>Age</th></tr>";
    foreach (var person in people)
    {
        htmlContent += $"<tr><td>{person.Name}</td><td>{person.Age}</td></tr>";
    }

    htmlContent += "</table>";
    // Convert the HTML content to a PDF
    var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);
    // Save the generated PDF to disk
    pdfDocument.SaveAs("PeopleInformation.pdf");
}
using IronPdf;

public void GeneratePersonPDF(ObservableCollection<Person> people)
{
    var pdf = new ChromePdfRenderer();  // Initialize IronPDF's ChromePdfRenderer class
    // Create HTML content representing the people in the collection
    var htmlContent = "<h1>People Information</h1><table border='1' cellpadding='5' cellspacing='0'>" +
                      "<tr><th>Name</th><th>Age</th></tr>";
    foreach (var person in people)
    {
        htmlContent += $"<tr><td>{person.Name}</td><td>{person.Age}</td></tr>";
    }

    htmlContent += "</table>";
    // Convert the HTML content to a PDF
    var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);
    // Save the generated PDF to disk
    pdfDocument.SaveAs("PeopleInformation.pdf");
}
Imports IronPdf

Public Sub GeneratePersonPDF(ByVal people As ObservableCollection(Of Person))
	Dim pdf = New ChromePdfRenderer() ' Initialize IronPDF's ChromePdfRenderer class
	' Create HTML content representing the people in the collection
	Dim htmlContent = "<h1>People Information</h1><table border='1' cellpadding='5' cellspacing='0'>" & "<tr><th>Name</th><th>Age</th></tr>"
	For Each person In people
		htmlContent &= $"<tr><td>{person.Name}</td><td>{person.Age}</td></tr>"
	Next person

	htmlContent &= "</table>"
	' Convert the HTML content to a PDF
	Dim pdfDocument = pdf.RenderHtmlAsPdf(htmlContent)
	' Save the generated PDF to disk
	pdfDocument.SaveAs("PeopleInformation.pdf")
End Sub
$vbLabelText   $csharpLabel

Ejemplo dos: generación de una factura PDF a partir de ObservableCollection

Aquí hay un ejemplo de cómo generar un PDF desde un ObservableCollection de artículos de factura usando IronPDF:

Paso 1: Ejemplo de clase de artículo de factura

Esta clase representa un artículo en la factura, con propiedades para el nombre del artículo, cantidad, precio y precio total.

public class InvoiceItem
{
    public string ItemName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal Total => Quantity * Price;
}
public class InvoiceItem
{
    public string ItemName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public decimal Total => Quantity * Price;
}
Public Class InvoiceItem
	Public Property ItemName() As String
	Public Property Quantity() As Integer
	Public Property Price() As Decimal
	Public ReadOnly Property Total() As Decimal
		Get
			Return Quantity * Price
		End Get
	End Property
End Class
$vbLabelText   $csharpLabel

Paso 2: Ejemplo de ObservableCollection

Este ejemplo inicializa una ObservableCollection que contiene varios artículos de factura. Puedes agregar, eliminar o modificar dinámicamente los artículos en esta colección.

ObservableCollection<InvoiceItem> invoiceItems = new ObservableCollection<InvoiceItem>
{
    new InvoiceItem { ItemName = "Item 1", Quantity = 2, Price = 10.00m },
    new InvoiceItem { ItemName = "Item 2", Quantity = 1, Price = 25.00m },
    new InvoiceItem { ItemName = "Item 3", Quantity = 5, Price = 5.00m }
};
ObservableCollection<InvoiceItem> invoiceItems = new ObservableCollection<InvoiceItem>
{
    new InvoiceItem { ItemName = "Item 1", Quantity = 2, Price = 10.00m },
    new InvoiceItem { ItemName = "Item 2", Quantity = 1, Price = 25.00m },
    new InvoiceItem { ItemName = "Item 3", Quantity = 5, Price = 5.00m }
};
Dim invoiceItems As New ObservableCollection(Of InvoiceItem) From {
	New InvoiceItem With {
		.ItemName = "Item 1",
		.Quantity = 2,
		.Price = 10.00D
	},
	New InvoiceItem With {
		.ItemName = "Item 2",
		.Quantity = 1,
		.Price = 25.00D
	},
	New InvoiceItem With {
		.ItemName = "Item 3",
		.Quantity = 5,
		.Price = 5.00D
	}
}
$vbLabelText   $csharpLabel

Paso 3: Generar el PDF con IronPDF

Aquí, creamos una función para generar el PDF. Esta función utiliza los datos en el ObservableCollection y los convierte a HTML, que luego se renderiza como un PDF usando IronPDF.

public void GenerateInvoicePDF(ObservableCollection<InvoiceItem> items)
{
    var pdf = new ChromePdfRenderer();  // Initialize IronPDF's ChromePdfRenderer class
    // Create HTML content representing the invoice
    var htmlContent = "<h1>Invoice</h1><table border='1' cellpadding='5' cellspacing='0'>" +
                      "<tr><th>Item</th><th>Quantity</th><th>Price</th><th>Total</th></tr>";
    foreach (var item in items)
    {
        htmlContent += $"<tr><td>{item.ItemName}</td><td>{item.Quantity}</td><td>{item.Price:C}</td><td>{item.Total:C}</td></tr>";
    }
    htmlContent += "</table>";
    // Convert the HTML content to a PDF
    var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);
    // Save the generated PDF to disk
    pdfDocument.SaveAs("Invoice.pdf");
}
public void GenerateInvoicePDF(ObservableCollection<InvoiceItem> items)
{
    var pdf = new ChromePdfRenderer();  // Initialize IronPDF's ChromePdfRenderer class
    // Create HTML content representing the invoice
    var htmlContent = "<h1>Invoice</h1><table border='1' cellpadding='5' cellspacing='0'>" +
                      "<tr><th>Item</th><th>Quantity</th><th>Price</th><th>Total</th></tr>";
    foreach (var item in items)
    {
        htmlContent += $"<tr><td>{item.ItemName}</td><td>{item.Quantity}</td><td>{item.Price:C}</td><td>{item.Total:C}</td></tr>";
    }
    htmlContent += "</table>";
    // Convert the HTML content to a PDF
    var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);
    // Save the generated PDF to disk
    pdfDocument.SaveAs("Invoice.pdf");
}
Public Sub GenerateInvoicePDF(ByVal items As ObservableCollection(Of InvoiceItem))
	Dim pdf = New ChromePdfRenderer() ' Initialize IronPDF's ChromePdfRenderer class
	' Create HTML content representing the invoice
	Dim htmlContent = "<h1>Invoice</h1><table border='1' cellpadding='5' cellspacing='0'>" & "<tr><th>Item</th><th>Quantity</th><th>Price</th><th>Total</th></tr>"
	For Each item In items
		htmlContent &= $"<tr><td>{item.ItemName}</td><td>{item.Quantity}</td><td>{item.Price:C}</td><td>{item.Total:C}</td></tr>"
	Next item
	htmlContent &= "</table>"
	' Convert the HTML content to a PDF
	Dim pdfDocument = pdf.RenderHtmlAsPdf(htmlContent)
	' Save the generated PDF to disk
	pdfDocument.SaveAs("Invoice.pdf")
End Sub
$vbLabelText   $csharpLabel

Paso 4: Suscribirse al evento CollectionChanged

Debido a que ObservableCollection notifica automáticamente los cambios, puedes regenerar fácilmente el PDF siempre que la colección se actualice. Por ejemplo, si se agrega o elimina un artículo, el PDF se puede regenerar con los datos actualizados.

Aquí te mostramos cómo puedes suscribirte al evento CollectionChanged y regenerar el PDF siempre que la colección cambie:

// Subscribe to the ObservableCollection's CollectionChanged event
invoiceItems.CollectionChanged += (sender, e) =>
{
    // Regenerate the PDF whenever the collection changes
    GenerateInvoicePDF(invoiceItems);
};
// Subscribe to the ObservableCollection's CollectionChanged event
invoiceItems.CollectionChanged += (sender, e) =>
{
    // Regenerate the PDF whenever the collection changes
    GenerateInvoicePDF(invoiceItems);
};
' Subscribe to the ObservableCollection's CollectionChanged event
AddHandler invoiceItems.CollectionChanged, Sub(sender, e)
	' Regenerate the PDF whenever the collection changes
	GenerateInvoicePDF(invoiceItems)
End Sub
$vbLabelText   $csharpLabel

Paso 5: Añadir elementos y probar

Ahora puedes probar agregando nuevos artículos a la ObservableCollection y observando cómo se regenera automáticamente el PDF.

// Adding a new item to the ObservableCollection
invoiceItems.Add(new InvoiceItem { ItemName = "Item 4", Quantity = 3, Price = 12.50m });
// Adding a new item to the ObservableCollection
invoiceItems.Add(new InvoiceItem { ItemName = "Item 4", Quantity = 3, Price = 12.50m });
' Adding a new item to the ObservableCollection
invoiceItems.Add(New InvoiceItem With {
	.ItemName = "Item 4",
	.Quantity = 3,
	.Price = 12.50D
})
$vbLabelText   $csharpLabel

Ejemplo de código completo

using System;
using System.Collections.ObjectModel;
using IronPdf;

public class InvoiceItem
{
    public string ItemName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    // Property to calculate the total price for each item
    public decimal Total => Quantity * Price;
}
public class InvoiceGenerator
{
    // Function to generate the invoice PDF
    public void GenerateInvoicePDF(ObservableCollection<InvoiceItem> items)
    {
        var pdf = new ChromePdfRenderer();  // Initialize IronPDF's ChromePdfRenderer class
        // Create HTML content representing the invoice
        var htmlContent = "<h1>Invoice</h1><table border='1' cellpadding='5' cellspacing='0'>" +
                          "<tr><th>Item</th><th>Quantity</th><th>Price</th><th>Total</th></tr>";
        foreach (var item in items)
        {
            htmlContent += $"<tr><td>{item.ItemName}</td><td>{item.Quantity}</td><td>{item.Price:C}</td><td>{item.Total:C}</td></tr>";
        }
        htmlContent += "</table>";
        // Convert the HTML content to a PDF
        var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);
        // Save the generated PDF to disk
        pdfDocument.SaveAs("Invoice.pdf");
    }

    // Main function to test the code
    public static void Main(string[] args)
    {
        var invoiceItems = new ObservableCollection<InvoiceItem>
        {
            new InvoiceItem { ItemName = "Item 1", Quantity = 2, Price = 10.00m },
            new InvoiceItem { ItemName = "Item 2", Quantity = 1, Price = 25.00m },
            new InvoiceItem { ItemName = "Item 3", Quantity = 5, Price = 5.00m }
        };

        var invoiceGenerator = new InvoiceGenerator();

        // Subscribe to the ObservableCollection's CollectionChanged event
        invoiceItems.CollectionChanged += (sender, e) =>
        {
            // Regenerate the PDF whenever the collection changes
            invoiceGenerator.GenerateInvoicePDF(invoiceItems);
        };

        // Generate initial PDF
        invoiceGenerator.GenerateInvoicePDF(invoiceItems);

        // Add a new item to the collection and automatically regenerate the PDF
        invoiceItems.Add(new InvoiceItem { ItemName = "Item 4", Quantity = 3, Price = 12.50m });

        // Remove an item and see the PDF update
        invoiceItems.RemoveAt(0);
    }
}
using System;
using System.Collections.ObjectModel;
using IronPdf;

public class InvoiceItem
{
    public string ItemName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    // Property to calculate the total price for each item
    public decimal Total => Quantity * Price;
}
public class InvoiceGenerator
{
    // Function to generate the invoice PDF
    public void GenerateInvoicePDF(ObservableCollection<InvoiceItem> items)
    {
        var pdf = new ChromePdfRenderer();  // Initialize IronPDF's ChromePdfRenderer class
        // Create HTML content representing the invoice
        var htmlContent = "<h1>Invoice</h1><table border='1' cellpadding='5' cellspacing='0'>" +
                          "<tr><th>Item</th><th>Quantity</th><th>Price</th><th>Total</th></tr>";
        foreach (var item in items)
        {
            htmlContent += $"<tr><td>{item.ItemName}</td><td>{item.Quantity}</td><td>{item.Price:C}</td><td>{item.Total:C}</td></tr>";
        }
        htmlContent += "</table>";
        // Convert the HTML content to a PDF
        var pdfDocument = pdf.RenderHtmlAsPdf(htmlContent);
        // Save the generated PDF to disk
        pdfDocument.SaveAs("Invoice.pdf");
    }

    // Main function to test the code
    public static void Main(string[] args)
    {
        var invoiceItems = new ObservableCollection<InvoiceItem>
        {
            new InvoiceItem { ItemName = "Item 1", Quantity = 2, Price = 10.00m },
            new InvoiceItem { ItemName = "Item 2", Quantity = 1, Price = 25.00m },
            new InvoiceItem { ItemName = "Item 3", Quantity = 5, Price = 5.00m }
        };

        var invoiceGenerator = new InvoiceGenerator();

        // Subscribe to the ObservableCollection's CollectionChanged event
        invoiceItems.CollectionChanged += (sender, e) =>
        {
            // Regenerate the PDF whenever the collection changes
            invoiceGenerator.GenerateInvoicePDF(invoiceItems);
        };

        // Generate initial PDF
        invoiceGenerator.GenerateInvoicePDF(invoiceItems);

        // Add a new item to the collection and automatically regenerate the PDF
        invoiceItems.Add(new InvoiceItem { ItemName = "Item 4", Quantity = 3, Price = 12.50m });

        // Remove an item and see the PDF update
        invoiceItems.RemoveAt(0);
    }
}
Imports System
Imports System.Collections.ObjectModel
Imports IronPdf

Public Class InvoiceItem
	Public Property ItemName() As String
	Public Property Quantity() As Integer
	Public Property Price() As Decimal
	' Property to calculate the total price for each item
	Public ReadOnly Property Total() As Decimal
		Get
			Return Quantity * Price
		End Get
	End Property
End Class
Public Class InvoiceGenerator
	' Function to generate the invoice PDF
	Public Sub GenerateInvoicePDF(ByVal items As ObservableCollection(Of InvoiceItem))
		Dim pdf = New ChromePdfRenderer() ' Initialize IronPDF's ChromePdfRenderer class
		' Create HTML content representing the invoice
		Dim htmlContent = "<h1>Invoice</h1><table border='1' cellpadding='5' cellspacing='0'>" & "<tr><th>Item</th><th>Quantity</th><th>Price</th><th>Total</th></tr>"
		For Each item In items
			htmlContent &= $"<tr><td>{item.ItemName}</td><td>{item.Quantity}</td><td>{item.Price:C}</td><td>{item.Total:C}</td></tr>"
		Next item
		htmlContent &= "</table>"
		' Convert the HTML content to a PDF
		Dim pdfDocument = pdf.RenderHtmlAsPdf(htmlContent)
		' Save the generated PDF to disk
		pdfDocument.SaveAs("Invoice.pdf")
	End Sub

	' Main function to test the code
	Public Shared Sub Main(ByVal args() As String)
		Dim invoiceItems = New ObservableCollection(Of InvoiceItem) From {
			New InvoiceItem With {
				.ItemName = "Item 1",
				.Quantity = 2,
				.Price = 10.00D
			},
			New InvoiceItem With {
				.ItemName = "Item 2",
				.Quantity = 1,
				.Price = 25.00D
			},
			New InvoiceItem With {
				.ItemName = "Item 3",
				.Quantity = 5,
				.Price = 5.00D
			}
		}

'INSTANT VB NOTE: The variable invoiceGenerator was renamed since it may cause conflicts with calls to static members of the user-defined type with this name:
		Dim invoiceGenerator_Conflict As New InvoiceGenerator()

		' Subscribe to the ObservableCollection's CollectionChanged event
		AddHandler invoiceItems.CollectionChanged, Sub(sender, e)
			' Regenerate the PDF whenever the collection changes
			invoiceGenerator_Conflict.GenerateInvoicePDF(invoiceItems)
		End Sub

		' Generate initial PDF
		invoiceGenerator_Conflict.GenerateInvoicePDF(invoiceItems)

		' Add a new item to the collection and automatically regenerate the PDF
		invoiceItems.Add(New InvoiceItem With {
			.ItemName = "Item 4",
			.Quantity = 3,
			.Price = 12.50D
		})

		' Remove an item and see the PDF update
		invoiceItems.RemoveAt(0)
	End Sub
End Class
$vbLabelText   $csharpLabel

Salida

C# ObservableCollection (Cómo funciona para desarrolladores): Figura 2 - Archivo PDF de salida

Qué hace el código

  • Clase InvoiceItem: representa un elemento de factura con propiedades para ItemName, Cantidad, Precio y un Total calculado.
  • ObservableCollection: se utiliza para almacenar la lista de elementos de la factura. La colección notifica automáticamente a los oyentes de cualquier cambio (por ejemplo, cuando se agregan o se eliminan artículos).
  • GenerateInvoicePDF: Este método crea el contenido HTML que representa la factura y utiliza IronPDF para convertirlo a PDF.
  • CollectionChanged: el evento ObservableCollection se maneja para regenerar el PDF cada vez que la colección cambia, lo que hace que la generación del PDF sea dinámica.
  • Prueba: El método principal demuestra cómo agregar y eliminar elementos de ObservableCollection desencadena la regeneración de PDF.

Consideraciones sobre el rendimiento

Gestión de grandes colecciones

Cuando se trabaja con instancias grandes de ObservableCollection, el rendimiento puede convertirse en una preocupación. Regenerar un PDF cada vez que cambia la colección podría ser intensivo en recursos si hay una gran cantidad de artículos. Para mitigar esto, considera agrupar actualizaciones o usar técnicas como la paginación para evitar sobrecargar el proceso de generación de PDF.

Reproducción eficaz de PDF

Para asegurar que el renderizado de PDF sea eficiente, ten en cuenta los siguientes consejos:

  • Minimizar re-renderizados innecesarios: Solo regenerar el PDF cuando los datos cambien significativamente (por ejemplo, cuando se agreguen o eliminen artículos).
  • Optimizar el diseño de la tabla: Al renderizar grandes conjuntos de datos, divídelos en secciones más pequeñas y manejables para mejorar el tiempo de renderizado.
  • Usar caché: Cachear PDF previamente generados para datos estáticos o que cambian con poca frecuencia.

Conclusión

Combinando el ObservableCollection de C# con IronPDF, puedes generar fácilmente PDF dinámicos que reflejan cambios en tiempo real en los datos de tu aplicación. Ya sea que esté generando facturas, informes u otros documentos, este enfoque permite actualizar automáticamente el contenido del PDF cada vez que la colección subyacente cambia.

La integración de ObservableCollection asegura que su aplicación esté siempre actualizada con un esfuerzo mínimo, mientras que IronPDF maneja la labor de renderizar PDF de alta calidad. Siguiendo las mejores prácticas y consejos de rendimiento discutidos en este artículo, puede crear una experiencia de generación de PDF optimizada para sus aplicaciones .NET.

¿Quiere probar IronPDF? Descargue la prueba gratuita hoy para elevar sus proyectos de PDF en C#, y asegúrese de consultar la sección de documentación extensa para ver más de esta biblioteca en acción.

Preguntas Frecuentes

¿Qué es una ObservableCollection en C#?

La ObservableCollection es una clase en C# que implementa la interfaz INotifyCollectionChanged. Proporciona notificaciones cuando se añaden, eliminan o modifican elementos, lo que la hace ideal para el enlace de datos en aplicaciones de UI donde se necesitan actualizaciones en tiempo real.

¿Cómo puedo generar un PDF a partir de datos dinámicos en C#?

Usando IronPDF, puede generar PDFs a partir de datos dinámicos aprovechando la ObservableCollection de C#. A medida que la colección se actualiza, puede regenerar automáticamente el PDF para reflejar el estado actual de los datos.

¿Cuáles son los beneficios de usar ObservableCollection con la generación de PDF?

ObservableCollection permite el seguimiento de datos en tiempo real, lo cual es beneficioso al generar PDFs que necesitan reflejar cambios en datos en vivo. Combinada con IronPDF, asegura que cualquier actualización en los datos se refleje puntualmente en la salida del PDF.

¿Cómo puede enlazar datos de una ObservableCollection a un PDF en C#?

Puede enlazar datos de una ObservableCollection a un PDF iterando sobre los elementos de la colección y utilizando los métodos de IronPDF para agregarlos al PDF como elementos estructurados, como tablas o listas. Esto asegura que el contenido del PDF siempre esté sincronizado con los datos.

¿Cuál es un caso de uso común para generar PDFs con ObservableCollection en C#?

Un caso de uso común es la generación de facturas a partir de una ObservableCollection de elementos. A medida que se añaden o eliminan elementos de la colección, la factura PDF puede regenerarse para reflejar con precisión la lista actual de elementos, asegurando documentación actualizada.

¿Cómo maneja IronPDF la conversión de HTML a PDF en C#?

IronPDF puede convertir HTML a PDF usando métodos como RenderHtmlAsPdf y RenderHtmlFileAsPdf. Esto permite a los desarrolladores crear PDFs a partir de páginas web o cadenas HTML, facilitando la representación de contenido dinámico.

¿Qué debe considerarse al trabajar con grandes ObservableCollections para la generación de PDF?

Al tratar con colecciones grandes, considere usar la paginación, la actualización por lotes y la optimización del diseño para prevenir problemas de rendimiento. Las optimizaciones de procesamiento en memoria y representación de IronPDF también pueden ayudar a manejar datos grandes de manera eficiente.

¿Cómo puede IronPDF mejorar la productividad en aplicaciones .NET?

IronPDF mejora la productividad al proporcionar funciones robustas para la creación de PDF, como la personalización detallada del diseño, la conversión de HTML a PDF y la integración con diversas fuentes de datos. Simplifica tareas complejas de PDF, permitiendo a los desarrolladores centrarse en la lógica principal de la aplicación.

Jacob Mellor, Director de Tecnología @ Team Iron
Director de Tecnología

Jacob Mellor es Director de Tecnología de Iron Software y un ingeniero visionario pionero en la tecnología C# PDF. Como desarrollador original de la base de código principal de Iron Software, ha dado forma a la arquitectura de productos de la empresa desde su creación, ...

Leer más

Equipo de soporte de Iron

Estamos disponibles online las 24 horas, 5 días a la semana.
Chat
Email
Llámame