Exemple #1
0
        /// <summary>
        /// Creates PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string dataPath1 = File1.ResolveClientUrl(File1.Value);

            if (System.IO.Path.GetExtension(dataPath1).Equals(".pdf"))
            {
                Stream stream1 = File1.PostedFile.InputStream;

                //Load a existing PDF document
                PdfLoadedDocument ldoc = new PdfLoadedDocument(stream1);

                //Create a new PDF compression options
                PdfCompressionOptions options = new PdfCompressionOptions();

                if (this.compressImage.Checked)
                {
                    //Compress image.
                    options.CompressImages = true;
                    options.ImageQuality   = int.Parse(this.imageQuality.SelectedValue);
                }
                else
                {
                    options.CompressImages = false;
                }
                //Compress the font data
                if (this.optFont.Checked)
                {
                    options.OptimizeFont = true;
                }
                else
                {
                    options.OptimizeFont = false;
                }
                //Compress the page contents
                if (this.optPageContents.Checked)
                {
                    options.OptimizePageContents = true;
                }
                else
                {
                    options.OptimizePageContents = false;
                }
                //Remove the metadata information.
                if (this.removeMetadata.Checked)
                {
                    options.RemoveMetadata = true;
                }
                else
                {
                    options.RemoveMetadata = false;
                }

                //Set the options to loaded PDF document
                ldoc.CompressionOptions = options;

                //Save to disk
                if (this.CheckBox1.Checked)
                {
                    ldoc.Save("Document1.pdf", Response, HttpReadType.Open);
                }
                else
                {
                    ldoc.Save("Document1.pdf", Response, HttpReadType.Save);
                }
            }
        }
Exemple #2
0
        public ActionResult CompressExistingPDF(bool compressImage = false, bool optPageContents = false, bool removeMetaData = false, bool optFont = false, string imageQuality = "50", string Browser = "null", HttpPostedFileBase file = null)
        {
            if (file != null && file.ContentLength > 0)
            {
                //Load a existing PDF document
                PdfLoadedDocument ldoc = new PdfLoadedDocument(file.InputStream);

                //Create a new PDF compression options
                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.CompressionOptions = options;

                string inputFileSize = "";
                inputFileSize = GetActualSize(file.ContentLength);

                Session["inputFileSize"] = inputFileSize;

                MemoryStream ms = new MemoryStream();
                ldoc.Save(ms);
                string outputFileSize = "";
                outputFileSize = GetActualSize(ms.Length);

                Session["outputFileSize"] = outputFileSize;

                string[] fileName = file.FileName.Split(new string[] { ".pdf" }, StringSplitOptions.RemoveEmptyEntries);

                //Save to disk
                //Stream the output to the browser.
                if (Browser == "Browser")
                {
                    return(ldoc.ExportAsActionResult(fileName[0] + "_compressed.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
                }
                else
                {
                    return(ldoc.ExportAsActionResult(fileName[0] + "_compressed.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save));
                }
            }
            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());
            }
        }
Exemple #3
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());
            }
        }
Exemple #4
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);
        }