Add Grid using Visual Brush in Java

Introduction

Are you looking to enhance your Java applications with visually appealing grids using Aspose.Page? In this tutorial, we’ll guide you through the process of adding a grid using Visual Brush in Java with Aspose.Page. Visual Brush allows you to paint an area with a visual content, creating stunning grid effects in your documents.

Prerequisites

Before we dive into the tutorial, make sure you have the following prerequisites:

  • Basic understanding of Java programming.
  • Aspose.Page library installed. You can download it from the Aspose.Page for Java documentation.
  • Java Development Kit (JDK) installed on your machine.

Import Packages

Ensure you have the necessary packages imported in your Java project:

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import com.aspose.xps.XpsCanvas;
import com.aspose.xps.XpsDocument;
import com.aspose.xps.XpsPath;
import com.aspose.xps.XpsPathGeometry;
import com.aspose.xps.XpsTileMode;
import com.aspose.xps.XpsVisualBrush;

Let’s break down the process into multiple steps to make it easier for you to follow.

Step 1: Set Up Your Project

String dataDir = "Your Document Directory";
XpsDocument doc = new XpsDocument();

Step 2: Create Magenta Grid Visual Brush

XpsCanvas visualCanvas = doc.createCanvas();
XpsPath visualPath = visualCanvas.addPath(doc.createPathGeometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"));
visualPath.setFill(doc.createSolidColorBrush(doc.createColor(1f, .61f, 0.1f, 0.61f)));

Step 3: Define Geometry for Magenta Grid Visual Brush

XpsPathGeometry pathGeometry = doc.createPathGeometry();
pathGeometry.addSegment(doc.createPolyLineSegment(new Point2D.Float[] {
    new Point2D.Float(240f, 5f),
    new Point2D.Float(240f, 310f),
    new Point2D.Float(0f, 310f)
}));
pathGeometry.get(0).setStartPoint(new Point2D.Float(0f, 5f));

Step 4: Create New Canvas

XpsCanvas canvas = doc.addCanvas();
canvas.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 268f, 70f));

Step 5: Add Grid to Canvas

XpsPath gridPath = canvas.addPath(pathGeometry);
gridPath.setFill(doc.createVisualBrush(visualCanvas,
    new Rectangle2D.Float(0f, 0f, 10f, 10f), new Rectangle2D.Float(0f, 0f, 10f, 10f)));
((XpsVisualBrush)gridPath.getFill()).setTileMode(XpsTileMode.Tile);

Step 6: Add Red Transparent Rectangle

XpsPath path = canvas.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
path.setFill(doc.createSolidColorBrush(doc.createColor(1.0f, 0.0f, 0.0f)));
path.setOpacity(0.7f);

Step 7: Save Resultant XPS Document

doc.save(dataDir + "AddGrid_out.xps");

Follow these steps, and you’ll successfully add a visually appealing grid using Visual Brush in your Java application with Aspose.Page.

Conclusion

Congratulations! You’ve learned how to leverage Aspose.Page for Java to add grids using Visual Brush. Enhance your document visuals effortlessly with this powerful feature.

Frequently Asked Questions

Is Aspose.Page suitable for professional document generation?

Yes, Aspose.Page is a robust library designed for professional document generation in Java.

Can I customize the grid colors using Visual Brush?

Absolutely! Visual Brush allows you to paint with various colors, providing flexibility in customization.

Where can I find additional support for Aspose.Page?

Visit the Aspose.Page forum for community support and discussions.

Is there a free trial available for Aspose.Page?

Yes, you can access the free trial to explore Aspose.Page’s features.

How can I obtain a temporary license for Aspose.Page?

Acquire a temporary license for testing purposes.