How to properly align text with drawstring method
Contents
[
Hide
]
How to properly align text with drawstring method
Issue : How to properly align text with drawstring method.
Tips : To properly align text with drawstring method can be used presented below sample code.
Example :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pycore as aspycore | |
from aspose.imaging import Image, Graphics, Color, StringAlignment, StringFormat, StringFormatFlags, Font, \ | |
SizeF, RectangleF, Point, Pen | |
from aspose.imaging.brushes import SolidBrush | |
from aspose.imaging.imageoptions import 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 draw_string(base_folder, align): | |
file_name = "output_" + align + ".png" | |
output_file_name = os.path.join(base_folder, file_name) | |
font_names = ["Arial", "Times New Roman", "Bookman Old Style", "Calibri", "Comic Sans MS", "Courier New", "Microsoft Sans Serif", "Tahoma", "Verdana", "Proxima Nova Rg"] | |
font_sizes = [10.0, 22.0, 50.0, 100.0] | |
width = 1000 | |
height = 1200 | |
# Create an instance of PngOptions and set its various properties | |
with PngOptions() as png_options: | |
# Set the Source for PngOptions | |
png_options.source = FileCreateSource(output_file_name, False) | |
# Create an instance of Image | |
with Image.create(png_options, width, height) as image: | |
# Create and initialize an instance of Graphics class | |
graphics = Graphics(image) | |
# Clear Graphics surface | |
graphics.clear(Color.white) | |
# Create a SolidBrush object and set its various properties | |
brush = SolidBrush() | |
brush.color = Color.black | |
x = 10 | |
line_x = 0 | |
y = 10 | |
w = width - 20 | |
pen = Pen(Color.red, 1) | |
alignment = StringAlignment.NEAR | |
if align == "Left": | |
line_x = round(x, 0) | |
elif align == "Center": | |
alignment = StringAlignment.CENTER | |
line_x = round(x + w / 2.0, 0) | |
elif align == "Right": | |
alignment = StringAlignment.FAR | |
line_x = (x + w) | |
string_format = StringFormat(StringFormatFlags.EXACT_ALIGNMENT) | |
string_format.alignment = alignment | |
for font_name in font_names: | |
for font_size in font_sizes: | |
font = Font(font_name, font_size) | |
text = "This is font: {0}, size:{1}".format(font_name, font_size) | |
s = graphics.measure_string(text, font, SizeF.empty, None) | |
graphics.draw_string(text, font, brush, RectangleF(float(x), float(y), float(w), float(s.height)), string_format) | |
y += s.height | |
graphics.draw_line(pen, Point(x, y), Point((x + w), y)) | |
graphics.draw_line(pen, Point(line_x, 0), Point(line_x, y)) | |
# save all changes. | |
image.save() | |
if delete_output: | |
os.remove(output_file_name) | |
alignments = ["Left", "Center", "Right"] | |
for alignment in alignments: | |
draw_string(data_dir, alignment) |