//
// 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 GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.TextMap;
using GrapeCity.Documents.Pdf.Layers;
using GrapeCity.Documents.Text;
namespace DsPdfWeb.Demos
{
// Finds all occurrences of a regular expression (specifically,
// the regex searches for strings that look like dates or times)
// and changes the text rendering attributes of all those occurrences.
// The following attributes can be changed:
// - Text rendering mode (fill/stroke/clip);
// - Text fill color and alpha;
// - Text stroke color and alpha.
// For reference, the original PDF without changes is appended to the result.
// The original PDF and the search regex are the same as used in DeleteText.
public class ChangeTextAttrs
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "TimeSheet.pdf"));
doc.Load(fs);
var pageCount = doc.Pages.Count;
// Duplicate the PDF:
doc.MergeWithDocument(doc);
// Modify text on the first copy:
for (int i = 0; i < pageCount; ++i)
{
var ftps = new FindTextParams(@"\d+/\d+/\d+|\d+:\d+| am| pm", false, false, 72, 72, false, true);
// TextRenderingAttrs allows you to specify the following text rendering attributes:
// - RenderingMode
// - FillAlpha
// - FillColor
// - StrokeAlpha
// - StrokePen
doc.Pages[i].SetTextRenderingAttrs(ftps, new TextRenderingAttrs() {
RenderingMode = TextRenderingMode.FillStroke,
FillColor = Color.Red,
FillAlpha = 0.5f,
StrokeAlpha = 1.0f,
StrokePen = new GrapeCity.Documents.Drawing.Pen(Color.Blue, 1)});
}
// Done:
doc.Save(stream);
return doc.Pages.Count;
}
}
}