Skip to content

Commit 5b7ddf7

Browse files
author
Evgeniy Sidenko
committed
Updated upto version 22.2
1 parent 731c78d commit 5b7ddf7

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

Examples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.aspose</groupId>
55
<artifactId>imaging-java-examples</artifactId>
6-
<version>21.12</version>
6+
<version>22.2</version>
77
<packaging>jar</packaging>
88
<properties>
99
<maven.compiler.source>1.8</maven.compiler.source>
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>com.aspose</groupId>
1717
<artifactId>aspose-imaging</artifactId>
18-
<version>21.12</version>
18+
<version>22.2</version>
1919
<classifier>jdk16</classifier>
2020
<type>jar</type>
2121
</dependency>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.aspose.imaging.examples.ModifyingImages;
2+
3+
import com.aspose.imaging.*;
4+
import com.aspose.imaging.brushes.SolidBrush;
5+
import com.aspose.imaging.examples.Logger;
6+
import com.aspose.imaging.examples.Path;
7+
import com.aspose.imaging.examples.Utils;
8+
import com.aspose.imaging.imageoptions.PngOptions;
9+
import com.aspose.imaging.sources.FileCreateSource;
10+
11+
public class PixelPerfectTextAlignment
12+
{
13+
public static void main(String[] args)
14+
{
15+
Logger.startExample();
16+
String baseFolder = Utils.getSharedDataDir() + "CDR";
17+
String[] alignments = {"Left", "Center", "Right"};
18+
for (String alignment : alignments)
19+
{
20+
drawString(baseFolder, alignment);
21+
}
22+
Logger.endExample();
23+
}
24+
25+
private static void drawString(String baseFolder, String align)
26+
{
27+
String fileName = "output_" + align + ".png";
28+
String outputFileName = Path.combine(baseFolder, fileName);
29+
String[] fontNames =
30+
{
31+
"Arial", "Times New Roman",
32+
"Bookman Old Style", "Calibri", "Comic Sans MS",
33+
"Courier New", "Microsoft Sans Serif", "Tahoma",
34+
"Verdana", "Proxima Nova Rg"
35+
};
36+
37+
float[] fontSizes = new float[]{10f, 22f, 50f, 100f};
38+
int width = 3000;
39+
int height = 3500;
40+
41+
//Create an instance of PngOptions and set its various properties
42+
try (PngOptions pngOptions = new PngOptions())
43+
{
44+
//Set the Source for PngOptions
45+
pngOptions.setSource(new FileCreateSource(outputFileName));
46+
47+
//Create an instance of Image
48+
try (Image image = Image.create(pngOptions, width, height))
49+
{
50+
//Create and initialize an instance of Graphics class
51+
Graphics graphics = new Graphics(image);
52+
// To speed up the drawing operations
53+
graphics.beginUpdate();
54+
55+
//Clear Graphics surface
56+
graphics.clear(Color.getWhite());
57+
58+
//Create a SolidBrush object and set its various properties
59+
SolidBrush brush = new SolidBrush();
60+
brush.setColor(Color.getBlack());
61+
float x = 10;
62+
int lineX;
63+
float y = 10;
64+
float w = width - 20;
65+
Pen pen = new Pen(Color.getRed(), 1);
66+
67+
int alignment;
68+
switch (align)
69+
{
70+
case "Left":
71+
alignment = StringAlignment.Near;
72+
lineX = Math.round(x);
73+
break;
74+
75+
case "Center":
76+
alignment = StringAlignment.Center;
77+
lineX = Math.round(x + w / 2f);
78+
break;
79+
80+
case "Right":
81+
alignment = StringAlignment.Far;
82+
lineX = (int) (x + w);
83+
break;
84+
default:
85+
throw new IllegalArgumentException("Wrong alignment: " + align);
86+
}
87+
88+
StringFormat stringFormat = new StringFormat(StringFormatFlags.ExactAlignment);
89+
stringFormat.setAlignment(alignment);
90+
for (String fontName : fontNames)
91+
{
92+
for (float fontSize : fontSizes)
93+
{
94+
Font font = new Font(fontName, fontSize);
95+
String text = String.format("This is font: %s, size:%d", fontName, (int) fontSize);
96+
SizeF s = graphics.measureString(text, font, SizeF.getEmpty(), null);
97+
graphics.drawString(text, font, brush, new RectangleF(x, y, w, s.getHeight()), stringFormat);
98+
99+
y += s.getHeight();
100+
}
101+
102+
graphics.drawLine(pen, new Point((int) (x), (int) y), new Point((int) (x + w), (int) y));
103+
}
104+
105+
graphics.drawLine(pen, new Point(lineX, 0), new Point(lineX, (int) y));
106+
107+
graphics.endUpdate();
108+
109+
// save all changes.
110+
image.save();
111+
}
112+
}
113+
114+
Path.deleteFile(outputFileName);
115+
}
116+
}

Examples/src/main/java/com/aspose/imaging/examples/RunExamples.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public static void main(String[] args) throws IOException, InterruptedException,
113113
//// =====================================================
114114

115115
Logger.println("Running modifying and converting images tests:");
116+
PixelPerfectTextAlignment.main(args);
116117
FileExtensionAwareSave.main(args);
117118
ImageScopedFonts.main(args);
118119
BmpRLE4.main(args);

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2001-2020 Aspose Pty Ltd
3+
Copyright (c) 2001-2022 Aspose Pty Ltd
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)