Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Create XML document in C# using LINQ XDocument Class

In this articles, I am going to explain you how to create XML document in C# using LINQ XDocument class. Below is step by step tutorial.

Namespace Required

Create an empty asp.net web application and add a webform to it.
Include below namespace.

How to extract text from PDF file using iTextSharp with C#

In this tutorial, I am going to explain you how to extract text from PDF file using iTextSharp with C# in ASP.NET. Below is step by step tutorial.

Creating ASP.NET Empty Application

Create an ASP.NET Empty WebForm project as shown below.
Go to FileNewProject. A new window will be open as shown below.
Now go to WebVisual Studio 2012 → select .NET Framework 4.5 → select ASP.NET Empty Web Application and give project name and click on OK.

Creating asp.net 4.5 empty project

Now, an asp.net empty project will be created. Add a new webform to application.

Installing iTextSharp

Now the next step is to add iTextSharp reference to your application. We can add reference by two ways.
First: Download from Internet
Click on the below link to download the dll.
https://github.com/itext/itextsharp Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.
or Second: Nuget Package Manager
Go to TOOLS → Library Package Manager → Manage NuGet Packages for Solution.. and a new window will open. Type and search for iTextSharp and click on Install button as shown below. Once installed successfully, you can check iTextSharp in references folder.

Adding iTextSharp
Installing iTextSharp

You can also install by using Package Manager Console.
Go to TOOLS → Library Package Manager → Package Manager Console → write Install-Package iTextSharp and press enter. This will install iTextSharp in application.

In aspx file

In designer file create two button controls, first button is used to generate pdf file and second button is used to extract text from pdf file. One textbox control to display extracted text from pdf. Designer file look like as shown below.

Aspx designer file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <table>
        <tr>
            <td><b>Extract Text from PDF file using iTextSharp</b></td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF File" OnClick="btnGeneratePDF_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnExtract" runat="server" Text="Extract Text From PDF File" OnClick="btnExtract_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Style="width: 500px; min-height: 150px;"> 
                </asp:TextBox>
            </td>
        </tr>
    </table>
</div>
</form>
</body>
</html>

C# Code

Complete C# code is given below.

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
 
}
 
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }
 
    // create pdf file and save it to the root directory of the application 
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
 
    Document doc = new Document();
 
    PdfWriter.GetInstance(doc, fs);
 
    doc.Open();
 
    Paragraph page = new Paragraph("This is first page (page number 1)");
    doc.Add(page);
 
    Paragraph para1 = new Paragraph();
    Chunk c1 = new Chunk(@"This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph.");
    c1.SetBackground(BaseColor.YELLOW);
    para1.Add(c1);
    doc.Add(para1);
 
    Paragraph para2 = new Paragraph();
    Chunk c2 = new Chunk(@"This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph.");
    c2.SetBackground(BaseColor.GREEN);
    para2.Add(c2);
    doc.Add(para2);
 
    doc.Close();
}
 
protected void btnExtract_Click(object sender, EventArgs e)
{
    //string FilePath = @"H:\\Demo\\WebApplication1\\WebApplication1\\Example.pdf";
 
    string FilePath = Server.MapPath("Example.pdf");
 
    if (File.Exists(FilePath))
    {
        string ExtractedData = string.Empty;
 
        using (PdfReader reader = new PdfReader(FilePath))
        {
            ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
 
            // 1. if pdf document has only one page
            //here second parameter is PDF Page number
            ExtractedData = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
 
 
            /*// 2. if pdf ducument has more than one page
            // iterating through all pages
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                ExtractedData = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
            }*/
 
 
            /*// if pdf single page is having more than one paragraph
            // then split paragraph using newline
            ExtractedData = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
            string[] lines = ExtractedData.Split('\n');
            StringBuilder sb = new StringBuilder();
            foreach (string line in lines)
            {
                // 
            }*/
 
        }
        TextBox1.Text = ExtractedData;
    }
}
}
}

When you click on the Generate PDF File button, a PDF will be generated and will be saved at root directory of application. When you open pdf file, you will see 3 paragraph as shown below.

PDF file generated using iTextSharp

Now when you click on Extract Text From PDF File, all the text from page one will be extracted and displayed to the TextBox. You can iterate through all the pages using foor loop. Code is added and commented above.

Extract text from PDF using iTextSharp

Export DataTable to PDF file using iTextSharp and download/transmit at client machine

In this article, I am going to explain you how to export DataTable to PDF file using iTextSharp in C# and download or transmit at client machine. First, you need to download iTextSharp dll from the internet. Click on the below link to download the dll.
https://github.com/itext/itextsharp

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

Related Article

  1. How to export GridView data into PDF using iTextSharp in asp.net with C#
  2. Insert an image into PDF using iTextSharp with C# (C-Sharp)
  3. How to add meta information of PDF file using iTextSharp with C-Sharp
  4. How to extract images from a pdf file using C#.Net

In Code-Behind File

Add below nampespaces.

using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;

C# Code Snippet

Below is complete C# code to generate pdf file using dummy data table.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        ExportDataTableToPdfandDownloadAtClient();
    }
}

private void ExportDataTableToPdfandDownloadAtClient()
{
    // creating datatable and adding dumy data
    DataTable dtEmployee = new DataTable();
    dtEmployee.Columns.Add("EmpId", typeof(Int32));
    dtEmployee.Columns.Add("Name", typeof(string));
    dtEmployee.Columns.Add("Gender", typeof(string));
    dtEmployee.Columns.Add("Salary", typeof(Int32));
    dtEmployee.Columns.Add("Country", typeof(string));
    dtEmployee.Rows.Add(1, "Rahul", "Male", 60000, "India");
    dtEmployee.Rows.Add(2, "John", "Male", 50000, "USA");
    dtEmployee.Rows.Add(3, "Mary", "Female", 75000, "UK");
    dtEmployee.Rows.Add(4, "Mathew", "Male", 80000, "Australia");

    // creating document object
    iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(PageSize.A4);
    rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);
    Document doc = new Document(rec);
    doc.SetPageSize(iTextSharp.text.PageSize.A4);
    PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
    doc.Open();

    //Creating paragraph for header
    BaseFont bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font fntHead = new iTextSharp.text.Font(bfntHead, 16, 1, iTextSharp.text.BaseColor.ORANGE);
    Paragraph prgHeading = new Paragraph();
    prgHeading.Alignment = Element.ALIGN_LEFT;
    prgHeading.Add(new Chunk("Employee Details".ToUpper(), fntHead));
    doc.Add(prgHeading);

    //Adding paragraph for report generated by
    Paragraph prgGeneratedBY = new Paragraph();
    BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font fntAuthor = new iTextSharp.text.Font(btnAuthor, 8, 2, iTextSharp.text.BaseColor.BLUE);
    prgGeneratedBY.Alignment = Element.ALIGN_RIGHT;
    prgGeneratedBY.Add(new Chunk("Report Generated by : ASPArticles", fntAuthor));
    prgGeneratedBY.Add(new Chunk("\nGenerated Date : " + DateTime.Now.ToShortDateString(), fntAuthor));
    doc.Add(prgGeneratedBY);

    //Adding a line
    Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, iTextSharp.text.BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
    doc.Add(p);

    //Adding line break
    doc.Add(new Chunk("\n", fntHead));

    //Adding  PdfPTable
    PdfPTable table = new PdfPTable(dtEmployee.Columns.Count);

    for (int i = 0; i < dtEmployee.Columns.Count; i++)
    {
        string cellText = Server.HtmlDecode(dtEmployee.Columns[i].ColumnName);
        PdfPCell cell = new PdfPCell();
        cell.Phrase = new Phrase(cellText, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffffff"))));
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#990000"));
        //cell.Phrase = new Phrase(cellText, new Font(Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(grdStudent.HeaderStyle.ForeColor)));
        //cell.BackgroundColor = new BaseColor(grdStudent.HeaderStyle.BackColor);
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.PaddingBottom = 5;
        table.AddCell(cell);
    }

    //writing table Data
    for (int i = 0; i < dtEmployee.Rows.Count; i++)
    {
        for (int j = 0; j < dtEmployee.Columns.Count; j++)
        {
            table.AddCell(dtEmployee.Rows[i][j].ToString());
        }
    }

    doc.Add(table);
    doc.Close();
    writer.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;" + "filename=EmployeeDetails.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(doc);
    Response.End();
}

Below is the pdf file will be downloaded at client machine.

Export datatable to pdf file using itextsharp


DOWNLOAD SOURCE CODE

LINQ Take and TakeWhile Partitioning operators in C#

LINQ Take and TakeWhile Partitioning operators in C#

In this article, I am going to explain you LINQ Take and TakeWhile partitioning operators in C#. I have already explained Skip and SkipWhile operators in detail. Below is the four partitioning operators. Partitioning operators split the collection into two parts and returns one part only.
1) Skip
2) SkipWhile  (click here to read about Skip and SkipWhile)
3) Take - It takes element up-to specified position starting from first element in collection or sequence.
4) TakeWhile - It take elements from given collection until the specified condition is true.

Let's understand the these operators with examples.

Using Take Partitioning Operator In LINQ

Example 1

Let's apply Take operator on a string array which contains some words. Here Take operator will Take first 4 element from collection and return.

string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
var takeFirstFourWords = words.Take(4);
foreach (var word in takeFirstFourWords)
{
    Console.WriteLine(word);
}
Console.ReadKey();
//OUTPUT:
//Five
//Four
//One
//Three

Example 2

Here, it will return first 5 numbers.

int[] numbers = { 9, 8, 6, 7, 2, 5, 4, 1, 3, 0 };
var takeFirstFiveNumbers = numbers.Take(5);
foreach (var number in takeFirstFiveNumbers)
{
    Console.WriteLine(number);
}
Console.ReadKey();
//OUTPUT:
//9
//8
//6
//7
//2

Example 3

Take and TakeWhile operator do not support in query syntax but still we can use Take on query variable. Consider the example given below, we need 2 words from first 4 words whose length is greater than 4.

string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };

var stringNumbers = from s in words
                              where s.Length > 4 // find words whose length is grater than 4
                              select s;

Console.WriteLine("words whose length is grater than 4");
foreach (var word in stringNumbers)
{
    Console.WriteLine(word);
}
/*
OUTPUT:
Three
Eight
Seven
*/

var takeFirstTwoWords = stringNumbers.Take(2); //take first 2 words
Console.WriteLine("After taking two words");
foreach (var word in takeFirstTwoWords)
{
    Console.WriteLine(word);
}
Console.ReadKey();
/*
OUTPUT:
Three
Eight
*/

Using TakeWhile Partitioning Operator In LINQ

Example 1

Let's understand TakeWhile operator with example. Consider the string array of words given below. Here first 3 elements are "Five", "Four", "One" and their length is less than 5. So, according to TakeWhile definition, for the first 3 elements condition is true because there length is less than 5, but from 4th element that is "Three" here length is equal to 5 that means if length is equal to 5 then condition becomes false, so TakeWhile will return those element for which condition was true.

string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
//length is               4         4         3         5           4         5          3         5           3         4
//length < 5           true     true     true     false
var data = words.TakeWhile(x => x.Length < 5);
foreach (var d in data)
{
    Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//Five
//Four
//One

Example 2

Here, we will use same example as shown above, in above example we used (<) less than operator, now we will use (>) greater than operator. Here the first element is "Five" whose length is equal to 4, but it is less than 5, so condition becomes false at first element only because we are checking for the element whose length is greater than 5. So, if condition becomes false at first statement only then it will return an empty collection.

string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
//length is               4         4         3         5           4         5          3         5           3         4
//length > 5           false    
var data = words.TakeWhile(x => x.Length > 5);
foreach (var d in data)
{
    Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//No Output

Example 3

Here, in below example first 5 elements when divided by 5, they have some Remainder but from 6th element that is '5' which has Remainder 0, here the condition becomes false because we are checking for Remainder should not be equal to zero, so it will return those element for which condition was true.

int[] numbers = { 9, 8, 6, 7, 2, 5, 4, 1, 3, 0 };

var data = numbers.TakeWhile(x => x % 5 != 0);
foreach (var d in data)
{
    Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//9
//8
//6
//7
//2

Using TakeWhile With Index

We can use, second overload of TakeWhile. It passes index of each elements. In below example, it take all the element till the length of elements is greater than it's index. Here, condition becomes false at 5th element("Nine"), so it will return elements for which condition was true.

string[] words = { "Five", "Four", "One", "Three", "Nine", "Eight", "Six", "Seven", "Two", "Zero" };
// length is              4         4         3         5           4         5         3         5           3         4
// index  is              0         1         2         3           4         5         6         7           8         9
// length > index   true     true     true     true       false      -         -          -            -          -
var data = words.TakeWhile((x,i) => x.Length > i);
foreach (var d in data)
{
    Console.WriteLine(d);
}
Console.ReadKey();
//OUTPUT:
//Five
//Four
//One
//Three