SvgClipArt.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Svg;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15. using DsPdfWeb.Demos.Common;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // Use GcSvgDocument to render SVG files on PDF pages.
  20. // The SVG clip art used in this sample is from freesvg.org.
  21. public class SvgClipArt
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. // Load images from the resources folder:
  26. var images = new List<(string, GcSvgDocument)>();
  27. foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "SvgClipArt"), "*", SearchOption.AllDirectories))
  28. images.Add((Path.GetFileName(fname), GcSvgDocument.FromFile(fname)));
  29. images.Shuffle();
  30.  
  31. var doc = new GcPdfDocument();
  32. // Font and format for captions:
  33. const float sMargin = 72f / 6;
  34. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"));
  35. var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };
  36.  
  37. // Set up a 3x4 layout grid with 1/2" margins all around:
  38. const float margin = 36;
  39. const int rows = 4;
  40. const int cols = 3;
  41. float gapx = 72f / 4, gapy = gapx;
  42. float sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols;
  43. float sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows;
  44. if (sWidth > sHeight)
  45. {
  46. gapx += sWidth - sHeight;
  47. sWidth = sHeight;
  48. }
  49. else
  50. {
  51. gapy += sHeight - sWidth;
  52. sHeight = sWidth;
  53. }
  54. var ip = new PointF(margin, margin);
  55.  
  56. // Render all images within the grid, adding new pages as needed:
  57. var g = doc.NewPage().Graphics;
  58. for (int i = 0; i < images.Count(); ++i)
  59. {
  60. // Draw border around image:
  61. var rect = new RectangleF(ip, new SizeF(sWidth - gapx, sHeight - gapy));
  62. g.FillRectangle(rect, Color.LightGray);
  63. g.DrawRectangle(rect, Color.Black, 0.5f);
  64. rect.Inflate(-sMargin, -sMargin);
  65.  
  66. // Draw the SVG:
  67. var svg = images[i].Item2;
  68. var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
  69. if (s.Width > 0 && s.Height > 0)
  70. {
  71. // If image proportions are different from our target rectangle,
  72. // we resize the rectangle centering the image in it:
  73. var qSrc = s.Width / s.Height;
  74. var qTgt = rect.Width / rect.Height;
  75. if (qSrc < qTgt)
  76. rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0);
  77. else if (qSrc > qTgt)
  78. rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2);
  79. }
  80. // Render the SVG:
  81. g.DrawSvg(svg, rect);
  82.  
  83. // Print image file name as caption in the bottom slide margin:
  84. g.DrawString(Path.GetFileName(images[i].Item1), tf,
  85. new RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  86. TextAlignment.Center, ParagraphAlignment.Near, false);
  87. ip.X += sWidth;
  88. if (ip.X + sWidth > doc.PageSize.Width && i < images.Count() - 1)
  89. {
  90. ip.X = margin;
  91. ip.Y += sHeight;
  92. if (ip.Y + sHeight > doc.PageSize.Height)
  93. {
  94. g = doc.NewPage().Graphics;
  95. ip.Y = margin;
  96. }
  97. }
  98. }
  99. // Done:
  100. doc.Save(stream);
  101. // Dispose images after saving the PDF:
  102. images.ForEach(t_ => t_.Item2.Dispose());
  103. return doc.Pages.Count;
  104. }
  105. }
  106. }
  107.