ASP.NET Core MVC PDF Viewer
How to compress and reduce PDF file size in ASP.NET Core MVC web app on Azure using C#?
How to view, edit, compress an existing PDF file size using C# in ASP.NET MVC web application for Azure
Open, view an existing PDF file from web server and save compressed PDF in ASP.NET web server using C# is very simple. EdgePDF provides a simple C# demo code to accomplish the job.
- Provide multiple PDF document compression methods to reduce a PDF file size: Image compression, Unused objects remove, User data remove, Document clean up
- Easy to integrate in ASP.NET Web Forms, ASP.NET MVC web applications using C#
How to view, compress PDF file programmatically in asp.net using C#
Preparation
To run the following tutorial successfully, we need the following setup ready.
- For ASP.NET Core web app: Setup EdgePDF
- For ASP.NET Core MVC web app: Setup EdgePDF
- For ASP.NET (.net framework): Setup EdgePDF on IIS
- Several demo PDF files in folder C:\temp\
How to view compress an existing PDF file in web browser using ASP.NET C#?
The following steps and C# source code will show to compress a modified PDF file in EdgePDF web application using C# ASP.NET.
After you have completed the following guide, you can open, view an existing PDF file through url (a sample url
http://localhost:56643/?yourtarget=pdf-1.pdf)
To save the modified PDF document to compressed PDF file in web server
- Go to EdgePDF toolbar in web browser
- Click tab "Customize"
- Click command button "Save Server"
There are several methods to reduce a PDF file size, such as PDF embeded image compression, remove unused objects, delete all user data, and clean up the PDF document.
Usually the most effective method is through image compression. Here we will show the demo code to compress images inside a PDF file in ASP.NET web application.
- For ASP.NET Core web app, open file UserCommandProcessMiddleware.cs from /DemoProjects/EdgePDF for ASP.NET Core/
- For ASP.NET (.net framework) project, 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
- Create a new PDFOptimizeOptions object where you can apply all the image compression settings
- You need enable image compression in PDFOptimizeOptions object, by setting PDFOptimizeOptions.EnableImagesOptimization to true
- Apply image compression settings as you need. View more information about How to compress PDF images using C#
- Utilize method PDFOptimizer.Optimize() to create a new PDFDocument object with image compression applied
- Use method PDFDocument.Save() to save the compressed PDF file in the server
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.
PDFDocument document = null;
if (documentPath.EndsWith(".pdf"))
{
// document object includes user modified content
document = new 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
{
// create optimizing options
PDFOptimizeOptions ops = new PDFOptimizeOptions();
// enable image optimization
ops.EnableImagesOptimization = true;
// -- Options for Monochrome Image --
// to enable downsampling for those images with resolution higher than 300 dpi to 150 dpi
ops.MonochromeImageOptions.DownsampleMode = ImageDownsampleMode.Bicubic;
ops.MonochromeImageOptions.MaxResolutionLimit = 300F;
ops.MonochromeImageOptions.TargetResolution = 150F;
// to change image compression mode to JBIG2
ops.MonochromeImageOptions.KeepCompressionMode = false;
ops.MonochromeImageOptions.Compression = PDFCompression.JBIG2Decode;
// -- Options for Grayscale Image --
// to enable downsampling for those images with resolution higher than 120 dpi to 96 dpi
ops.GrayscaleImageOptions.DownsampleMode = ImageDownsampleMode.Bilinear;
ops.GrayscaleImageOptions.MaxResolutionLimit = 120F;
ops.GrayscaleImageOptions.TargetResolution = 96F;
// to change image compression mode to DCT
ops.GrayscaleImageOptions.KeepCompressionMode = false;
ops.GrayscaleImageOptions.Compression = PDFCompression.DCTDecode;
// set quality level, only available for compression mode DCT
ops.GrayscaleImageOptions.JPEGImageQualityLevel = JPEGImageQualityLevel.High;
// -- Options for Color Image --
// to enable downsampling for those images with resolution higher than 120 dpi to 96 dpi
ops.ColorImageOptions.DownsampleMode = ImageDownsampleMode.Bicubic;
ops.ColorImageOptions.MaxResolutionLimit = 120F;
ops.ColorImageOptions.TargetResolution = 96F;
// to change image compression mode to DCT
ops.ColorImageOptions.KeepCompressionMode = false;
ops.ColorImageOptions.Compression = PDFCompression.DCTDecode;
// set quality level, only available for compression mode DCT
ops.ColorImageOptions.JPEGImageQualityLevel = JPEGImageQualityLevel.Highest;
// apply optimizing
PDFDocument compressedPDFDoc = PDFOptimizer.Optimize(document, ops);
String compressedPDFFilepath = @"C:\\temp\" + paraFilepathValue;
compressedPDFFilepath = compressedPDFFilepath.Replace(".pdf", "-compressed.pdf");
compressedPDFDoc.Save(@compressedPDFFilepath);
}
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);
}
}
After complete the ASP.NET PDF viewer web application, you could easily publish the ASP.NET MVC web app to Azure cloud service. View details at
How to deploy ASP.NET MVC PDF viewer web application to Azure?