//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.IO.Compression;usingSystem.Collections.Generic;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Common; namespaceDsPdfWeb.Demos{// This sample shows how the GcPdfDocument.OptimizeFonts() method can be used// to reduce the size of a PDF by merging duplicate and removing unused embedded fonts.//// This sample extracts the first five pages of a PDF, and then merges those pages// into a single new document. Because each page has its own set of embedded fonts,// the created PDF contains redundant font data, resulting in excessive file size.// The sample records the merged PDF's size, then it runs OptimizeFonts() on that PDF// and records the size of the optimized PDF. These results are printed into the// PDF returned by the sample, thus illustrating the effect of running OptimizeFonts().//// Note that while in this sample the PDF being optimized is somewhat contrived,// similar PDFs do appear in real life applications. The OptimizeFonts()// method may reduce (sometimes significantly) the size of such PDFs.// Actual results will vary.// // See also RemoveDuplicateImages.publicclassOptimizeFonts {publicintCreatePDF(Stream stream) {// Create a 5 page non-optimal PDF:var srcFn = Path.Combine("Resources", "PDFs", "CompleteJavaScriptBook.pdf");var tmpInput = MakeInputFile(srcFn);var fiInput = newFileInfo(tmpInput); // Create a new PDF, load the source PDF into it, and optimize it:var tmpOutput = Path.GetTempFileName();var tmpDoc = newGcPdfDocument();using (var fs = File.OpenRead(tmpInput)) { tmpDoc.Load(fs);// By default GcPdfDocument uses CompressionLevel.Fastest when saving a PDF.// If PDF size is important, we should use optimal compression: tmpDoc.CompressionLevel = CompressionLevel.Optimal; tmpDoc.OptimizeFonts(); tmpDoc.Save(tmpOutput); }var fiOutput = newFileInfo(tmpOutput); // Record the input and output file sizes in the resultant PDF:var doc = newGcPdfDocument();Common.Util.AddNote(String.Format("Using the GcPdfDocument.OptimizeFonts() method reduced the size of a 5-page PDF from {0:N0} to {1:N0} bytes " +"by merging duplicate and removing unused font data.\n" +"To reproduce these results locally, download and run this sample. You may also modify the sample code to keep the temporary " +"input and output files and compare their sizes using a file manager.", fiInput.Length, fiOutput.Length), doc.NewPage()); doc.Save(stream); // Clean up:File.Delete(tmpInput);File.Delete(tmpOutput); return doc.Pages.Count; } staticstringMakeInputFile(string inFn) {var indoc = newGcPdfDocument();usingvar fs = File.OpenRead(inFn); indoc.Load(fs); // Create 5 PDFs from the first 5 pages of the source document:var pageCount = 5;var docs = newList<GcPdfDocument>(pageCount);for (int i = 0; i < pageCount; ++i) {var outdoc = newGcPdfDocument(); outdoc.MergeWithDocument(indoc, newMergeDocumentOptions() { PagesRange = newOutputRange(i + 1, i + 1) }); docs.Add(outdoc); }// Merge the PDFs into a single document:var doc = newGcPdfDocument();foreach (var d in docs) doc.MergeWithDocument(d); // Save the resultant PDF in a temp file:var outFn = Path.GetTempFileName(); doc.Save(outFn);return outFn; } }}