|
ASP.NET PDF Viewer Control
How to view, export PDF to Microsoft Word file programmatically without Office, interop installed in ASP.NET using C#?
How to open, view, comment, edit Adobe PDF online, and convert modified PDF to Word in web browser using C# in ASP.NET MVC web application.
Open, read, view and comment an existing PDF, and convert to Word in ASP.NET using C# is very simple. EdgePDF provides a simple C# demo code to accomplish the job.
- Open, read, view, edit an existing PDF file from server file system, database, or network in web browser
- Convert modified PDF document to Office Word .docx file online in ASP.NET
How to view PDF online and export to Word programmatically in asp.net using C#
Preparation
To run the following tutorial successfully, we need the following setup ready.
- Install EdgePDF demo project in IIS
- Several demo PDF files in folder C:\temp\
How to convert, export PDF to Word file using C# in ASP.NET web application?
The following steps and C# source code will learn how to open, view, edit a PDF file in web browser using C# ASP.NET, and export modified PDF document to Office Word (.docx) file in the web server in ASP.NET web application.
After you have completed the following guide, you can open, view, comment a PDF file online through url (a sample url
http://localhost:56643/?yourtarget=pdf-1.pdf)
To export the modified PDF file to Microsoft Word document in web server
- Go to EdgePDF toolbar in web browser
- Click tab "Customize"
- Click command button "Save Server"
- Open file UserCommandProcessHandler.ashx from {EdgePDF demo project}/RasterEdge_Resource_Files/
- Go to method SaveFile_OnServer()
- Create a new PDFDocument object with EdgePDF displayed PDF file loaded
- Define a new file path var ("newExportFilepath") for converted Word document
- Use method PDFDocument.ConvertToDocument() save and convert the modified PDF document into a new Word .docx file
public override void SaveFile_OnServer(String fid, String savePath, String responseMsg)
{
// You can process saved pdf document here, like add watermark, ...
var documentPath = savePath;
if (!String.IsNullOrEmpty(documentPath))
{
Logger.LogFile(fid, "SaveFileOnServer: output file path " + documentPath, LogType.DEBUG);
// To verify the output file.
RasterEdge.Imaging.Basic.BaseDocument document = null;
if (documentPath.EndsWith(".pdf"))
{
// document object includes user modified content
document = new RasterEdge.XDoc.PDF.PDFDocument(documentPath);
}
if (document != null)
{
// Get the upload information of the file
RasterEdge.WDP.DocUploadInfo docinfo = RasterEdge.WDP.Manager.FileManager.getUploadinfoByFid(fid);
// Get your file open url parameters value, if needed.
String paraFilepathValue = docinfo.GetRequestParameters("yourtarget");
if (!String.IsNullOrEmpty(paraFilepathValue))
{
try
{
string newExportFilepath = paraFilepathValue.Replace(".pdf", "-exported-to-word.docx");
document.ConvertToDocument(DocumentType.DOCX, @"C:\temp\" + newExportFilepath);
}
catch (Exception ex)
{
// Process error code, and return error information here
Logger.LogFile(fid, ex.Message + ". fail to save file to server.", LogType.ERROR);
}
}
else
{
Logger.LogFile(fid, "no 'yourtarget' in the HTTPRequest.", LogType.INFO);
}
}
else
{
Logger.LogFile(fid, "output PDF file is invalid.", LogType.ERROR);
}
}
else
{
Logger.LogFile(fid, "fail to save file to server. output file path is null or empty.", LogType.ERROR);
}
}
How to convert, export scanned PDF to text editable, searchable Word file using C# in ASP.NET web application?
The following contents and C# source code will show how to export a scanned PDF file into text editable, searchable Office Word document in web browser using ASP.NET C# code.
After you have completed the following guide, you can open, view a scanned PDF file through url (a sample url
http://localhost:56643/?yourtarget=pdf-scanned-1.pdf).
To export the scanned PDF file to text searchable Word document in web server
- Go to EdgePDF toolbar in web browser
- Click tab "Customize"
- Click command button "Save Server"
- Open file UserCommandProcessHandler.ashx from {EdgePDF demo project}/RasterEdge_Resource_Files/
- Go to method SaveFile_OnServer()
- Create a new PDFDocument object with EdgePDF displayed PDF file loaded
- Define a new file path var ("newExportFilepath") for converted Word document
- Start using OCR to extract text content from the scanned PDF document
- Define OCR train resource files path using method OCRHandler.SetTrainResourcePath()
- For each PDF page, convert it to a OCRPage object
- Combine all OCRPage objects into a new text editable PDF document
- Last step: use method PDFDocument.ConvertToDocument() convert the new text PDF document into a new Word file
public override void SaveFile_OnServer(String fid, String savePath, String responseMsg)
{
// You can process saved pdf document here, like add watermark, ...
var documentPath = savePath;
if (!String.IsNullOrEmpty(documentPath))
{
Logger.LogFile(fid, "SaveFileOnServer: output file path " + documentPath, LogType.DEBUG);
// To verify the output file.
RasterEdge.Imaging.Basic.BaseDocument document = null;
if (documentPath.EndsWith(".pdf"))
{
// document object includes user modified content
document = new RasterEdge.XDoc.PDF.PDFDocument(documentPath);
}
if (document != null)
{
// Get the upload information of the file
RasterEdge.WDP.DocUploadInfo docinfo = RasterEdge.WDP.Manager.FileManager.getUploadinfoByFid(fid);
// Get your file open url parameters value, if needed.
String paraFilepathValue = docinfo.GetRequestParameters("yourtarget");
if (!String.IsNullOrEmpty(paraFilepathValue))
{
try
{
string newExportFilepath = paraFilepathValue.Replace(".pdf", "-exported-to-editable-word.docx");
// The folder that contains '.traineddata' files.
OCRHandler.SetTrainResourcePath(@"C:\Source");
int pageCount = document.GetPageCount();
MemoryStream[] streams = new MemoryStream[pageCount];
for (int i = 0; i < document.GetPageCount(); i++)
{
streams[i] = new MemoryStream();
OCRPage page = OCRHandler.Import(document.GetPage(i));
page.Recognize();
page.SaveTo(MIMEType.PDF, streams[i]);
}
MemoryStream editablePDFDocStream = new MemoryStream();
PDFDocument.CombineDocument(streams, editablePDFDocStream);
PDFDocument editablePDFDoc = new PDFDocument(editablePDFDocStream);
editablePDFDoc.ConvertToDocument(DocumentType.DOCX, @"C:\temp\" + newExportFilepath);
}
catch (Exception ex)
{
// Process error code, and return error information here
Logger.LogFile(fid, ex.Message + ". fail to save file to server.", LogType.ERROR);
}
}
else
{
Logger.LogFile(fid, "no 'yourtarget' in the HTTPRequest.", LogType.INFO);
}
}
else
{
Logger.LogFile(fid, "output PDF file is invalid.", LogType.ERROR);
}
}
else
{
Logger.LogFile(fid, "fail to save file to server. output file path is null or empty.", LogType.ERROR);
}
}
|