Insert an image into a document using Aspose.Words for Java

Hello,

I’m trying to insert an bar code image into a document. But I can’t figure out how to format the image and the sections to meet my requirements. The image and the subsequent text section are either over lapping or the section is on the next new page. I need to put the image in the first section and subsequent text sections should continue after the image. I attached my test code the documents I generated. Can anybody help out? Thanks. - Jessica

Attached files:

  1. Sections.java - test code

  2. doc.doc - image and text section are overlapping

  3. doc1.doc - text section starts on a new page

  4. doc3.doc - required document

Hi Jessica,
Thanks for your inquiry.
To get your document to appear as you want you need to remove the call to shape.isBehindText(true); as this will make the image behind text and the text wrap over it. The page should then appear properly.
Thanks,

Dear Jessica,

To amplify Adam’s message I would like to provide you with code sample how to generate desired otput (doc3.doc) document using DocumentBuilder:

DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(MyDir + "barcode.bmp");
shape.setWrapType(WrapType.TOP_BOTTOM);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PARAGRAPH);
shape.setVerticalAlignment(VerticalAlignment.TOP);
Font font = builder.getFont();
font.setSize(16);
font.setColor(Color.BLUE);
font.setName("Arial");
builder.insertParagraph();
builder.insertParagraph();
builder.write(text2);
doc.save(MyDir + "out.docx");

Hope you find this useful.

Dear Svetlana,

Thank you so much for your help. It worked. But I have some other issues. After inserting the image, I managed to get the mail merge working. But I couldn’t insert text after the mail merge. Also I don’t know how to insert the image on every page. Can you please help out? I attached test code and related documents.

Attached files:

  1. SectionTest.java - test code

  2. doc1.doc - generated doc from SectionTest.java but it’s not quite right

  3. doc.doc - required document

  4. Template.doc - template used in the code

Thank you!

Jessica

Hi Jessica,
Thanks for your inquiry.

  1. To insert an image on every page, you can out it into the document header. For instance, you can try using the same technique as demonstrated here for inserting watermark into the document:
    https://docs.aspose.com/words/java/working-with-watermark/
  2. To insert text after executing mail merge, you can use DocumentBuilder. Just move DocumentBuilder cursor to the end of the document and use Write or WriteLine method to insert text.

Best regards,

A post was split to a new topic: Insert image logo into PDF file