'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsSystem.LinqImportsSystem.Collections.GenericImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.Pdf.ArticlesImportsGrapeCity.Documents.Drawing '' This sample shows how to create article threads in a PDF document.'' An article thread Is a sequence of related pages Or page areas that can be'' navigated sequentially (forward Or back) in a supporting PDF viewer.'' In this sample we load a number of photos from a folder And render them'' one per page in a random order.'' Some photos are associated (via known file names) with a specific subject'' (buildings, art, etc.), And we put all images associated with'' each subject into a subject-specific article thread (images that are Not'' associated with any known subject are put in the 'Miscellaneous' thread).'' In addition we create 3 threads for different aspect ratios (horizontal,'' vertical And square), And add each image to the appropriate aspect article.'' See section 'Article threads' in Navigating PDF pages'' for details on how to navigate article threads in Acrobat'' (our JavaScript PDF viewer provides a similar UI for this).PublicClassImageArticles'' Article names:ClassArticleNamesPublicSharedLandscape = "Subject: landscape"PublicSharedArt = "Subject: art"PublicSharedFlora = "Subject: flora"PublicSharedBuildings = "Subject: buildings"PublicSharedMisc = "Subject: Miscellaneous"PublicSharedAspectHorz = "Aspect: horizontal"PublicSharedAspectVert = "Aspect: vertical"PublicSharedAspectSquare = "Aspect: square"EndClass '' Associate known image file names with appropriate subjects:Shared_subjectsAsNewDictionary(OfString, String)() From { {"aurora.jpg", ArticleNames.Landscape}, {"chairs.jpg", ArticleNames.Buildings}, {"clouds.jpg", ArticleNames.Landscape}, {"colosseum.jpg", ArticleNames.Art}, {"deadwood.jpg", ArticleNames.Flora}, {"door.jpg", ArticleNames.Buildings}, {"ferns.jpg", ArticleNames.Flora}, {"fiord.jpg", ArticleNames.Landscape}, {"firth.jpg", ArticleNames.Landscape}, {"lady.jpg", ArticleNames.Art}, {"lavender.jpg", ArticleNames.Flora}, {"maple.jpg", ArticleNames.Buildings}, {"minerva.jpg", ArticleNames.Art}, {"newfoundland.jpg", ArticleNames.Landscape}, {"pines.jpg", ArticleNames.Flora}, {"purples.jpg", ArticleNames.Flora}, {"reds.jpg", ArticleNames.Flora}, {"road.jpg", ArticleNames.Landscape}, {"rome.jpg", ArticleNames.Art}, {"roofs.jpg", ArticleNames.Buildings}, {"sea.jpg", ArticleNames.Landscape}, {"skye.jpg", ArticleNames.Landscape}, {"tudor.jpg", ArticleNames.Buildings}, {"windswept.jpg", ArticleNames.Flora} }'' Images not in this list are 'misc'. '' Class to hold image info:ClassImageInfoPublicNameAsStringPublicImageAsIImagePublicSubjectAsStringPublicAspectAsStringEndClass FunctionCreatePDF(ByVal stream AsStream) AsInteger'' Load images and their associated infos:Dim imageInfos = NewList(OfImageInfo)ForEach fname InDirectory.GetFiles(Path.Combine("Resources", "Images"), "*", SearchOption.AllDirectories)Dim image = Util.ImageFromFile(fname)Dim aspect AsStringIf image.Width > image.Height Then aspect = ArticleNames.AspectHorzElseIf image.Width < image.Height Then aspect = ArticleNames.AspectVertElse aspect = ArticleNames.AspectSquareEndIfDim name = Path.GetFileName(fname)Dim subject AsString = Nothing_subjects.TryGetValue(name, subject)IfString.IsNullOrEmpty(subject) Then subject = ArticleNames.MiscEndIf imageInfos.Add(NewImageInfo() With { .Name = name, .Image = image, .Subject = subject, .Aspect = aspect })Next'' Randomize the order of images in the PDF: imageInfos.Shuffle() '' Keys are article thread names (from ArticleNames),'' values are ArticleThread objects to be added to the PDF:Dim articles = NewDictionary(OfString, ArticleThread)()ForEach subject In_subjects.Values.Distinct() articles.Add(subject,NewArticleThread() With { .Info = NewDocumentInfo() With {.Title = subject} })Next articles.Add(ArticleNames.Misc,NewArticleThread() With {.Info = NewDocumentInfo() With {.Title = ArticleNames.Misc}})Dim horizontals = NewArticleThread() With {.Info = NewDocumentInfo() With {.Title = ArticleNames.AspectHorz}}Dim verticals = NewArticleThread() With {.Info = NewDocumentInfo() With {.Title = ArticleNames.AspectVert}}Dim squares = NewArticleThread() With {.Info = NewDocumentInfo() With {.Title = ArticleNames.AspectSquare}} '' Create the document:Dim doc = NewGcPdfDocument() '' Add images (one per page) to the PDF and article threads:Dim ia = NewImageAlign(ImageAlignHorz.Center, ImageAlignVert.Top, True, True, True, False, False)For i = 0To imageInfos.Count - 1Dim page = doc.NewPage()Dim ii = imageInfos(i)Dim rc = NewRectangleF(72, 72, doc.PageSize.Width - 144, doc.PageSize.Height - 144)'' Note that we get the actual image bounds to precisely specify the page area in the thread:Dim imageBounds AsRectangleF() = Nothing page.Graphics.DrawImage(ii.Image, rc, Nothing, ia, imageBounds)Dim bounds = imageBounds(0)'' Add the image to proper subject and aspect threads: articles(ii.Subject).Beads.Add(NewArticleBead() With {.Page = page, .Bounds = bounds})If (ii.Aspect = ArticleNames.AspectHorz) Then horizontals.Beads.Add(NewArticleBead() With {.Page = page, .Bounds = bounds})ElseIf (ii.Aspect = ArticleNames.AspectVert) Then verticals.Beads.Add(NewArticleBead() With {.Page = page, .Bounds = bounds})Else squares.Beads.Add(NewArticleBead() With {.Page = page, .Bounds = bounds})EndIfNext'' Add subject and aspect article threads to the PDF:ForEach article In articles.Select(Function(a_) a_.Value) doc.ArticleThreads.Add(article)Next doc.ArticleThreads.Add(horizontals) doc.ArticleThreads.Add(verticals) doc.ArticleThreads.Add(squares) '' Done doc.Save(stream)Return doc.Pages.CountEndFunctionEndClass