TextToOutlines.cs
//
// This code is part of GrapeCity Documents for PDF samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Pdf.Graphics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;

namespace GcPdfWeb.Samples
{
    // This sample shows how to convert all text in an existing PDF to glyph outlines.
    // The resulting PDF will look exactly like the original, but all glyphs in it
    // will be rendered as graphics paths. This can be used to manipulate the paths,
    // or to make sure it will be impossible to copy or search the text.
    // Note that the resulting documents will have no fonts (see for example 
    // the Document Properties | Fonts tab in GcPdfViewer).
    // The original PDF used by this sample was generated by Wetlands.
    public class TextToOutlines
    {
        public int CreatePDF(Stream stream)
        {
            GcPdfDocument doc = new GcPdfDocument();
            using (var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf")))
            {
                // Load the source PDF into a temp document:
                var srcDoc = new GcPdfDocument();
                srcDoc.Load(fs);
                // Draw all pages of the source document on pages of the new PDF:
                foreach (var srcPage in srcDoc.Pages)
                {
                    var page = doc.Pages.Add(srcPage.Size);
                    // Setting Graphics.DrawTextAsPath to true makes all glyphs draw as graphics paths
                    // instead of rendering text using fonts:
                    page.Graphics.DrawTextAsPath = true;
                    // Draw the source page on the target:
                    srcPage.Draw(page.Graphics, srcPage.Bounds);
                }
                // Done:
                doc.Save(stream);
                return doc.Pages.Count;
            }
        }
    }
}