Free Support Forum - aspose.com

How i can replicate my shape TextBox?

I have an shape TextBox and works well for the first page but i cannot replicate for the other pages; How i can replicate my shape for all pages of documents? Thank you in advance

Hi Antonio,


Thanks for your inquiry.

Sure, to achieve this, you can easily insert any Shape (TextBox) or image into a header or footer. I believe, you can achieve what you need after reading the article suggested below:
http://www.aspose.com/docs/display/wordsnet/How+to++Add+a+Watermark+to+a+Document

Please let me know if I can be of any further assistance.

Best regards,

Hi,

Thank you for the feedback.

But the problem is that i created an TextBox in margin of first page and i want replicate for all pages in document, but i cannot use header and footer or watermark. Do you think that's possible?

Below i put the code that i have and works well for the first page

public void insertInLeftMarginInDocument(File file, int typeModels, List valuesXML){

try {

FileInputStream fileInputStream =

new FileInputStream(file);

com.aspose.words.Document doc =

new com.aspose.words.Document(fileInputStream);

DocumentBuilder builder =

new DocumentBuilder(doc);

PageSetup ps = doc.getFirstSection().getPageSetup();

Shape textBox;

Run run;

Paragraph para;

System.

out.println("Number of pages: " + builder.getDocument().getCount());

textBox =

new Shape(doc, ShapeType.TEXT_BOX);

textBox.setWrapType(WrapType.

NONE);

textBox.setLeft(0);

textBox.setHeight(ps.getPageHeight());

textBox.setWidth((ps.getLeftMargin())-3);

textBox.setFilled(true);

textBox.setStroked(

false);

textBox.setVerticalAlignment(VerticalAlignment.

CENTER);

textBox.setHorizontalAlignment(HorizontalAlignment.

LEFT);

textBox.setRelativeHorizontalPosition(RelativeHorizontalPosition.

PAGE);

textBox.setRelativeVerticalPosition(RelativeVerticalPosition.

PAGE);

textBox.appendChild(

new Paragraph(doc));

textBox.getTextBox().setLayoutFlow(LayoutFlow.

TOP_TO_BOTTOM);

para = textBox.getFirstParagraph();

para.getParagraphFormat().setAlignment(ParagraphAlignment.

LEFT);

run =

new Run(doc);

String valuesModel = getModel(typeModels);

valuesModel = valuesModel +

" " + valuesXML.get(0) + valuesXML.get(1) + valuesXML.get(2);

CheckDigit checkDigit =

new CheckDigit();

if(!checkDigit.verifyNumberDigits(valuesModel))

throw new BarcodeException("The number of CheckDigit is not correct");

String verifyCheckDigit = checkDigit.verifyCheckDigit(valuesModel);

String valuesAfterCheckDigit = checkDigit.convertNumberToStringBarcode(verifyCheckDigit);

run.setText(valuesAfterCheckDigit);

run.getFont().setBold(

true);

run.getFont().setName(PropertiesUtil.getInstance().getPropertiesFile(

"barCodeComponent.properties").getProperty("name.font"));

run.getFont().setSize(Double.valueOf(PropertiesUtil.getInstance().getPropertiesFile(

"barCodeComponent.properties").getProperty("size.font", "8")));

para.appendChild(run);

doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox);

builder.getDocument().save(file.getAbsolutePath());

}

catch (BarcodeException barcodeException) {

barcodeException.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

Best regards,

Antonio Nobre

Hi Antonio,

Thanks for the additional information.
Antonio:
But the problem is that i created an TextBox in margin of first page and i want replicate for all pages in document, but i cannot use header and footer or watermark. Do you think that's possible?
Sure, you can loop through each page in your document and insert Shapes with the help of newly introduced LayoutCollector class. Please try using the following code snippet. I hope, this helps.
// This a document that we want to add an image and custom text for each page without using the header or footer.
Document doc = new Document("C:\\Temp\\TestFile.doc");

// Create and attach collector before the document before page layout is built.
LayoutCollector layoutCollector = new LayoutCollector(doc);

// Images in a document are added to paragraphs, so to add an image to every page we need to find at any paragraph
// belonging to each page.
Iterator iterator = doc.selectNodes("//Body/Paragraph").iterator();

// Loop through each document page.
for (int page = 1; page <= doc.getPageCount(); page++)
{
while (iterator.hasNext())
{
// Check if the current paragraph belongs to the target page.
Paragraph paragraph = (Paragraph)iterator.next();
if (layoutCollector.getStartPageIndex(paragraph) == page)
{
AddImageToPage(paragraph, page);
break;
}
}
}

doc.save(“C:\Temp\TestFile Out.docx”);


/**
  • Adds an image to a page using the supplied paragraph.

  • @param para The paragraph to an an image to.

  • @param page The page number the paragraph appears on.
    */


public static void AddImageToPage(Paragraph para, int page)
{
Document doc = (Document)para.getDocument();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(para);

try
{
// Add a logo to the top left of the page. The image is placed infront of all other text.
Shape shape = builder.insertImage(“C:\Temp\Aspose Logo.png”, RelativeHorizontalPosition.PAGE, 60,
RelativeVerticalPosition.PAGE, 60, -1, -1, WrapType.NONE);
}
catch (Exception e)
{
e.printStackTrace();
}
// Add a textbox which contains some text consisting of the page number.
Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);

// We want a floating shape relative to the page.
textBox.setWrapType(WrapType.NONE);
textBox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
textBox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

// Set the textbox position.
textBox.setHeight(30);
textBox.setWidth(200);
textBox.setLeft(150);
textBox.setTop(80);

// Add the textbox and set text.
textBox.appendChild(new Paragraph(doc));
builder.insertNode(textBox);
builder.moveTo(textBox.getFirstChild());
builder.writeln("This is a custom note for page " + page);
}

PS: Please find attached the sample documents and image file.

Best regards,

Hi,

Thak you for your feedback.

I Think this could be a solution for this issue, but i use the Aspose.words version words-jdk16-11.8.jar and this jar doesn't have the class. Where i can download with this newly class?

Thank for you in advance,

Best regards,

Eduardo

Hi Eduardo,


Thanks for your inquiry. Please upgrade to the latest version of Aspose.Words for Java (13.1.0) from the following link:
http://www.aspose.com/community/files/72/java-components/aspose.words-for-java/default.aspx

If we can help you with anything else, please feel free to ask.

Best regards,