//// 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).// // The resulting TIFF is similar to one produced by the PdfToGrayscaleTiff sample,// with two differences: // - Unlike PdfToGrayscaleTiff, this code does need a valid DsImaging license// in addition to a DsPdf license to work in production, as it directly uses// GcBitmapGraphics.// - Because it avoids the additional step of saving each page to PNG, // this example works faster than PdfToGrayscaleTiff.//// 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.publicclassPdfToTiffGcImaging {publicintCreatePDF(Stream stream) {// Arbitrary target DPI for the generated TIFF, adjust as needed:constinttargetDPI = 200;staticintptToDpi(float pts) {return (int)Math.Round((pts * targetDPI) / 72f); } var inputDoc = newGcPdfDocument();usingvar fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf")); inputDoc.Load(fs); // 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) {// Create GcBitmap with same size as the PDF page, adjusted for the target DPI:usingvar bmp = newGcBitmap(ptToDpi(p.Size.Width), ptToDpi(p.Size.Height), true, ptToDpi(72), ptToDpi(72));// Draw the PDF page on the bitmap and apply in-place grayscale effect:usingvar g = bmp.CreateGraphics(Color.White); p.Draw(g, newRectangleF(0, 0, bmp.Width, bmp.Height)); bmp.ApplyEffect(GrayscaleEffect.Get());// Convert full color bitmap to grayscale bitmap and append it as a frame to the TIFF: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 = [];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; } }}