TextJustifyRules.cs
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.Drawing;using System.IO;using GrapeCity.Documents.Pdf;using GrapeCity.Documents.Text;using GCTEXT = GrapeCity.Documents.Text;using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos.Basics{
public class TextJustifyRules { public int CreatePDF(Stream stream) { var fntInfo = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Bold.ttf")); var fntText = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSansJP-Regular.ttf"));
var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics;
var tl = g.CreateTextLayout(); tl.TextAlignment = TextAlignment.Distributed; tl.JustifiedSpaceExtension = 0f; tl.JustifiedTextExtension = 20f;
var tf = new TextFormat { FontSize = 24f, Font = fntText }; var tfInfo = new TextFormat { FontSize = 11f, Font = fntInfo };
float marginx = 120, marginy = 36; tl.MaxWidth = page.Size.Width - marginx * 2; var text = "abcdefg!1010101010abc999999-12345本列島で使され99 555";
float DrawText(TextLayout tl, float y) { var pt = new PointF(marginx, y + 20); tl.Append(text, tf); tl.PerformLayout(true); var rc = new RectangleF(pt, new SizeF(tl.ContentWidth, tl.ContentHeight)); g.FillRectangle(rc, Color.PaleGoldenrod); g.DrawString($"LineBreakingRules.{tl.LineBreakingRules}, TextExtensionStrategy.{tl.TextExtensionStrategy}:", tfInfo, new PointF(marginx / 2f, y)); g.DrawTextLayout(tl, pt); tl.Clear(); return rc.Bottom + 16; }
float y = marginy, dy = marginy * 3.5f; y = DrawText(tl, y); tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel; y = DrawText(tl, y); tl.TextExtensionStrategy = TextExtensionStrategy.Excel; y = DrawText(tl, y); tl.LineBreakingRules = LineBreakingRules.Simplified; tl.WordBoundaryRules = WordBoundaryRules.Simplified; tl.TextExtensionStrategy = TextExtensionStrategy.Default; y = DrawText(tl, y); tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel; y = DrawText(tl, y); tl.TextExtensionStrategy = TextExtensionStrategy.Excel; y = DrawText(tl, y);
// Done: doc.Save(stream); return doc.Pages.Count; } }}