예제 #1
0
        public ActionResult CompressExistingPDF(bool compressImage = false, bool optPageContents = false, bool removeMetaData = false, bool optFont = false, string imageQuality = "50", string Browser = "null")
        {
            if (Request.Form.Files != null && Request.Form.Files.Count != 0)
            {
                PdfLoadedDocument ldoc = new PdfLoadedDocument(Request.Form.Files[0].OpenReadStream());

                PdfCompressionOptions options = new PdfCompressionOptions();
                if (compressImage)
                {
                    //Compress image.
                    options.CompressImages = true;
                    options.ImageQuality   = int.Parse((imageQuality));
                }
                else
                {
                    options.CompressImages = false;
                }

                //Compress the font data
                if (optFont)
                {
                    options.OptimizeFont = true;
                }
                else
                {
                    options.OptimizeFont = false;
                }

                //Compress the page contents
                if (optPageContents)
                {
                    options.OptimizePageContents = true;
                }
                else
                {
                    options.OptimizePageContents = false;
                }

                //Remove the metadata information.
                if (removeMetaData)
                {
                    options.RemoveMetadata = true;
                }
                else
                {
                    options.RemoveMetadata = false;
                }

                //Set the options to loaded PDF document
                ldoc.Compress(options);

                //If the position is not set to '0' then the PDF will be empty.
                MemoryStream ms = new MemoryStream();
                ldoc.Save(ms);
                ms.Position = 0;
                ldoc.Close(true);
                //Download the PDF document in the browser.
                FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf");
                fileStreamResult.FileDownloadName = "Compression.pdf";
                return(fileStreamResult);
            }
            else
            {
                ViewBag.lab = "Choose a valid PDF file.";
                ViewData.Add("imageQuality", new SelectList(new string[] { "10", "20", "30", "40", "50", "60", "70", "80", "90", "100" }, "50"));
                return(View());
            }
        }
예제 #2
0
        public IActionResult CompressPDF()
        {
            string path = Path.Combine(_hostingEnvironment.ContentRootPath, "Data", "PDF_succinctly.pdf");

            FileStream inputDocument = new FileStream(path, FileMode.Open);

            //Load an existing PDF document
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);

            //Create a new compression option.
            PdfCompressionOptions options = new PdfCompressionOptions();

            //Enable the compress image.
            options.CompressImages = true;

            //Set the image quality.
            options.ImageQuality = 30;

            //Optimize the font in the PDF document
            options.OptimizeFont = true;

            //Optimize page contents
            options.OptimizePageContents = true;

            //Remove metadata from the PDF document
            options.RemoveMetadata = true;

            //Flatted form fields in the PDF document
            if (loadedDocument.Form != null)
            {
                loadedDocument.Form.Flatten = true;
            }

            //Flatten all the annotation in PDF document
            foreach (PdfPageBase page in loadedDocument.Pages)
            {
                if (page.Annotations != null)
                {
                    page.Annotations.Flatten = true;
                }
            }

            //Assign the compression option and compress the PDF document
            loadedDocument.Compress(options);

            //Save the PDF document.
            MemoryStream outputDocument = new MemoryStream();

            //Save the PDF document
            loadedDocument.Save(outputDocument);
            outputDocument.Position = 0;

            //Close the document
            loadedDocument.Close(true);


            //Download the PDF document in the browser.
            FileStreamResult fileStreamResult = new FileStreamResult(outputDocument, "application/pdf");

            fileStreamResult.FileDownloadName = "Compressed_PDF_document.pdf";

            return(fileStreamResult);
        }