PDF Tool using C# and iTextsharp


PDF Tool is Console Application to create seperate PDF file from given huge PDF for selective range of PageNumbers. Developed in the .Net platform with iTextsharp third Party dll for reading and creating a PDF File.

PDF Tool


PDF Tool is designed to create individual PDF file of selective range from Huge PDF containing more number of pages.

Software Requirement:



  • .Net Framework 4.0

  • • Itextsharp – Free PDF tool version (5.1.2.0)



FromPage and ToPage is passed as input param to create the PDF starting from FromPage to ToPage value


namespace PDFTool
{
class Program
{
static void Main(string[] args)
{
clsSplitPDF splitPDF = new clsSplitPDF();
string outputFileName = @"D:\PDF\OutputPDF.pdf";
string inputFileName = @"D:\PDF\InputPDF.pdf";
int fromPage = 10;
int toPage = 15;
if (File.Exists(inputFileName))
{
Common.TitleMessage("PDF Tool");
splitPDF.CopyFile(outputFileName, inputFileName, fromPage, toPage);
Common.DisplayMessage("Execution Completed.");
}
else
{
Common.DisplayMessage("Source file [" + inputFileName + "] does not exist.");
}
Console.ReadKey();
}
}

public static class Common
{
public static void TitleMessage(string message)
{
Console.WriteLine("/***************************************************************/");
Console.WriteLine("\t\t" + message);
Console.WriteLine("/***************************************************************/");
}
public static void DisplayMessage(string message)
{
Console.WriteLine("\n" + message);
}
public static void ErrorMessage(string message)
{
Console.WriteLine("\n" + message);
}
}
public class clsSplitPDF
{
public void CopyFile(string outputFileName, string inputFileName, int fromPage, int toPage)
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(inputFileName);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
int i = 0;
if (n > fromPage && n > toPage)
{
while (i < n)
{
i++;
if (i >= fromPage && i <= toPage)
{
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}

}
// step 5: we close the document
document.Close();
Common.DisplayMessage("File Created.");
}
else
{
Common.ErrorMessage("Total Number is less than From Page/ To Page");
}

}
}
}




Note:

  1. Programatically creates new a PDF File over the given selective Range

  2. Only One Range of Input can be Specified

Comments

Author: ketan Italiya18 Nov 2013 Member Level: Gold   Points : 0

This article is so helpful.

Thanks.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: