Skip to content

Commit

Permalink
programmatic report and multi page document creation improvements.
Browse files Browse the repository at this point in the history
Add PDF generation and report enhancements

- Added `Document_Test` class with tests for PDF generation.
- Introduced `Card` and `Document` classes for report elements.
- Renamed test classes for consistency.
- Standardized `Textbox` to `Text` across multiple files.
- Added `Page` class for structured report page layout.
- Enhanced `Report` class with new methods for adding elements.
- Updated `ReportItemsBody`, `ReportItemsFooter`, and `ReportItemsHeader` to use `Text`.
- Changed `Textbox` class to `Text` and updated `CanGrow` property type.
  • Loading branch information
majorsilence committed Feb 18, 2025
1 parent d219266 commit a77b192
Show file tree
Hide file tree
Showing 15 changed files with 639 additions and 158 deletions.
146 changes: 146 additions & 0 deletions RdlCreator.Tests/Document_Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System;
using Majorsilence.Reporting.RdlCreator;
using NUnit.Framework;
using System.Text.RegularExpressions;
using Microsoft.Identity.Client;
using System.Threading.Tasks;
using UglyToad.PdfPig;
using System.Linq;

namespace Majorsilence.Reporting.RdlCreator.Tests
{
[TestFixture]
public class Document_Test
{
[Test]
public async Task SinglePagePdfDiskExport()
{
var document = GenerateData(1);
var pdfBytes = await document.Create();

using var fileStream = new FileStream("SinglePagePdf.pdf", FileMode.Create, FileAccess.Write);
fileStream.Write(pdfBytes);
await fileStream.DisposeAsync();

using var pdfDocument = PdfDocument.Open("SinglePagePdf.pdf");
var text = string.Join(" ", pdfDocument.GetPages().SelectMany(page => page.GetWords()).Select(word => word.Text));

Assert.That(text, Is.Not.Null);
Assert.That(pdfDocument.NumberOfPages, Is.EqualTo(1));
Assert.That(text, Is.EqualTo("Test Header Text Area 1 Lorem ipsum. 0"));
}

[Test]
public async Task MultiPagePdfTest()
{
var document = GenerateData(2);
var pdfBytes = await document.Create();

using var fileStream = new FileStream("MultiPagePdf.pdf", FileMode.Create, FileAccess.Write);
fileStream.Write(pdfBytes);
await fileStream.DisposeAsync();

using var pdfDocument = PdfDocument.Open("MultiPagePdf.pdf");
var text = string.Join(" ", pdfDocument.GetPages().SelectMany(page => page.GetWords()).Select(word => word.Text));

Assert.That(text, Is.Not.Null);
Assert.That(pdfDocument.NumberOfPages, Is.EqualTo(2));
Assert.That(text, Is.EqualTo("Test Header Text Area 1 Lorem ipsum. 0 Test Header Text Area 1 Lorem ipsum. 1"));
}

private RdlCreator.Document GenerateData(int pageCount = 1)
{
var document = new Document
{
Description = "Sample report",
Author = "John Doe",
PageHeight = "11in",
PageWidth = "8.5in",
//Width = "7.5in",
TopMargin = ".25in",
LeftMargin = ".25in",
RightMargin = ".25in",
BottomMargin = ".25in"
};

for (int i = 0; i < pageCount; i++)
{
document.WithPage((option) =>
{
option.WithHeight("10in")
.WithWidth("7.5in")
.WithText(new Text
{
Name = "Textbox1",
Top = ".1in",
Left = ".1in",
Width = "6in",
Height = ".25in",
Value = new Value { Text = "Text Area 1" },
Style = new Style { FontSize = "12pt", FontWeight = "Bold" }
})
.WithText(new Text
{
Name = "Textbox2",
Top = "1in",
Left = "1in",
Width = "6in",
Height = "4in",
Value = new Value { Text = "Lorem ipsum." },
Style = new Style
{
FontSize = "12pt",
BackgroundColor = "gray"
}
});

option.WithPageFooter(new PageFooter
{
Height = "14pt",
ReportItems = new ReportItemsFooter
{
Textbox = new Text
{
Name = "Footer",
Top = "1pt",
Left = "10pt",
Height = "12pt",
Width = "3in",
Value = new Value { Text = $"{i}" },
Style = new Style { FontSize = "10pt", FontWeight = "Normal" }
}
},
PrintOnFirstPage = "true",
PrintOnLastPage = "true"
});

option.WithPageHeader(
new PageHeader
{
Height = ".5in",
ReportItems = new ReportItemsHeader
{
Textbox = new Text
{
Name = "Header",
Top = ".1in",
Left = ".1in",
Width = "6in",
Height = ".25in",
Value = new Value { Text = "Test Header" },
Style = new Style { FontSize = "15pt", FontWeight = "Bold" }
}
},
PrintOnFirstPage = "true",
PrintOnLastPage = "true"
});
});
}

return document;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Majorsilence.Reporting.RdlCreator.Tests
{
[TestFixture]
public class ManualFluentChainedReportDefinitionTest
public class Reports_ChainedTest
{
string connectionString = "Data Source=sqlitetestdb2.db;";
string dataProvider = "Microsoft.Data.Sqlite";
Expand Down Expand Up @@ -59,30 +59,6 @@ public async Task PdfDiskExport()
Assert.That(text, Is.EqualTo("Test Data Set Report CategoryID CategoryName Description Beverages Soft drinks, coffees, teas, beers, and ales Condiments Sweet and savory sauces, relishes, spreads, and seasonings Confections Desserts, candies, and sweet breads Dairy Products Cheeses Grains/Cereals Breads, crackers, pasta, and cereal Meat/Poultry Prepared meats Produce Dried fruit and bean curd Seafood Seaweed and fish 1 of 1"));
}

[Test]
public async Task NoDataSetPdfDiskExport()
{
var create = new RdlCreator.Create();
var report = SmallTestData();
var fyiReport = await create.GenerateRdl(report);
using var ms = new Majorsilence.Reporting.Rdl.MemoryStreamGen();
await fyiReport.RunGetData(null);
await fyiReport.RunRender(ms, Majorsilence.Reporting.Rdl.OutputPresentationType.PDF);
var pdfStream = ms.GetStream();
pdfStream.Position = 0;

using var fileStream = new FileStream("NoDataSetExport.pdf", FileMode.Create, FileAccess.Write);
pdfStream.CopyTo(fileStream);
await fileStream.DisposeAsync();

using var pdfDocument = PdfDocument.Open("NoDataSetExport.pdf");
var text = string.Join(" ", pdfDocument.GetPages().SelectMany(page => page.GetWords()).Select(word => word.Text));

Assert.That(text, Is.Not.Null);
Assert.That(text, Is.EqualTo("Test Header Text Area 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 1 of 1"));
}


private RdlCreator.Report GenerateTestData()
{

Expand Down Expand Up @@ -141,7 +117,7 @@ private RdlCreator.Report GenerateTestData()
Height = ".5in",
ReportItems = new ReportItemsHeader
{
Textbox = new Textbox
Textbox = new Text
{
Name = "Textbox1",
Top = ".1in",
Expand All @@ -160,7 +136,7 @@ private RdlCreator.Report GenerateTestData()
Height = "14pt",
ReportItems = new ReportItemsFooter
{
Textbox = new Textbox
Textbox = new Text
{
Name = "Textbox5",
Top = "1pt",
Expand Down Expand Up @@ -190,13 +166,13 @@ private RdlCreator.Report GenerateTestData()
{
TableCell = new List<TableCell>
{
new TableCell { ReportItems= new TableCellReportItems(){ ReportItem = new Textbox { Name = "Textbox2",
new TableCell { ReportItems= new TableCellReportItems(){ ReportItem = new Text { Name = "Textbox2",
Value = new Value { Text = "CategoryID" },
Style = new Style { TextAlign = "Center", FontWeight = "Bold" } } } },
new TableCell { ReportItems= new TableCellReportItems(){ReportItem = new Textbox { Name = "Textbox3",
new TableCell { ReportItems= new TableCellReportItems(){ReportItem = new Text { Name = "Textbox3",
Value = new Value { Text = "CategoryName" },
Style = new Style { TextAlign = "Center", FontWeight = "Bold" } } } },
new TableCell { ReportItems= new TableCellReportItems(){ReportItem = new Textbox { Name = "Textbox4",
new TableCell { ReportItems= new TableCellReportItems(){ReportItem = new Text { Name = "Textbox4",
Value = new Value { Text = "Description" },
Style = new Style { TextAlign = "Center", FontWeight = "Bold" } } } }
}
Expand Down Expand Up @@ -259,13 +235,13 @@ private RdlCreator.Report GenerateTestData()
{
ReportItems = new TableCellReportItems()
{
ReportItem = new Textbox {
ReportItem = new Text {
Name = "CategoryName",
Value = new Value
{
Text = "=Fields!CategoryName.Value"
},
CanGrow = "true",
CanGrow = true,
Style = new Style
{
BorderStyle= new BorderStyle
Expand All @@ -290,14 +266,14 @@ private RdlCreator.Report GenerateTestData()
{
ReportItems= new TableCellReportItems()
{
ReportItem = new Textbox
ReportItem = new Text
{
Name = "Description",
Value = new Value
{
Text = "=Fields!Description.Value"
},
CanGrow = "true",
CanGrow = true,
Style = new Style
{
BorderStyle= new BorderStyle
Expand Down Expand Up @@ -325,88 +301,5 @@ private RdlCreator.Report GenerateTestData()
return report;
}

private RdlCreator.Report SmallTestData()
{

var report = new Report
{
Description = "Sample report",
Author = "John Doe",
PageHeight = "11in",
PageWidth = "8.5in",
Width = "7.5in",
TopMargin = ".25in",
LeftMargin = ".25in",
RightMargin = ".25in",
BottomMargin = ".25in"
}
.WithPageHeader(
new PageHeader
{
Height = ".5in",
ReportItems = new ReportItemsHeader
{
Textbox = new Textbox
{
Name = "Header",
Top = ".1in",
Left = ".1in",
Width = "6in",
Height = ".25in",
Value = new Value { Text = "Test Header" },
Style = new Style { FontSize = "15pt", FontWeight = "Bold" }
}
},
PrintOnFirstPage = "true",
PrintOnLastPage = "true"
})
.WithPageFooter(new PageFooter
{
Height = "14pt",
ReportItems = new ReportItemsFooter
{
Textbox = new Textbox
{
Name = "Footer",
Top = "1pt",
Left = "10pt",
Height = "12pt",
Width = "3in",
Value = new Value { Text = "=Globals!PageNumber + ' of ' + Globals!TotalPages" },
Style = new Style { FontSize = "10pt", FontWeight = "Normal" }
}
},
PrintOnFirstPage = "true",
PrintOnLastPage = "true"
})
.WithBody("36pt")
.WithReportText(new Textbox
{
Name = "Textbox1",
Top = ".1in",
Left = ".1in",
Width = "6in",
Height = ".25in",
Value = new Value { Text = "Text Area 1" },
Style = new Style { FontSize = "12pt", FontWeight = "Bold" }
})
.WithReportText(new Textbox
{
Name = "Textbox2",
Top = "1in",
Left = "1in",
Width = "6in",
Height = "4in",
Value = new Value { Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." },
Style = new Style
{
FontSize = "12pt",
BackgroundColor = "gray"
}
});

return report;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Majorsilence.Reporting.RdlCreator.Tests
{
[TestFixture]
public class DataProviderTest
public class Reports_DataProviderTest
{
string connectionString = "Data Source=sqlitetestdb2.db;";
string dataProvider = "Microsoft.Data.Sqlite";
Expand Down
Loading

0 comments on commit a77b192

Please sign in to comment.