//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Collections.Generic;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Imaging;usingGrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos{// This example shows how to convert an arbitrary PDF to a multi-frame TIFF// in which each frame corresponds to a single PDF page, converting full color// original pages to grayscale images with an arbitrary target resolution// (in the sample code the resolution is set to 200 dpi).//// Note that while the code in this example uses some DsImaging features,// it does not need a DsImaging license to work without issues.// See PdfToTiffDsImaging for an example that does the same conversion faster,// but does require a valid DsImaging license.//// In order to seamlessly build this sample into the demo site framework,// the generated TIFF is then converted back into a PDF. In a real life scenario// you can just use the code that generates the TIFF directly.publicclassPdfToGrayscaleTiff {publicintCreatePDF(Stream stream) {// Arbitrary target DPI for the generated TIFF, adjust as needed:constinttargetDPI = 200; var inputDoc = newGcPdfDocument();usingvar fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf")); inputDoc.Load(fs); usingvar ms = newMemoryStream();usingvar bmp = newGcBitmap();// NOTE: to produce a TIFF disk file, you would create a GcTiffWriter// with the file path as the parameter. But due to the demo browser requirements,// we create a GcTiffWriter on a memory stream instead:// using var tiffWriter = new GcTiffWriter("result.tiff");usingvar msTiff = newMemoryStream();using (var tiffWriter = newGcTiffWriter(msTiff)) {foreach (var p in inputDoc.Pages) {// Save each PDF page as PNG with target resolution: ms.Position = 0; p.SaveAsPng(ms, newSaveAsImageOptions { Resolution = targetDPI }); ms.Position = 0; bmp.Load(ms);// Load the PNG into GcBitmap, apply in-place grayscale effect,// convert to grayscale bitmap and append it to the TIFF: bmp.ApplyEffect(GrayscaleEffect.Get());usingvar gbmp = bmp.ToGrayscaleBitmap(); tiffWriter.AppendFrame(gbmp); } }// At this point tiffWriter has the newly created TIFF. If it was created on// a disk file, that file would contain the TIFF when tiffWriter is disposed.//// To use this sample in the demo browser, we have to convert the TIFF// back to a PDF that is returned to the controller. msTiff.Position = 0;var doc = newGcPdfDocument();usingvar tiffReader = newGcTiffReader(msTiff);List<IDisposable> disposables = newList<IDisposable>();foreach (var tp in tiffReader.Frames) {var img = tp.ToImage(ImageBinding.InMemoryData);var p = doc.Pages.Add(newSizeF(img.Width / img.HorizontalResolution * 72, img.Height / img.VerticalResolution * 72)); p.Graphics.DrawImage(img, p.Bounds, null, ImageAlign.Default); disposables.Add(img); }// Save the PDF: doc.Save(stream);// We can only dispose images after saving the PDF: disposables.ForEach(d_ => d_.Dispose());return doc.Pages.Count; } }}