Free Support Forum - aspose.com

Add image to word document's each page

Hi there. I am trying to add image to every page of word document. My code is:

///


///
///

/// Source word document
/// Array of images (for each page)
public byte[] Update(byte[] content, List<byte[]> updateContent)
{
MemoryStream originalStream = new MemoryStream(content);
MemoryStream resultStream = new MemoryStream();

try
{
Document doc = new Document(originalStream);
DocumentBuilder docBuilder = new DocumentBuilder(doc);

for (int i = 0; i < doc.PageCount; i++)
{
docBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);

Shape image = docBuilder.InsertImage(updateContent[i]);
image.WrapType = WrapType.None;
image.BehindText = false;
image.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
image.HorizontalAlignment = HorizontalAlignment.Center;
image.RelativeVerticalPosition = RelativeVerticalPosition.Page;
image.VerticalAlignment = VerticalAlignment.Center;
}

doc.Save(resultStream, SaveFormat.Doc);
return resultStream.ToArray();
}
catch (Exception exc)
{
throw new Exception(String.Format(“Не удалось конвертировать Word файл: {0}”, exc.Message));
}
finally
{
originalStream.Dispose();
resultStream.Dispose();
}
}

Nothing failes, but I receive a document without images…
Thank you

P.S. Why there is no ‘code’ button in WYSIWYG editor?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the code provided in the following article:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-add-a-watermark-to-a-document.html

Hope this helps.

Best regards,

Thanks for your reply. This code is working fine, but what I need is to add different images to appropriate page, so I need to go throw each page… And the second problem - every image that I have to add should be above the any document’s content. Here is the code that i rewrited:

Document doc = new Document(sourceStream);
DocumentBuilder docBuilder = new DocumentBuilder(doc);

for (int i = 0; i < doc.Sections.Count; i++)
{
PageInfo docInfo = doc.GetPageInfo(0);

Shape image = docBuilder.InsertImage(layersContent[0]);
image.WrapType = WrapType.None;
image.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
image.HorizontalAlignment = HorizontalAlignment.Center;
image.RelativeVerticalPosition = RelativeVerticalPosition.Page;
image.VerticalAlignment = VerticalAlignment.Center;
image.Left = 0;
image.Width = docInfo.WidthInPoints;
image.Height = docInfo.HeightInPoints;

Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(image);

InsertWatermarkIntoHeader(watermarkPara, doc.Sections[i], HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, doc.Sections[i], HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, doc.Sections[i], HeaderFooterType.HeaderEven);
}

How can I set the image above the text? Thank you

I found the problem:

private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)

{


HeaderFooter header = sect.HeadersFooters[headerType];



if (header == null)


{


// There is no header of the specified type in the current section, create it.


header = new HeaderFooter(sect.Document, headerType);


sect.HeadersFooters.Add(header);


}



// Insert a clone of the watermark into the header.


header.AppendChild(watermarkPara.Clone(true));


}


was adding a watermark to HeaderFooter, but how can I add it to body?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. MS Word document is flow document and does not contain any information about its layout into lines and pages. So there is no way to determine where page starts or ends.

However, you can try using a workaround suggested by my colleague here:

http://www.aspose.com/community/forums/thread/245384/textbox-at-every-page.aspx

Best regards.

Thanks for your reply, I’ll try this solution. But I have one more question about HeaderFooter watermark implementation that you suggested to me first time - this solution is good for me except one problem - the image in header/footer of output document is semivisible (for example, if i put some image with black text, after adding watermark it becomes gray). Can I handle that from code? Thank you

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. MS Word shade headers and footer so you can see where body starts and where header/footer is. After printing heeders/footers are not shaded.

Best regards,

It’s understood, but the question is - can I disable this feature (header/footer shade) programmatically?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. No, you cannot. Using Aspose.Words you can work only with document, but what you are asking for is seem to be a option of MS Word itself.

Best regards,

Ok, thanks.
Another question - the solution here - http://www.aspose.com/community/forums/thread/245384/textbox-at-every-page.aspx (FindPages.zip)
returns me a valid document, with less pages than original have. Can you check please what I am doing wrong?

Hi Victor,

Thanks for inquiry.

Could you please attach your document here for testing? I think maybe the call to doc.UpdatePageLayout is laying out the document differently.

Thanks,

Hi Adam,
here is the word file (226 pages), after handling I receive only 4 pages document.
See AddLayers method in code.cs

Hi Viktor,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Could you please explain why you need to add an image in front of each page? This is not common task, usually users adds watermarks behind. In you add an image in front, it will overlap part of content of your document and it will be not readable.

Best regards,

Hi Victor,

Thanks for your inquiry.

The code does appear to work properly on my end and will output a textbox on each page, could you please attach our output document here?

Also as Alexey suggested, why do you require a textbox on every page? Perhaps there is an easier way to achieve what you are looking for.

Thanks,

Alexey, Aske012,
first of all thanks for your replies.

One of my company’s projects need ability to work with MS Office documents - that’s why we choosed Aspose solution. I can’t explain why I need to add image to every page’s body (and not in header/footer) because it’s confidential information (you should to understand that), but this is the main feature.

alexey.noskov:

In you add an image in front, it will overlap part of content of your document and it will be not readable.

I definately understand that but its our requirments.

aske012:

Prehaps there is an easier way to achieve what you are looking for

As Alexey already explained to me - there is no way to know where page starts because MS Word document is flow document and does not contain any information about its layout into lines and pages. So the only solution he suggested was yours.

Aske012,
I removed any RemoveNodeRangeFromDocument calls so the code works perfectly. Thank you.

But I noticed that my code that appends image to pages skips empty pages… This is my code:

ArrayList pages = GetPages(doc);
Node page;

Shape layer;
Paragraph layerParagraph;
Run run;

for (int i = 0; i < pages.Count; i++)
{
if (pages[i] != null)
{
page = pages[i] as Node;

run = new Run(doc);
layerParagraph = new Paragraph(doc);
layerParagraph.AppendChild(run);

layer = new Shape(doc, ShapeType.Image);
layer.ImageData.SetImage(layersContent[0]);
layer.WrapType = WrapType.None;
layer.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
layer.HorizontalAlignment = HorizontalAlignment.Center;
layer.RelativeVerticalPosition = RelativeVerticalPosition.Page;
layer.VerticalAlignment = VerticalAlignment.Center;
layer.Left = 0;
layer.BehindText = false;
layer.Width = docInfo.WidthInPoints;
layer.Height = docInfo.HeightInPoints;

layer.AppendChild(layerParagraph);

builder.MoveTo(page);
builder.InsertNode(layer);
}
}

Hi Viktor,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. In one of future versions we plan to expose layout information of nodes in the Document. This will allow you to determine on which page the particular node is located. I think, with this feature implementation of this will be much easier. I linked your request to the appropriate issue. You will be notified once this feature is available. You can also vote for this feature here:

http://www.aspose.com/community/blogs/aspose.words-for-.net-java-reporting-services-and-jasperreports/archive/2008/12/01/where-on-a-page-is-my-paragraph.aspx

For now, unfortunately, you are compelled to use the workaround suggested by Adam.

Best regards,

Hi there,

Thanks for your inquiry.

It appears your original template did not have any blank pages, could you please attach your new template here and I will take a look for you.

Thanks,

Hi aske012,
thanks for your reply.
I can’t reproduce the bug… So I think the problem solved.
Thank you very much. And Alexey too

Hi, it’s me again.

I have found problem in your code (http://www.aspose.com/community/forums/thread/245384/textbox-at-every-page.aspx). Sometimes GetFieldValue returns an empty string. Can you tell me why this might happen? When I am trying to reproduce that - I can’t.
Thank you.