Display page X of Y in RDLC Reports of Navision
What sounds like a simple task is actually quite complicated in NAV. This recipe will show you
how to print the total number of pages on every page of a report.
You must have PDFCreator installed on your machine. This recipe was tested with version
0.9.8 and 0.9.9, and is not guaranteed to work with future or previous releases of PDFCreator.
You must also have Visual Studio 2005 or later installed on your machine in order to write the
C# code for this recipe.
1. Create a new class library project named NAVUtilities in Visual Studio.
2. Add a new fle called PDFPageCounter with the following code:
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace NAVUtilities
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("PDFPageCounter")]
[ComVisible(true)]
public class PDFPageCounter
{
public int GetNoOfPagesPDF(string FileName)
{
int result = 0;
FileStream fs = new FileStream(FileName, FileMode.Open,
FileAccess.Read);
StreamReader r = new StreamReader(fs);
string pdfText = r.ReadToEnd();
System.Text.RegularExpressions.Regex regx = new
Regex(@"/Type\s*/Page[^s]");
System.Text.RegularExpressions.MatchCollection matches =
regx.Matches(pdfText);
result = matches.Count;
r.Close();
fs.Close();
return result;
}
}
}
3. View the Properties of the project.
4. On the Application tab set the Assembly Name to Packt-PDFWatermark.
5. On the Build tab set the Register for COM interop property to True (checked)
6. Save and compile your objects.
7. Create a new report by following the Using the Report Generation Wizard recipe.
8. Add the following global variable:
Name Type
NoOfPages Integer
9. Add a global function named SetNoOfPages.
10. The function should take the following parameter:
Name Type
NoOfPagesIn Integer
11. Add the following code to the function:
NoOfPages := NoOfPagesIn;
12. Delete the Page No. label and textbox from the Header section.
13. Replace them with a single textbox.
14. Set the following property on the textbox control:
Property Value
SourceExpr 'Page ' + FORMAT(CurrReport.PAGENO)
+ ' of ' + FORMAT(NoOfPages)
15. Save and close the report.
16. Create a new codeunit from Object Designer.
17. Add the following global variables:
Name Type Subtype Length
PrintToPDF Codeunit (See Printing Reports to PDF recipe)
FileName Text 1024
FileDir Text 1024
FullFileName Text 1024
NoOfPages Integer
18. Add a global function named GetNumberOfPages.
19. The function should take the following parameter:
Name Type Length
FileNameIn Text 1024
20 It should return an integer named NoOfPagesOut.
21. Add the following local variable:
Name Type Subtype
PDFUtil Automation 'Packt-PDFPageCounter'.PDFPageCounter
22. Add the following code to the function:
IF ISCLEAR(PDFUtil) THEN
CREATE(PDFUtil);
IF EXISTS(FileNameIn) THEN
NoOfPagesOut := PDFUtil.GetNoOfPagesPDF(FileNameIn);
CLEAR(PDFUtil);
EXIT(NoOfPagesOut);
23. Add a global function named PrintReportToPDF.
24. Add the following code to the function:
IF EXISTS(FullFileName) THEN
ERASE(FullFileName);
PrintToPDF.SetupPDFCreator(FileDir, FileName);
RunReport;
PrintToPDF.ClearPDFCreator;
25. Add a global function named RunReport.
26. Add the following global variable:
Name Type Subtype
ReportToRun Report Page X of Y
27. Add the following code to the function:
CLEAR(ReportToRun);
ReportToRun.USEREQUESTFORM := FALSE;
ReportToRun.SetNumberOfPages(NoOfPages);
ReportToRun.RUNMODAL;
IF NOT PrintToPDF.WaitUntilFileExists(FullFileName) THEN
ERROR(Text001, FullFileName);
28. Add a global function named SetupFile.
29. The function should take in the following parameters:
Name Type Length
FileDir Text 1024
FileNameIn Text 1024
30. Add the following code to the function:
FileDir := FileDirIn;
FileName := FileNameIn;
FullFileName := COPYSTR(FileDirIn + '\' + FileName, 1,
MAXSTRLEN(FullFileName));
31. Add the following code to the OnRun trigger:
SetupFile(ENVIRON('Temp'), 'TempPDF.pdf');
PrintReportToPDF;
NoOfPages := GetNumberOfPages(FullFileName);
PrintReportToPDF;
HYPERLINK(FullFileName);
32. Save and close the codeunit.
Download Object files :
Last edited by Formatted on 12th October 2013, 10:31 pm; edited 2 times in total