GetImages.cs
- //
- // This code is part of Document Solutions for PDF demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Text;
- using GrapeCity.Documents.Pdf;
- using GrapeCity.Documents.Text;
- using GrapeCity.Documents.Drawing;
-
- namespace DsPdfWeb.Demos
- {
- // This sample loads a PDF (we use the document generated by the Wetlands sample)
- // and extracts all images from it. It then prints those images into a new PDF,
- // centered one per page.
- public class GetImages
- {
- public int CreatePDF(Stream stream)
- {
- var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 12 };
- using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf")))
- {
- var docSrc = new GcPdfDocument();
- // Load an existing PDF with some images:
- docSrc.Load(fs);
- // This call extracts information about images from the loaded PDF,
- // note that for large files it may take a while to complete:
- var imageInfos = docSrc.GetImages();
-
- var doc = new GcPdfDocument();
- var textPt = new PointF(72, 72);
- var imageRc = new RectangleF(72, 72 * 2, doc.PageSize.Width - 72 * 2, doc.PageSize.Height - 72 * 3);
-
- foreach (var imageInfo in imageInfos)
- {
- // The same image may appear on multiple locations,
- // imageInfo includes page indices and locations on pages;
- // for simplicity sake we only print page numbers here:
- var sb = new StringBuilder();
- imageInfo.Locations.ForEach(il_ => sb.Append((il_.Page.Index + 1).ToString() + ", "));
- var g = doc.NewPage().Graphics;
- g.DrawString($"This image appears on page(s) {sb.ToString().TrimEnd(' ', ',')} of the original PDF:", tf, new PointF(72, 72));
- g.DrawImage(imageInfo.Image, imageRc, null, ImageAlign.ScaleImage);
- }
- // Done:
- doc.Save(stream);
- return doc.Pages.Count;
- }
- }
- }
- }
-