Using ASP.NET PDF Viewer Control
How to read, view, edit a scanned PDF file in C# ASP.NET application?
How to open, convert a scanned image PDF file to text editable, searchable PDF, view, edit PDF online in ASP.NET using C#
Open, read, view and edit an existing scanned PDF file in ASP.NET using C# is very simple. EdgePDF provides a simple C# demo code to accomplish the task.
- Read a scanned PDF file from server side file system
- Convert the scanned PDF file to a editable PDF document, which you can do text searching on it.
- Load the editable PDF file into EdgePDF, where you can read, view, edit PDF in web browser
How to view, edit a scanned PDF file in asp.net using C#
Preparation
To run the following tutorial successfully, we need the following setup ready.
- Install EdgePDF demo project in IIS
- Prepare OCR resource files. Copy all contents from downloaded package /OCR Files/Source/ to {EdgePDF demo project folder}/OCRSource/
- A demo scanned PDF file from C:\temp\pdf-scanned-1.pdf
How to read, view, edit a scanned PDF file online in ASP.NET C#?
The following steps and C# source code will help to setup a demo project, which allows you to open, view, edit a scanned PDF file in web browser using ASP.NET C# code.
After you have completed the following guide, you can open a scanned PDF file for editing through url (a sample url
http://localhost:56643/?yourtarget=pdf-scanned-1.pdf)
- Open file UserCommandProcessHandler.ashx from {EdgePDF demo project}/RasterEdge_Resource_Files/
- Go to method FileProcess()
- Get target scanned PDF file id from url parameter "yourtarget"
- Create a new PDFDocument object with scanned PDF file loaded
- Get the scanned PDF file pages count
- Create a new MemoryStream object for converted editable PDF document
- Set OCR resource files location
- For each scanned PDF page, using OCR engine to convert to a editable PDF page, and save editable PDF page to a Stream object
- Combine all editable PDF pages into the new editable PDF document
- Call method REProcessControl.PageLoadFile() to load editable PDF document into EdgePDF for viewing, editing
public override PDFWebDocument FileProcess()
{
HttpRequest request = this.Context.Request;
if (!String.IsNullOrEmpty(request.QueryString["yourtarget"]))
{
// load file
String docid = request.QueryString["yourtarget"];
if (docid == null) docid = "0";
String fileName = docid;
PDFDocument scannedDoc = new PDFDocument(@"C:\\temp\" + docid);
int pageCount = scannedDoc.GetPageCount();
MemoryStream editableDoc = new MemoryStream();
// The folder that contains OCR '.traineddata' files.
OCRHandler.SetTrainResourcePath(@"C:\Sites\EdgePDF.Sample\OCRSource");
MemoryStream[] streams = new MemoryStream[pageCount];
for (int i = 0; i < scannedDoc.GetPageCount(); i++)
{
streams[i] = new MemoryStream();
OCRPage page = OCRHandler.Import(scannedDoc.GetPage(i));
page.Recognize();
page.SaveTo(MIMEType.PDF, streams[i]);
}
PDFDocument.CombineDocument(streams, editableDoc);
return REProcessControl.PageLoadFile(request, this.Manager, editableDoc, fileName);
}
else
{
Logger.Log(">>> Unknown load file mode. Load default file defined in Web.config.", LogType.DEBUG);
// load default file. defined in Web.config
return REProcessControl.PageLoadFile(request, this.Manager, "", LoadType.Server);
}
}