Assignment Help, Cloud Based ERP System, Microsoft NAV Certification
WELCOME !!

Please Register, ask for assignment solutions & post the solutions if you know any.

LETS START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.

Assignment Help, Cloud Based ERP System, Microsoft NAV Certification
WELCOME !!

Please Register, ask for assignment solutions & post the solutions if you know any.

LETS START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.

Assignment Help, Cloud Based ERP System, Microsoft NAV Certification

Stock Market, Online Tutoring, Cloud Based ERP System, Microsoft Dynamics Reporting, Microsoft Nav Certification


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Microsoft Dynamics Reporting Techniques Empty Microsoft Dynamics Reporting Techniques 12th October 2013, 10:16 pm

Formatted

Formatted
Administrator

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

http://kantipur.friendhood.net

2Microsoft Dynamics Reporting Techniques Empty Display Page 1 of 2 in RDLC Reports Navision 12th October 2013, 10:25 pm

Formatted

Formatted
Administrator

How it Works
The problem with knowing how many pages will there be in a printed report is that it's
something you won't know until the report has fnished printing! There's no way around this so,
unfortunately, we will have to process our report twice. That means double the execution time.
This is not recommended for large or process-intensive reports.
There is a lot going on in this recipe, but don't worry. We will take it step-by-step. In order to
use the code from this recipe, you will need to import the print to PDF codeunit. We will not see how that code works in this recipe. Just know that it takes the report you are running and saves it to a temporary PDF fle.

To start, we need to create an Automation control to count the number of pages in our PDF
document. NAV doesn't have built-in support for analyzing PDF fles so we have to build this
part of our solution in another programming language. In this case we are going to use C#
which we can compile and use inside NAV.
Let's take a look at the libraries we will be using. System.IO is used for reading and
writing to fles. The System.Text.RegularExpressions library is used to fnd
patterns of characters in strings or text variables. The last library, System.RunTime.
InteropServices is used to register the program on the computer so that it can be seen
and used by other applications like NAV.
Now we need to examine the attributes of our class. The frst attribute is called
ClassInterface. By setting the value to ClassInterfaceType.AutoDual we tell the
program to automatically register itself on the system, if we choose to register it at all (which
we will). The second attribute is called ProgId and is the name that our program will be
referenced by. The last is called COMVisible, which tells the system that this class can be
registered on the computer.

Download Videos :



Last edited by Formatted on 12th October 2013, 10:31 pm; edited 1 time in total

http://kantipur.friendhood.net

3Microsoft Dynamics Reporting Techniques Empty Microsoft Dynamics Reporting Techniques 2013 12th October 2013, 10:30 pm

Formatted

Formatted
Administrator

Alright, now we get to the meat of the program. It is a function called GetNoOfPages that
takes in a fle name and returns an integer named result. The frst two lines about streams
are fairly standard for opening a fle. The text of the fle is stored in the PDFText variable by
doing a ReadToEnd on the stream.
This part will be confusing if you have never encountered a Regular Expression before.
Basically, we are looking for a pattern like this:
/Type + "some optional, unknown amount of whitespace" + /Page (but
not /Pages)
If you open a PDF fle in Notepad and search for bits and pieces of this text you'll fnd that it
appears as metadata on every page in the fle.
Finally we have to close our streams. If we fail to do this the PDF fle will be locked and we
won't be able to use it.
That's the coding part for the automation. But there are some properties that need to be set,
specifcally the Register COM for interop. Remember those attributes that we set so that if
we ever registered this program, it would work? Well now we have to register it. Check the box,
compile it, and you are ready to go.
For the report, we need to create a function to tell it how many pages will print in all. We pass
it an integer variable and it stores it in a global integer variable called NoOfPages. We also
have to change the page number in the header to display the total number of pages.
Lastly, we need to create a codeunit to manage the printing of this report. This codeunit will
consist of four functions. The frst is a helper function called SetupFile. This function just
sets some global text variables that point to the path or folder of the PDF fle, the name of the
PDF fle, and the combined path plus name of the fle.
We also need a wrapper function for our Automation class. This function will be called
GetNumberOfPages. It creates a new instance of the Automation class, checks to make
sure that the fle exists, and counts the number of pages using the GetNoOfPagesPDF
function from the C# code. This value is then returned from the function.
Our third function is used to actually run our report. This function takes in the number of
pages we found by using the GetNumberOfPages function. It passes that value to the report
and runs it.
The last function is called PrintReportToPDF. The details of how this works can be found in
Chapter 10, Integration, in the Printing reports to PDF recipe. To give you a quick overview, we
delete any fles that have the same name, set up PDFCreator, print the report, and then clear
any changes that were made.
So how does all of that work together? Let's step through it. In the OnRun trigger, we set up
our fle to go to the local Temp directory on the computer. We then print our report to that
PDF fle. At this point we don't know how many pages will be printed, so the upper right-hand
corner of the report would look like "Page 1 of 0". Next, we determine how many pages are in
the PDF fle. We then call the same function to print the report to PDF, but this time we pass
the real number of pages instead of zero. Finally, we use the HYPERLINK command to open
the fle and display it to the user.
See how to print global page numbers in RDLC Reports.

http://kantipur.friendhood.net

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum