Frequently Asked Questions

Add images, shapes, and text to the PDF file [Step-by-step]
Last Updated 2 years ago

When there is a need to create a PDF file with specifically positioned shapes, text, and images Aspose.PDF is a perfect tool to use.

The customer had a request to create a PDF document that would contain images, a description of the image, and cut-off lines, so we provided him with support that included the following steps:

1. Creating the PDF file and adding an empty page
2. Adding an image and positioning it on the page
3. Adding rotated text to a PDF page
4. Adding lines to PDF file

We will describe each of these steps in detail below.

Creating the PDF file and adding an empty page

To create a PDF file using Aspose.PDF for Java we will first create an object of the Document class and then add a Page object to the Pages collection of the Document object.

Since the customer needed margins to be set to '0' we have set them using PageInfo class which represents the page information.

//Create an object of Document class
Aspose.Pdf.Document document = new Aspose.Pdf.Document();

//Add a page to newly created file
Page page = document.Pages.Add();
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Bottom = 0;

Adding an image and positioning it on the page

To add an image to a page in a PDF file we first need to add that image to the page’s Resources collection and then use the GSave operator to save the current graphical state:

FileStream image1 = new FileStream("Image1.jpg", FileMode.Open);

//Add the image to new page
page.Resources.Images.Add(image1);
page.Contents.Add(new Aspose.Pdf.Operators.GSave());
Afterward, we can use the ConcatenateMatrix operator to specify where the image is to be placed, draw the image on the page using the Do operator and finally save the updated graphical state using GRestore operator.
Since the customer wanted the final output to contain two images positioned 35 pixels from left and right we will create a Rectangle with mentioned dimensions:

Aspose.Pdf.Rectangle rectangle1 = new Aspose.Pdf.Rectangle(35, height / 2, width - 35, height - 20);
Aspose.Pdf.Matrix matrix1 = new Aspose.Pdf.Matrix(new double[] { rectangle1.URX - rectangle1.LLX, 0, 0, rectangle1.URY - rectangle1.LLY, rectangle1.LLX, rectangle1.LLY });
page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix1));
XImage ximage1 = page.Resources.Images[page.Resources.Images.Count];
page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage1.Name));
page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
The result of the above is the file with the following content:

image

Adding rotated text to a PDF page

The customer had a request to add a rotated text that is placed next to the image. This can be achieved using TextStamp, TextStamp class provides properties necessary to create a text-based stamp-like font size, font style, font color, rotation, etc:

TextStamp stamp = new TextStamp("Windows 7 background");
stamp.RotateAngle = 90;

//Position the TextStamp relative to the rectangle that contains the image
stamp.XIndent = rectangle1.URX + lineMargin;
stamp.YIndent = rectangle1.URY - 100 - lineMargin;
page.AddStamp(stamp);

TextStamp stamp1 = new TextStamp("1/2");
stamp1.RotateAngle = 90;
stamp1.XIndent = rectangle1.URX + lineMargin;
stamp1.YIndent = (float)rectangle1.LLY + lineMargin + 20;
page.AddStamp(stamp1);

Adding lines to PDF file

The third requirement in the project was adding lines to the PDF file, which, in customers scenario were used as cut-off lines.

First, we will create a Graph instance that spans the entire page height and add it to page paragraphs:

// Create Graph instance
var graph = new Aspose.Pdf.Drawing.Graph(0, (float)page.PageInfo.Height);

// Add graph object to paragraphs collection of a page instance
page.Paragraphs.Add(graph);

The customer wanted to create 4 lines per image and one with a higher thickness at the top of the page:

To achieve this we can use Apose.Pdf.Drawing.Line class to position the lines relative to the rectangle that contains the image.

//Length of the line
var lineWidth = 10;
//Distance between
var lineMargin = 10;

//Add the line to top left corner of the page
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { 25, (float)page.PageInfo.Height - 15, 100, (float)page.PageInfo.Height - 15 });
//Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(line);
//Change the line width
line.GraphInfo.LineWidth = 4;

//Create a bottom left line
Aspose.Pdf.Drawing.Line line1 = new Aspose.Pdf.Drawing.Line(new float[] { (float)rectangle1.LLX - lineWidth - lineMargin, (float)rectangle1.LLY + lineMargin, (float)rectangle1.LLX - lineMargin, (float)rectangle1.LLY + lineMargin });
//Create top right line
Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { (float)rectangle1.URX + lineWidth + lineMargin, (float)rectangle1.URY - lineMargin, (float)rectangle1.URX + lineMargin, (float)rectangle1.URY - lineMargin }); //Add the line to bottom left corner of the page
//Create top left line
Aspose.Pdf.Drawing.Line line3 = new Aspose.Pdf.Drawing.Line(new float[] { (float)rectangle1.LLX - lineWidth - lineMargin, (float)rectangle1.LLY + (float)rectangle1.URY - (float)rectangle1.LLY - lineMargin, (float)rectangle1.LLX - lineMargin, (float)rectangle1.LLY + (float)rectangle1.URY - (float)rectangle1.LLY - lineMargin }); //Add the line to bottom left corner of the page
//Create bottom left line
Aspose.Pdf.Drawing.Line line4 = new Aspose.Pdf.Drawing.Line(new float[] { (float)rectangle1.URX + lineMargin + lineWidth, (float)rectangle1.LLY + lineMargin, (float)rectangle1.URX + lineMargin, (float)rectangle1.LLY + lineMargin }); //Add the line to bottom left corner of the page

// Add rectangle object to shapes collection of Graph object
graph.Shapes.Add(line1);
graph.Shapes.Add(line2);
graph.Shapes.Add(line3);
graph.Shapes.Add(line4);

The final result is a one-page PDF file that contains all the required content:

image

Please Wait!

Please wait... it will take a second!