Thursday, August 28, 2014

Introduction to XAML

Introduction to XAML

  1.  XAML is a defined as Extensible Markup Language whose primary role is in the creation of .Net Objects.
  2. XAML is a declarative Markup language based on XML. A declarative language is one that describes what it is required but not how the requirement will be met (eg: HTML is a declarative languague that describes what should be displayed on a webpage. Similarly XAML can be used as a means of describing objects)
  3. Although it is used mainly in Windows Presentation Foundation (WPF), However it can be used in WF, Sliverlight and XPS technologies.
    1. WF: WF creates Workflows and XAML is used to define the structure of Wokflows including conditions and rules
    2. Sliverlight: Sliverlight is a tool for creating rich internet application and uses XAML to construct and initiate
    3. XPS: XAML is used in XPS for formatted electronic documents

Important points of XAML
XAML is a DECLARATIVE langauage
XAML is designed to be in CONJUNCTION WITH WPF
XAML is used to DESCRIBE A TREE OF .NET OBJECTS IN WPF
XAML is a tool that can BE USED IN CONJUNCTION WITH OTHER TECHNOLOGIES
XAML targets A SUBSET of .Net Namespaces
XAML displays its data in a HIERARCHICAL fashion
XAML uses XML namespaces
XAML is complied into BAML at runtime
XAML Elements represents System.Windows Namespaces classes in wPF

Saturday, August 23, 2014

How to display PDF in WPF application



Create a new WPF project
  1. In the design mode of the Mainwindow.xaml. Add button control and WindowsFormHost Control in the MainWindow.xaml
<Window x:Class="DemoPDFView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="619" Width="700">
    <Grid>
        <Button Name="Btn1" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="126,27,0,0"/>
        <WindowsFormsHost Name="WFH1" HorizontalAlignment="Left" Height="430" VerticalAlignment="Top" Width="492" Margin="10,69,0,0"/>

    </Grid>
</Window>
  1. Now Add a UserControl by clicking on Add New Item in the solution Explorer



Go to the design Mode: In the design view of UserControl
  1. Right Click on the tool box and Click on “Choose Items”

  1. You will get the following “Choose Toolbox Items” window. Go to “COM Components” Tab  and check “Adobe PDF Reader” and click on “OK”

  1. You will now find the adobe control in the tool boxes.


 Add the Adobe PDF Reader(axAcroPDF1)control in the form.



Set the width and height properties of the axAcroPDF1 control same as WindowsFormsHost control
That is



In the code behind file Copy and paste the code below InitializeComponent();
Add the line this.axAcroPDF1.LoadFile(filename);
Your userControl class should look like this
public partial class UserControl1 : UserControl
    {
        public UserControl1(string filename)
        {
            InitializeComponent();
            this.axAcroPDF1.LoadFile(filename);
        }
    }

Now in the code behind file of MainWindow.xaml  add the code for the Btn1 button event 

  private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            // Configure open file dialog box
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".pdf"; // Default file extension
            dlg.Filter = "pdf files (*.pdf) |*.pdf;"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                var uc = new UserControl1(filename);
                this.WFH1.Child = uc;
            }
        }

OUTPUT:




Tuesday, August 19, 2014

How to display PDF documents in Windows applications(C# and VB.Net)



To display PDF in Windows Application:
  1. Start a new Windows Project
  2. Right Click on the tool box and Click on “Choose Items”

  1. You will get the following “Choose Toolbox Items” window. Go to “COM Components” Tab  and check “Adobe PDF Reader” and click on “OK”

  1. You will now find the adobe control in the tool boxes.

  1. In the Design View Drag and drop Adobe PDF reader Control and a Button Control

On the button click event add the following code:

VB.Net code

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dlg As OpenFileDialog
        dlg = New OpenFileDialog()
        dlg.Filter = "pdf files (*.pdf) |*.pdf;"
        dlg.ShowDialog()
        If (Not String.IsNullOrEmpty(dlg.FileName)) Then
            AxAcroPDF1.LoadFile(dlg.FileName)
        End If
     End Sub


C# .Net Code

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "pdf files (*.pdf) |*.pdf;";
            dlg.ShowDialog();
            if(dlg.FileName != null)
            {
                axAcroPDF1.LoadFile(dlg.FileName);
            }

        }

Output: