Add Tiled Image in Java XPS

Introduction

In the dynamic world of Java development, the need for efficient document manipulation and creation is ever-growing. Aspose.Page for Java emerges as a powerful tool, providing developers with the capability to work with XPS documents seamlessly. This tutorial focuses on a specific task – adding a tiled image to a Java XPS document.

Prerequisites

Before diving into the tutorial, ensure that you have the following prerequisites in place:

  1. Java Development Kit (JDK): Make sure you have JDK installed on your system.
  2. Aspose.Page for Java: Download and install Aspose.Page for Java from the website.
  3. Your Document Directory: Choose or create a directory where you want to save your XPS document.

Import Packages

In your Java project, import the necessary packages to utilize Aspose.Page functionalities:

import com.aspose.xps.XpsDocument;
import com.aspose.xps.XpsImageBrush;
import com.aspose.xps.XpsPath;
import com.aspose.xps.XpsTileMode;
import java.awt.geom.Rectangle2D;

Now, let’s break down the process of adding a tiled image to a Java XPS document into clear, manageable steps.

Step 1: Set Up Your Project

Start by setting up your Java project, ensuring that Aspose.Page for Java is properly integrated.

Step 2: Create XPS Document

Initialize a new XPS document using the following code:

// The path to the documents directory.
String dataDir = "Your Document Directory";
// Create new XPS Document
XpsDocument doc = new XpsDocument();

Step 3: Define Tiled Image Path

Specify the path to the tiled image you want to add to the XPS document.

Step 4: Add Tiled Image

Utilize the code snippet below to add a tiled image to the XPS document:

// Tile image
// ImageBrush filled rectangle in the right top below
XpsPath path = doc.addPath(doc.createPathGeometry("M 10,160 L 228,160 228,305 10,305"));
path.setFill(doc.createImageBrush(dataDir +  "R08LN_NN.jpg",
                                new Rectangle2D.Float(0f, 0f, 128f, 96f), new Rectangle2D.Float(0f, 0f, 64f, 48f)));
((XpsImageBrush)path.getFill()).setTileMode(XpsTileMode.Tile);
path.getFill().setOpacity(0.5f);

Step 5: Save the Document

Finally, save the resultant XPS document using the code below:

// Save resultant XPS document
doc.save(dataDir + "AddTiledImage_out.xps"); 

Repeat these steps to effortlessly incorporate a tiled image into your Java XPS document using Aspose.Page.

Conclusion

Aspose.Page for Java streamlines the process of working with XPS documents, offering developers an efficient solution for document manipulation. By following this step-by-step guide, you can effortlessly add a tiled image to your Java XPS document.

FAQs

Is Aspose.Page compatible with all Java versions?

Aspose.Page is designed to work with various Java versions. Ensure compatibility by checking the documentation here.

Can I use Aspose.Page for commercial projects?

Yes, Aspose.Page offers commercial licenses. Purchase them here.

Is there a free trial available?

Yes, explore Aspose.Page features with a free trial here.

Where can I find community support and discussions?

Engage with the Aspose.Page community on the forum.

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

Acquire a temporary license here.