//
// 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 System.Text;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.AcroForms;
namespace GcPdfWeb.Samples
{
// TBD:
public class ReplaceText
{
public void CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "LeaseAgreementDemo.pdf"));
doc.Load(fs);
// Replace:
// "Jane Donahue" -> "John Doe"
// "(123)098-7654" -> "(007)123-4567"
// "janed@example.com" -> "johnd@example.com"
doc.ReplaceText(new FindTextParams("Jane Donahue", false, true), "John Doe");
doc.ReplaceText(new FindTextParams("(123)098-7654", false, true), "(007)123-4567");
doc.ReplaceText(new FindTextParams("janed@example.com", false, true), "johnd@example.com");
// "13-Dec-20 22:16:00" -> date now
// "13-Dec-22 22:16:00" -> date now + 2 years
var termStart = DateTime.Now;
var termEnd = DateTime.Now + TimeSpan.FromDays(365 * 2);
doc.ReplaceText(new FindTextParams("13-Dec-20 22:16:00", false, true), termStart.ToShortDateString() + " " + termStart.ToShortTimeString());
doc.ReplaceText(new FindTextParams("13-Dec-22 22:16:00", false, true), termEnd.ToShortDateString() + " " + termStart.ToShortTimeString());
// "condominium" -> "flat"
var font = GrapeCity.Documents.Text.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuib.ttf"));
doc.ReplaceText(new FindTextParams("condominium", true, false), "flat", null, font);
// For reference, append the original PDF:
Common.Util.AddNote("For reference, the following pages contain a copy of the original unmodified PDF.", doc.NewPage());
var docOrig = new GcPdfDocument();
docOrig.Load(fs);
doc.MergeWithDocument(docOrig);
// Done:
doc.Save(stream);
}
}
}