How to Draw String with extra distance between symbols or lines

How to Draw String with extra distance between symbols or lines

Issue : How to Draw String with extra distance between symbols or lines.

Tips : To Draw String with extra distance between symbols or lines there can be used StringFormat.ALIGNMENT, StringFormat.LINE_ALIGNMENT and StringFormatFlags options of stringformat object.

Example :

import aspose.pycore as aspycore
from aspose.imaging import StringAlignment, StringFormatFlags, StringFormat, Image, GraphicsUnit, Font, RectangleF, \
Color, Rectangle, Pen, PointF, Graphics
from aspose.imaging.brushes import SolidBrush
from aspose.imaging.imageoptions import BmpOptions, PngOptions
from aspose.imaging.sources import FileCreateSource
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
def get_alignment_name(flags):
if flags == 0:
return "0"
return StringFormatFlags(flags).name
def draw_text(alignment, flags):
base_folder = data_dir
file_name = "output_" + StringAlignment(alignment).name + "_" + get_alignment_name(flags) + ".png"
output_file_name = os.path.join(base_folder, file_name)
font_sizes = [8.0, 12.0, 16.0, 24.0, 32.0, 48.0, 64.0, 96.0]
with BmpOptions() as obj_init:
obj_init.source = FileCreateSource("temp.dat", True)
with Image.create(obj_init, 500, 900) as bmp:
gr = Graphics(bmp)
gr.clear(Color.white)
gr.page_unit = GraphicsUnit.PIXEL
text = "Hello world 1111 \n2222"
brush = SolidBrush(Color.black)
ypos = 0.0
for font_size in font_sizes:
font = Font("Times New Roman", font_size)
obj_init2 = StringFormat()
obj_init2.custom_char_ident = PointF(5.0, 5.0)
obj_init2.alignment = alignment
obj_init2.format_flags = flags
format_ = obj_init2
rectangle = RectangleF(0.0, ypos, 200.0, font_size * 2.5)
gr.draw_string(text, font, brush, rectangle, format_)
ypos += rectangle.height * 1.2
gr.draw_rectangle(Pen(Color.red), rectangle)
bmp.save(output_file_name, PngOptions())
if delete_output:
os.remove(output_file_name)
# Horizontal left to right
draw_text(StringAlignment.NEAR, 0)
draw_text(StringAlignment.FAR, 0)
draw_text(StringAlignment.CENTER, 0)
# Horizontal right to left
draw_text(StringAlignment.NEAR, StringFormatFlags.DIRECTION_RIGHT_TO_LEFT)
draw_text(StringAlignment.FAR, StringFormatFlags.DIRECTION_RIGHT_TO_LEFT)
draw_text(StringAlignment.CENTER, StringFormatFlags.DIRECTION_RIGHT_TO_LEFT)
# Vertical left to right
draw_text(StringAlignment.NEAR, StringFormatFlags.DIRECTION_VERTICAL)
draw_text(StringAlignment.FAR, StringFormatFlags.DIRECTION_VERTICAL)
draw_text(StringAlignment.CENTER, StringFormatFlags.DIRECTION_VERTICAL)