//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Pdf.Annotations;usingGrapeCity.Documents.Svg; namespaceDsPdfWeb.Demos{// Shows how to programmatically control which annotations to include when saving a PDF as images.// The sample loads a one-page PDF containing several annotations, and saves that page as SVG// using DrawAnnotationFilter to include only annotations which satisfy certain conditions// (free text annotations that are within the page bounds).// The SVG is then inserted as the first page of the resulting PDF.// The second page shows the original PDF for reference. // The PDF used by this sample was generated by AnnotationTypes.publicclassAnnotationDrawFilter {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();// Load a PDF with some annotations into the document:usingvar fs = File.OpenRead(Path.Combine("Resources", "PDFs", "AnnotationTypes.pdf")); doc.Load(fs);usingvar svgStream = newMemoryStream();// Draw the first page of the PDF into an SVG: doc.Pages[0].SaveAsSvg(svgStream,newSaveAsImageOptions() {BackColor = Color.Transparent,DrawAnnotationFilter = (GcPdfDocument d, Page p, AnnotationBase a, refbool draw) => {// Draw only free text annotations which also fit horizontally within the page bounds: draw = a isFreeTextAnnotation fta && fta.Rect.Left > 0 && fta.Rect.Right < p.Size.Width; } }); svgStream.Position = 0;// Create a GcSvgDocument from the saved SVG so that we can draw it in the resulting PDF:usingvar svgDoc = GcSvgDocument.FromStream(svgStream);// Insert a page into the PDF and draw the SVG on it, compare to the original on the 2nd page:var page = doc.Pages.Insert(0);var g = page.Graphics;var rc = newRectangleF(36, 36, page.Size.Width - 72, page.Size.Height - 72); g.DrawRectangle(rc, Color.DarkGoldenrod); g.FillRectangle(rc, Color.PaleGoldenrod); g.DrawSvg(svgDoc, rc); // Done: doc.Save(stream);return doc.Pages.Count; } }}