In this Post i am going to Explain how to extract PDF file. Suppose you have 500 Page PDF File in Original Folder and you wanna 10 only from the Original file and you want to save this 10 page in the Sample Folder.
First we need to download these .dll file from iTextSharp
itextsharp.dll
itextsharp.pdfa.dll
itextsharp.xtra.dll
and after that we need to write this code it will we working fine for me.
for more detail you can also visit: itextsharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
public partial class UploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SampleDocument();
}
public void SampleDocument()
{
DirectoryInfo directory = new DirectoryInfo(@"E:\\MY File\\EminoSoft\\File\\File\\PDFDockUpload1\\Orisinal/");
var myFile = (from f in directory.GetFiles()
orderby f.CreationTime descending
select f).First();
string sourcePdfPath = (@"E:\\MY File\\Ali\\File\\File\\PDFDockUpload1\\Orisinal/" + myFile);
string outputPdfPath = (@"E:\\MY File\\Ali\\File\\File\\PDFDockUpload1\\Sample/" + myFile);
if (!File.Exists(outputPdfPath))
{
PdfReader pdfReader = new PdfReader(sourcePdfPath);
int numberOfPages = pdfReader.NumberOfPages;
if (numberOfPages > 10)
{
ExtractPages(sourcePdfPath, outputPdfPath, 1, 10);
}
}
}
public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
reader = new PdfReader(sourcePdfPath);
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
First we need to download these .dll file from iTextSharp
itextsharp.dll
itextsharp.pdfa.dll
itextsharp.xtra.dll
and after that we need to write this code it will we working fine for me.
for more detail you can also visit: itextsharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text.RegularExpressions;
public partial class UploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SampleDocument();
}
public void SampleDocument()
{
DirectoryInfo directory = new DirectoryInfo(@"E:\\MY File\\EminoSoft\\File\\File\\PDFDockUpload1\\Orisinal/");
var myFile = (from f in directory.GetFiles()
orderby f.CreationTime descending
select f).First();
string sourcePdfPath = (@"E:\\MY File\\Ali\\File\\File\\PDFDockUpload1\\Orisinal/" + myFile);
string outputPdfPath = (@"E:\\MY File\\Ali\\File\\File\\PDFDockUpload1\\Sample/" + myFile);
if (!File.Exists(outputPdfPath))
{
PdfReader pdfReader = new PdfReader(sourcePdfPath);
int numberOfPages = pdfReader.NumberOfPages;
if (numberOfPages > 10)
{
ExtractPages(sourcePdfPath, outputPdfPath, 1, 10);
}
}
}
public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
reader = new PdfReader(sourcePdfPath);
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
No comments:
Post a Comment