Learning Objectives
In this tutorial, you’ll learn how to:
- Adjust barcode image sizes using different sizing modes
- Rotate barcodes to specific angles
- Customize barcode borders with different styles and thicknesses
- Set proper padding around barcode elements
- Apply bar width reduction for optimal printing results
Prerequisites
Before starting this tutorial, make sure you have:
- An Aspose Cloud account (sign up for a free trial if needed)
- Your Client ID and Client Secret from the Aspose Cloud Dashboard
- Basic knowledge of REST APIs
- Completed the basic barcode generation tutorial or have equivalent knowledge
- A development environment for your preferred language (C#, Java, PHP, Python, Node.js, or Go)
Introduction
The appearance of a barcode is crucial for its readability and integration into your application or document. Aspose.BarCode Cloud API offers extensive customization options to tailor barcode appearance to your specific requirements.
In this tutorial, we’ll explore various techniques to modify the visual aspects of barcodes, ensuring they meet both aesthetic and functional needs.
Understanding Barcode Structure
A barcode image consists of several elements, each with its own position relative to others:
The main components that we can customize include:
- Barcode bars/modules (the actual encoded data)
- Human-readable text (optional)
- Borders around the barcode image
- Paddings between elements
- Overall image dimensions
Step 1: Barcode Image Sizing Modes
Aspose.BarCode Cloud provides three different sizing modes to control barcode dimensions:
AutoSizeMode.None
When using this mode, the barcode size is determined by the XDimension
parameter rather than explicitly setting height and width:
cURL Example for AutoSizeMode.None
curl -v "https://api.aspose.cloud/v3.0/barcode/autosizemode-none.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&xDimension=2" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
In this example:
xDimension=2
: Sets the minimum width of bars (for 1D barcodes) or cells (for 2D barcodes)- No explicit width or height is provided - they are calculated based on the XDimension
Try it yourself:
- Generate a barcode with different XDimension values (1, 2, 5)
- Observe how the overall barcode size changes
AutoSizeMode.Interpolation
This mode uses the explicit height and width you provide, even if it leads to distorted proportions:
cURL Example for AutoSizeMode.Interpolation
curl -v "https://api.aspose.cloud/v3.0/barcode/interpolation-mode.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&width=300&height=100&autoSizeMode=interpolation" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
Key parameters:
width=300
: Sets image width to 300 pixelsheight=100
: Sets image height to 100 pixelsautoSizeMode=interpolation
: Forces the barcode to fit the exact dimensions
AutoSizeMode.Nearest
This mode tries to find the best fit for your specified dimensions while maintaining proper barcode proportions:
cURL Example for AutoSizeMode.Nearest
curl -v "https://api.aspose.cloud/v3.0/barcode/nearest-mode.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&width=300&height=100&autoSizeMode=nearest" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
Learning checkpoint:
- What is the main difference between Interpolation and Nearest modes?
- When would you use XDimension-based sizing instead of explicit width/height?
Step 2: Rotating Barcode Images
Rotating barcodes can be essential for fitting them into specific layouts or designs:
cURL Example for Barcode Rotation
curl -v "https://api.aspose.cloud/v3.0/barcode/rotated-barcode.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&rotationAngle=90" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
Key parameter:
rotationAngle=90
: Rotates the barcode 90 degrees clockwise
Try it yourself:
Generate barcodes with different rotation angles:
- 45 degrees
- -45 degrees (counterclockwise)
- 180 degrees (upside down)
Step 3: Customizing Barcode Borders
Borders can help make your barcode stand out from the background:
cURL Example for Solid Border
curl -v "https://api.aspose.cloud/v3.0/barcode/bordered-barcode.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&borderVisible=true&borderWidth=5&borderDashStyle=Solid" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
Key parameters:
borderVisible=true
: Shows the borderborderWidth=5
: Sets border thickness to 5 pixelsborderDashStyle=Solid
: Creates a solid line border
Available Border Styles:
- Solid
- Dash
- Dot
- DashDot
- DashDotDot
Try it yourself:
Create barcodes with each of the available border styles to see the differences.
C# Example for Border Customization
// Tutorial Code Example: Customize barcode borders in C#
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace AsposeBarcodeCloudTutorial
{
class Program
{
// Replace with your actual credentials
const string ClientId = "YOUR_CLIENT_ID";
const string ClientSecret = "YOUR_CLIENT_SECRET";
const string ApiBaseUrl = "https://api.aspose.cloud/v3.0/barcode/";
const string AuthUrl = "https://api.aspose.cloud/oauth2/token";
static async Task Main(string[] args)
{
// Get the access token
var accessToken = await GetAccessToken();
// Generate barcodes with different border styles
string[] borderStyles = { "Solid", "Dash", "Dot", "DashDot", "DashDotDot" };
foreach (var style in borderStyles)
{
await GenerateBarcodeWithBorder(accessToken, style);
Console.WriteLine($"Generated barcode with {style} border");
}
}
static async Task<string> GetAccessToken()
{
// Authentication code as in previous examples
// ...
}
static async Task GenerateBarcodeWithBorder(string accessToken, string borderStyle)
{
using (var client = new HttpClient())
{
// Set authorization header
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
// Build the request URL with parameters
string fileName = $"barcode-{borderStyle}-border.png";
string requestUrl = $"{ApiBaseUrl}{fileName}/generate?text=BARCODE-BORDER&type=Code128&format=png" +
$"&borderVisible=true&borderWidth=3&borderDashStyle={borderStyle}";
// Make the PUT request to save to cloud
var response = await client.PutAsync(requestUrl, null);
response.EnsureSuccessStatusCode();
}
}
}
}
Step 4: Setting Barcode Padding
Padding controls the space between the barcode elements and the edge of the image:
cURL Example for Padding
curl -v "https://api.aspose.cloud/v3.0/barcode/padded-barcode.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&topMargin=20&bottomMargin=20&leftMargin=20&rightMargin=20" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
Key parameters:
topMargin=20
: 20-pixel padding at the topbottomMargin=20
: 20-pixel padding at the bottomleftMargin=20
: 20-pixel padding on the leftrightMargin=20
: 20-pixel padding on the right
Try it yourself:
- Create a barcode with minimal padding (1-2 pixels)
- Create a barcode with excessive padding (50+ pixels)
- Observe how padding affects the overall appearance and usability
Step 5: Bar Width Reduction for Print Optimization
When printing barcodes, ink spread can cause bars to appear thicker than intended. Bar width reduction compensates for this:
cURL Example for Bar Width Reduction
curl -v "https://api.aspose.cloud/v3.0/barcode/print-optimized.png/generate?text=PRINT-OPTIMIZED&type=Code128&format=png&resolution=300&barWidthReduction=3" \
-X PUT \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json"
Key parameter:
barWidthReduction=3
: Reduces bar width by 3 pixels to compensate for ink spread
When to use Bar Width Reduction:
- For commercial printing on offset presses
- When barcode scanning fails due to “ink floating”
- Based on printer manufacturer recommendations
Note:
Bar width reduction is typically not needed for laser printers or digital printing.
Step 6: Implementing Appearance Customization in Python
Let’s consolidate our learning into a Python example:
# Tutorial Code Example: Customize barcode appearance in Python
import requests
import json
# Replace with your actual credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
auth_url = "https://api.aspose.cloud/oauth2/token"
api_base_url = "https://api.aspose.cloud/v3.0/barcode/"
def get_access_token():
"""Get OAuth2 access token"""
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.post(auth_url, data=payload, headers=headers)
response.raise_for_status()
return response.json()['access_token']
def generate_custom_barcode(access_token, file_name, params):
"""Generate barcode with custom appearance parameters"""
# Build request URL with filename
request_url = f"{api_base_url}{file_name}/generate"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# Make PUT request to save to cloud
response = requests.put(request_url, params=params, headers=headers)
response.raise_for_status()
return response.json()
if __name__ == "__main__":
# Get access token
token = get_access_token()
# Example 1: Custom sizing with AutoSizeMode.None
params_xdimension = {
'text': 'CUSTOM-SIZE-XDIM',
'type': 'Code128',
'format': 'png',
'xDimension': 3
}
result1 = generate_custom_barcode(token, "custom-size-xdim.png", params_xdimension)
print("Generated barcode with custom XDimension")
# Example 2: Custom sizing with explicit dimensions
params_dimensions = {
'text': 'CUSTOM-DIMENSIONS',
'type': 'Code128',
'format': 'png',
'width': 400,
'height': 100,
'autoSizeMode': 'Nearest'
}
result2 = generate_custom_barcode(token, "custom-dimensions.png", params_dimensions)
print("Generated barcode with custom dimensions")
# Example 3: Rotated barcode
params_rotated = {
'text': 'ROTATED-BARCODE',
'type': 'Code128',
'format': 'png',
'rotationAngle': 45
}
result3 = generate_custom_barcode(token, "rotated-barcode.png", params_rotated)
print("Generated rotated barcode")
# Example 4: Custom border
params_border = {
'text': 'BORDERED-BARCODE',
'type': 'Code128',
'format': 'png',
'borderVisible': True,
'borderWidth': 5,
'borderDashStyle': 'DashDot'
}
result4 = generate_custom_barcode(token, "bordered-barcode.png", params_border)
print("Generated barcode with custom border")
# Example 5: Custom padding
params_padding = {
'text': 'PADDED-BARCODE',
'type': 'Code128',
'format': 'png',
'topMargin': 30,
'bottomMargin': 30,
'leftMargin': 30,
'rightMargin': 30
}
result5 = generate_custom_barcode(token, "padded-barcode.png", params_padding)
print("Generated barcode with custom padding")
print("All custom barcodes generated successfully!")
Troubleshooting Common Issues
Issue: Barcode not scanning correctly after appearance customization
- Possible cause: Excessive rotation or incorrect bar width reduction
- Solution: Ensure rotation is in increments of 90° for best results or adjust bar width reduction value
Issue: Image distortion when using AutoSizeMode.Interpolation
- Possible cause: Extreme aspect ratio differences between width and height
- Solution: Use AutoSizeMode.Nearest instead or maintain proportional dimensions
Issue: Border not appearing as expected
- Possible cause: Too small border width or incorrect dash style
- Solution: Increase border width and verify the dash style parameter is correctly spelled
What You’ve Learned
Congratulations! In this tutorial, you’ve learned how to:
- Control barcode image sizing using different methods (XDimension vs. explicit dimensions)
- Rotate barcodes to meet design requirements
- Add and customize borders around barcodes
- Set proper padding for optimal barcode presentation
- Apply bar width reduction for print optimization
- Implement these customizations programmatically
Further Practice
To reinforce your learning:
- Create a series of barcodes with different appearance settings and test their scannability
- Experiment with combining multiple appearance customizations (e.g., rotation + borders + padding)
- Create a simple application that allows users to customize barcode appearance through a UI
- Test bar width reduction with actual printed barcodes on different printers
Next Steps
Now that you’ve learned how to customize barcode appearance, you might want to explore how to manage barcode text and captions:
Tutorial: How to Manage Barcode Text and Captions
Helpful Resources
Have questions about this tutorial? Feel free to post them on our support forum!