//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingSystem.Collections.Generic;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Drawing;usingDsPdfWeb.Demos.Common;usingGrapeCity.Documents.Pdf.Annotations;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos{// This sample loads all images found in a directory, then renders each image// in the largest possible size on a separate page of the PDF.// Finally it inserts a TOC of image thumbnails linked to the large images// into the document. // See also SlidePages.publicclassImageLinks {privateclassImageInfo {publicstringName;publicIImageImage;publicintPageIdx; } publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Bold.ttf"));// 1/2" page margins all around:constfloatmargin = 36; // Load all images from the Resources/Images folder:List<ImageInfo> imageInfos = [];foreach (var fname inDirectory.GetFiles(Path.Combine("Resources", "Images"), "*", SearchOption.AllDirectories)) imageInfos.Add(newImageInfo() { Name = Path.GetFileName(fname), Image = Util.ImageFromFile(fname) }); imageInfos.Shuffle();// Set up image alignment that would center images horizontally and align to top vertically:var ia = newImageAlign(ImageAlignHorz.Center, ImageAlignVert.Top, true, true, true, false, false);// Image rectangle for full-sized images - whole page:var rBig = newRectangleF(margin, margin, doc.PageSize.Width - margin * 2, doc.PageSize.Height - margin * 2);// Render all images full-size, one image per page:for (int i = 0; i < imageInfos.Count; ++i) {var g = doc.NewPage().Graphics;var ii = imageInfos[i]; g.DrawImage(ii.Image, rBig, null, ia); ii.PageIdx = i; } // Insert page(s) with thumbnails into the beginning of the document as a 4x5 grid (see SlidePages):constintrows = 5;constintcols = 4;float gapx = 72f / 4, gapy = gapx;float sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols;float sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows;if (sWidth > sHeight) { gapx += sWidth - sHeight; sWidth = sHeight; }else { gapy += sHeight - sWidth; sHeight = sWidth; }constfloatsMargin = 72f / 6;// Center thumbnails vertically too: ia.AlignVert = ImageAlignVert.Center;// Text format for image captions:var tf = newTextFormat() { Font = font, FontSize = sMargin * 0.65f };// Insertion point:var ip = newPointF(margin, margin);var page = doc.Pages.Insert(0);for (int i = 0; i < imageInfos.Count; ++i) {var ii = imageInfos[i];var rect = newRectangleF(ip, newSizeF(sWidth - gapx, sHeight - gapy));// Add a link to the page where the full-sized image is (the page index// will be updated when we know how many pages are in TOC, see below): page.Annotations.Add(newLinkAnnotation(rect, newDestinationFit(ii.PageIdx)));// Draw thumbnail:var g = page.Graphics; g.FillRectangle(rect, Color.LightGray); g.DrawRectangle(rect, Color.Black, 0.5f); rect.Inflate(-sMargin, -sMargin); g.DrawImage(ii.Image, rect, null, ia, outRectangleF[] imageRect); g.DrawRectangle(imageRect[0], Color.DarkGray, 1);// Print image file name as caption in the bottom slide margin: g.DrawString(ii.Name, tf, newRectangleF(rect.X, rect.Bottom, rect.Width, sMargin),TextAlignment.Center, ParagraphAlignment.Near, false); ip.X += sWidth;if (ip.X + sWidth > doc.PageSize.Width) { ip.X = margin; ip.Y += sHeight;if (ip.Y + sHeight > doc.PageSize.Height) { page = doc.Pages.Insert(doc.Pages.IndexOf(page) + 1); ip.Y = margin; } } }// We now go through all TOC pages, updating page indices in links' destinations// to account for the TOC pages inserted in the beginning of the document:int tocPages = doc.Pages.IndexOf(page) + 1;for (int i = 0; i < tocPages; ++i) {foreach (var ann in doc.Pages[i].Annotations)if (ann isLinkAnnotation link && link.DestisDestinationFit dest) link.Dest = newDestinationFit(dest.PageIndex.Value + tocPages); }// Done: doc.Save(stream); imageInfos.ForEach((ii_) => ii_.Image.Dispose());return doc.Pages.Count; } }}