Shape behind text per-page

Hi,

I’m aware that Aspose.Words doesn’t (yet perhaps) support pages when editing / defining a document, but I would like to implement something that we deem as necessary for our project to go forward.

Attached is a zip file containing 2 doc’s, ideal.doc is what I would ideally like to achieve, which is a shape set to be behind text on every page (1 per page), however I appreciate that this is not possible (I can not use headers to achieve this, as this document will need to be merged into another document that can not contain these shapes).
However, perhaps it is possible to achieve what I have manually done in acceptable.doc, which is to propagate a shape down a document until the end of the document is reached, however I was unable to work out how to do this in Aspose.Words. So my question is, is this possible?

Thanks for any help,

-David

Hi David,
Please give me several hours to investigate probable solutions, although honestly it is hard to find a workaround without having pagination functionality.

Thanks for the response, I appreciate this might be difficult, but any help is welcome

-David

David,
One major obstacle to implement the solution is the following. Given the page size, we can calculate relative coordinates of the floating shapes, but they need to be anchored (added) to a paragraph on the same page, and we can’t assume what paragraph it is. Currently I see 2 possible ways to research:

  1. Manually place continuous section breaks at the end of each page. This would help to mark page limits, but based on your description, this is not likely to be acceptable for you because you need to insert watermarks into an arbitrary document, don’t you?
  2. You said that placing the shapes into the header is not suitable, but maybe it still is? Can’t you programmatically remove the shapes from the header before merging the document to another document?

I can appreciate the problem without having a paging system in place, that is why I thought perhaps the second solution might be feasible.

  1. Your assumptions are correct, we have no control over the documents we recieve
  2. I will explain the reason why I think it wouldn’t be possible to use the headers in this instance:

We have one document, we then need to duplicate this document within itself (so basically if the first document was page 1 + page 2, it then becomes page 1 + page 2 + page 1 + page 2 etc) but we need to place a shape on only the duplicated pages (basically with a watermark to indicate that this is a duplicate). I would normally say that we would just create 2 documents, but unfortunatly we have clients that expect this behaviour (of one document).

If you can think of a better way to achieve this behaviour or if you still think headers are applicable, I’m all ears.

Thanks,

-David

I still can’t understand why you think headers are not feasible. If you duplicate the document by cloning and appending sections, then the resulting document will consist of at least two sections which is exactly what we need: the first section will have an empty header and the second section will have a header with floating shapes in it. Based on your words, headers seem to me the ideal solution in your case. Is there something I missed?

Problem solved. Sorry about the misunderstanding, I actually wasn’t aware that Word was capable of having seperate headers/footers per section, so my appologies for my lack of knowledge.

I can now achieve what I need using Aspose,

Thanks again,

-David

Sorry too, I would have had to clarify this point. But I’m happy that the problem is resolved now.

Is there a code sample anywhere for this?
Thanks,
Salil

Hi
Thanks for your inquiry. I think that this code will be useful for you.

// Open document
Document doc = new Document(@"447_68358_salilbhole\in.doc");
// Create an instan Ce of DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
// Move coursor to primary header
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
// Insert image into header (watermark)
builder.InsertImage(@"447_68358_salilbhole\test.jpg");
// Clone firs section
Section section = (Section)doc.FirstSection.Clone(true);
// Remove watermark from header
section.DeleteHeaderFooterShapes();
// unlink header 
section.HeadersFooters.LinkToPrevious(false);
// Add cloned section to document
doc.Sections.Add(section);
// save document
doc.Save(@"447_68358_salilbhole\out.doc");

Best regards.

Thanks Alexey for the quick response.
Here is what I am trying to acheive.

  1. Same text on all pages with different watermarks. Following is my code

It sort of working except it put both watermarks on all pages. Any help would be appreciated

protected void Button1_Click(object sender, EventArgs e)
{
    Document doc = new Document(@"C:\Program Files\Aspose\Aspose.Words\Demos\CSharp\WebForms\SomeTemplate.dot");
    InsertWatermark(doc, "", "Same Text");
    InsertWatermark(doc, "Page1", "Same Text");
    InsertWatermark(doc, "Page2", "Same Text");
    doc.Save(@"C:\Program Files\Aspose\Aspose.Words\Demos\CSharp\WebForms\TestFile.doc");
}
private static void InsertWatermark(Document doc, string text, string html)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    Shape shape = new Shape(doc, ShapeType.TextPlainText);
    shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    shape.HorizontalAlignment = HorizontalAlignment.Center;
    shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    shape.VerticalAlignment = VerticalAlignment.Center;
    Paragraph para = new Paragraph(doc);
    Run run = new Run(doc);
    Node node = builder.CurrentNode;
    run.Text = text;
    run.Font.Color = System.Drawing.Color.Red;
    para.AppendChild(run);
    shape.BehindText = true;
    shape.PrependChild(para);
    builder.InsertNode(shape);
    builder.MoveToDocumentStart();
    builder.InsertHtml(html);
    builder.InsertBreak(BreakType.PageBreak);
}

I modified your function a little.

private static void InsertWatermark(Document doc, string text, string html)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToDocumentEnd(); //move to document end
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary); //Move to header
                                                                // Prepare and insert watermark
    Shape shape = new Shape(doc, ShapeType.TextPlainText);
    shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
    shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    shape.VerticalAlignment = VerticalAlignment.Center;
    Paragraph para = new Paragraph(doc);
    Run run = new Run(doc);
    Node node = builder.CurrentNode;
    run.Text = text;
    run.Font.Color = System.Drawing.Color.Red;
    para.AppendChild(run);
    shape.BehindText = true;
    shape.PrependChild(para);
    builder.InsertNode(shape);
    //move to document end
    builder.MoveToDocumentEnd();
    //Insert text
    builder.InsertHtml(html);
    builder.InsertBreak(BreakType.SectionBreakNewPage);
    builder.CurrentSection.DeleteHeaderFooterShapes();
    builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
}

I hope that this will solve you problem.
Best regards.

Thanks Alexey,
Now it preserves the watermarks but getting rid off the Header which include text and image(s) which also needs to be preserved along with the watermark.
Thanks again for the help,
Salil

Hi
Try to use that following code

// Create clone of last section
Section section = builder.CurrentSection.Clone();
// Insert cloned sectin to and of the document
doc.Sections.Add(section);
// Delete images from header
section.DeleteHeaderFooterShapes();
// Remove content of section
section.Body.RemoveAllChildren();
// add one empty paragreph (Section.Body must have on paragraph)
section.Body.AppendChild(new Paragraph(doc));
section.HeadersFooters.LinkToPrevious(false);
instead this
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.CurrentSection.DeleteHeaderFooterShapes();
builder.CurrentSection.HeadersFooters.LinkToPrevious(false);

I hope that this will help you.