We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Image at Position

Hi there!


I have the following two problems to solve:

1) How do I insert a picture at a specified position relative to the edge ot the page?

This is the code I use (VB6 with COM-Interop)

oDocumentBuilder.MoveToHeaderFooter HeaderFooterType_HeaderFirst
With oShape
.RelativeHorizontalPosition = RelativeHorizontalPosition_Page
.RelativeVerticalPosition = RelativeVerticalPosition_Page
.VerticalAlignment = VerticalAlignment_Top
.HorizontalAlignment = HorizontalAlignment_Left
.Left = 0
.Top = 0
.AnchorLocked = False
.WrapSide = WrapSide_Both
.WrapType = WrapType_None
End With

When I ran this code I expected the image to be in the top left corner of the page (left=0, top=0), but it was inserted at the left/top margin of the header.

2) I have to dynamically insert one or more images on one or more pages of the document. I have not found any method to use DocumentBuilder.MoveTo to go to a specified page…have I overloooked something? I read about the method GetPages() but could not find any reference to this function so far…

Best regards,
Michael

Hi Michael,


Thanks for your inquiry.

1) You can insert a floating image at the top left corner of first page by using the following code snippet:

DocumentBuilder builder = new DocumentBuilder();<o:p></o:p>

PageSetup ps = builder.PageSetup;

Shape shape = builder.InsertImage(@"C:\Temp\Aspose.Words.png");

shape.WrapType = WrapType.None;

shape.Left = 0 - ps.LeftMargin;

shape.Top = 0 - ps.TopMargin;

builder.Document.Save(@"C:\Temp\out.docx");


2) I am afraid, there is no way to move cursor to the beginning of a Page; however, you can move the cursor to the first Paragraph in a particular Page and then make that Paragraph as an anchor point for your image. Here is how you can achieve this:

Document doc = new Document(@"C:\Temp\in.docx");

LayoutCollector collector = new LayoutCollector(doc);

Paragraph anchorPara = null;

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

foreach (Paragraph para in paragraphs)

{

if (collector.GetStartPageIndex(para) == 2)

{

anchorPara = para;

break;

}

}

DocumentBuilder builder = new DocumentBuilder(doc);

PageSetup ps = builder.PageSetup;

builder.MoveToParagraph(paragraphs.IndexOf(anchorPara), 0);

Shape shape = builder.InsertImage(@"C:\Temp\Aspose.Words.png");

shape.WrapType = WrapType.None;

shape.Left = 0 - ps.LeftMargin;

shape.Top = 0 - ps.TopMargin;

builder.Document.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best regards,

Hi Awais,


thank you for your reply. The first problem is solved to my full satisfaction and as for the second problem I only have one more question: How do I instantiate the LayoutCollector-object in COM-Interop?

As far as I can tell, there is no creatable LayoutCollector object.

I can do this:
Dim oDocumentBuilder As New Aspose_Words.DocumentBuilder

But I can’t do that:
Dim oLayoutCollector As New Aspose_Words.LayoutCollector

Thank you in advance
Michael
Hi Michael,

Thanks for your inquiry. Currently, Aspose.Words defines four publicly creatable COM objects i.e. ComHelper, Document, DocumentBuilder and License. To access other classes/methods available in Aspose.Words API, you must have to create wrapper/helper classes. However, we will consider making all public API accessible from COM and scripting languages in future. Your request has been linked to the appropriate issue (WORDSNET-4977) in our issue tracking system and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Edit: Please also visit the following link to learn how to use Aspose.Words for .NET via COM Interop:
http://www.aspose.com/docs/display/wordsnet/Use+Aspose.Words+for+.NET+via+COM+Interop

Best regards,

@masteriou,

The issues you have found earlier (filed as WORDSNET-4977) have been fixed in this Aspose.Words for .NET 17.10 update and this Aspose.Words for Java 17.10 update.

Please see Aspose.Words.Wrapper.Samples.sln at GitHub (A wrapper for common methods by Aspose.Words for .NET).