Using Java code, I want to make this shape, Here shape color (blue), and text (testcase1,testcase2) and image should be dynamically added from java, can you send me an example for same?
I have attached image for reference
@kalpeshAspose1997 the following code will help you to achieve what you want, you just need to set the right sizes, positions and colors. Please notice that the code is in C#, when I have it ready in java I will post it here (translation is not too hard if you want to start doing it by yourself):
Document doc = new Document();
doc.RemoveAllChildren();
var builder = new DocumentBuilder(doc);
GroupShape gs = new GroupShape(doc);
var bigShape = new Shape(doc, ShapeType.Rectangle)
{
WrapType = WrapType.Inline,
StrokeColor = Color.Black,
FillColor = Color.Blue,
Height = 70,
Width = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.LeftMargin - builder.CurrentSection.PageSetup.RightMargin,
};
gs.AppendChild(bigShape);
int smallShape1Top = (int)(bigShape.Top + 5);
int smallShape1Left = (int)(bigShape.Left + bigShape.Width - 115);
var smallShape1 = new Shape(doc, ShapeType.Rectangle)
{
WrapType = WrapType.Square,
StrokeColor = Color.Black,
FillColor = Color.DeepSkyBlue,
Height = 30,
Width = 100,
Left = smallShape1Left,
Top = smallShape1Top,
CoordOrigin = new Point(smallShape1Top, smallShape1Left),
Bounds = new RectangleF(smallShape1Left, smallShape1Top, 100, 30),
TextBox = { FitShapeToText = true, TextBoxWrapMode = TextBoxWrapMode.Square },
};
var paragraph1 = new Paragraph(doc);
paragraph1.AppendChild(new Run(doc, "Testcase 1"));
smallShape1.ChildNodes.Add(paragraph1);
int smallShape2Top = (int)(bigShape.Top + bigShape.Height - 35);
int smallShape2Left = (int)(bigShape.Left + bigShape.Width - 115);
var smallShape2 = new Shape(doc, ShapeType.Rectangle)
{
WrapType = WrapType.Square,
StrokeColor = Color.Black,
FillColor = Color.Teal,
Height = 30,
Width = 100,
Left = smallShape2Left,
Top = smallShape2Top,
CoordOrigin = new Point(smallShape2Top, smallShape2Left),
Bounds = new RectangleF(smallShape2Left, smallShape2Top, 100, 30),
TextBox = { FitShapeToText = true, TextBoxWrapMode = TextBoxWrapMode.Square },
};
var paragraph2 = new Paragraph(doc);
paragraph2.AppendChild(new Run(doc, "Testcase 2"));
smallShape2.ChildNodes.Add(paragraph2);
Shape img = new Shape(doc, ShapeType.Image)
{
Left = 10,
WrapType = WrapType.Square,
Top = 5,
CoordOrigin = new Point(5, 10),
Bounds = new RectangleF(10, 5, 100, 60),
BehindText = false,
Width = 100,
Height = 60,
};
img.ImageData.SetImage(@"C:\Temp\image.png");
gs.AppendChild(smallShape1);
gs.AppendChild(smallShape2);
gs.AppendChild(img);
gs.CoordSize = new Size((int)bigShape.Width, (int)bigShape.Height);
gs.Height = bigShape.Height;
gs.Width = bigShape.Width;
builder.InsertNode(gs);
doc.Save(@"C:\Temp\output.docx");
C# code not useful for me, I want to make shape color (blue), and text (testcase1,testcase2) and image(left side ‘Square’ text image) should be dynamically added using the java, so can you send me java code once done from your end.
thanks,
@kalpeshAspose1997 this is the solution in Java:
Document doc = new Document();
doc.removeAllChildren();
DocumentBuilder builder = new DocumentBuilder(doc);
GroupShape gs = new GroupShape(doc);
Shape bigShape = new Shape(doc, ShapeType.RECTANGLE);
bigShape.setWrapType(WrapType.INLINE);
bigShape.setStrokeColor(Color.BLACK);
bigShape.setFillColor(Color.BLUE);
bigShape.setHeight(70);
bigShape.setWidth(builder.getCurrentSection().getPageSetup().getPageWidth() - builder.getCurrentSection().getPageSetup().getLeftMargin() - builder.getCurrentSection().getPageSetup().getRightMargin());
gs.appendChild(bigShape);
int smallShape1Top = (int)(bigShape.getTop() + 5);
int smallShape1Left = (int)(bigShape.getLeft() + bigShape.getWidth() - 115);
Shape smallShape1 = new Shape(doc, ShapeType.RECTANGLE);
smallShape1.setWrapType(WrapType.SQUARE);
smallShape1.setStrokeColor(Color.BLACK);
smallShape1.setFillColor(Color.DEEPSKYBLUE);
smallShape1.setHeight(30);
smallShape1.setWidth(100);
smallShape1.setLeft(smallShape1Left);
smallShape1.setTop(smallShape1Top);
smallShape1.setCoordOrigin(new Point(smallShape1Top, smallShape1Left));
smallShape1.setBounds(new java.awt.geom.Rectangle2D.Float(smallShape1Left, smallShape1Top, 100, 30));
smallShape1.getTextBox().setFitShapeToText(true);
smallShape1.getTextBox().setTextBoxWrapMode(TextBoxWrapMode.SQUARE);
Paragraph paragraph1 = new Paragraph(doc);
paragraph1.appendChild(new Run(doc, "Testcase 1"));
smallShape1.appendChild(paragraph1);
int smallShape2Top = (int)(bigShape.getTop() + bigShape.getHeight() - 35);
int smallShape2Left = (int)(bigShape.getLeft() + bigShape.getWidth() - 115);
Shape smallShape2 = new Shape(doc, ShapeType.RECTANGLE);
smallShape2.setWrapType(WrapType.SQUARE);
smallShape2.setStrokeColor(Color.BLACK);
smallShape2.setFillColor(Color.TEAL);
smallShape2.setHeight(30);
smallShape2.setWidth(100);
smallShape2.setLeft(smallShape2Left);
smallShape2.setTop(smallShape2Top);
smallShape2.setCoordOrigin(new Point(smallShape2Top, smallShape2Left));
smallShape2.setBounds(new java.awt.geom.Rectangle2D.Float(smallShape2Left, smallShape2Top, 100, 30));
smallShape2.getTextBox().setFitShapeToText(true);
smallShape2.getTextBox().setTextBoxWrapMode(TextBoxWrapMode.SQUARE);
Paragraph paragraph2 = new Paragraph(doc);
paragraph2.appendChild(new Run(doc, "Testcase 2"));
smallShape2.appendChild(paragraph2);
Shape img = new Shape(doc, ShapeType.IMAGE);
img.setLeft(10);
img.setWrapType(WrapType.SQUARE);
img.setTop(5);
img.setCoordOrigin(new Point(5, 10));
img.setBounds(new java.awt.geom.Rectangle2D.Float(10, 5, 100, 60));
img.setBehindText(false);
img.setWidth(100);
img.setHeight(60);
try {
img.getImageData().setImage("C:\Temp\image.png");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
gs.appendChild(smallShape1);
gs.appendChild(smallShape2);
gs.appendChild(img);
gs.setCoordSize(new Size((int)bigShape.getWidth(), (int)bigShape.getHeight()));
gs.setHeight(bigShape.getHeight());
gs.setWidth(bigShape.getWidth());
builder.insertNode(gs);
doc.save("C:\Temp\output.docx");
Okay Thanks my friend, will check and let you know if any need
It’s looking good but header is not exact same as required so can you change according that in java code and send to me, I have attached sample example, let me know if any doubt
@kalpeshAspose1997 looks like the already posted solution provides an output very similar to what you want, you just need to set the desired sizes and select the right colors.
Additionally to include the shape in the header you can use the following code:
// Replace this line
builder.insertNode(gs);
// With the followings
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.insertNode(gs);
Also you can modify the top margin and the Header distance using the PageSetup property of the sections.