Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jul 24, 2014 4:21 am

Hi,

I am using spire.doc .net version 3.5, I am trying to insert an image by finding a bookmark on word document and also I need to set the image vertical and horizontal position. How can I do this using spire doc.

Suman

suman79
 
Posts: 2
Joined: Thu Jul 24, 2014 4:16 am

Thu Jul 24, 2014 6:50 am

Dear Suman,

Thanks for your inquiry.
Please refer to the below code to insert an image at bookmark.

Code: Select all
Document document = new Document();
            document.LoadFromFile("..\\..\\Test.docx");
               BookmarksNavigator bn = new BookmarksNavigator(document);
            //if name of bookmark is SpireDoc
            bn.MoveToBookmark("SpireDoc", true, true);
            Section section =document.AddSection();
            Paragraph paragraph = section.AddParagraph();
            Image image = Image.FromFile("..\\..\\image1.jpg");
            DocPicture picture = paragraph.AppendPicture(image);

            bn.InsertParagraph(paragraph);
            document.Sections.Remove(section);
            string output = "sample.docx";
            document.SaveToFile(output,FileFormat.Docx);


You could try the below propertied to set the image position based on your requirement.
picture.TextWrappingStyle
picture.HorizontalAlignment
picture.HorizontalOrigin
picture.HorizontalPosition
picture.VerticalAlignment
picture.VerticalOrigin
picture.VerticalPosition


Please feel free to contact us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Jul 24, 2014 11:47 pm

Hi Amy,

thanks for your code, When I try to execute the application I am getting the following error.

"Could not load file or assembly 'Spire.Doc, Version=4.2.11.3, Culture=neutral, PublicKeyToken=19cd8b2d76b7209e' or one of its dependencies. The system cannot find the file specified."

I have used .NET 3.5 dll and using it in SharePoint 2010 visual web part.

Any fix for this issue.

suman79
 
Posts: 2
Joined: Thu Jul 24, 2014 4:16 am

Fri Jul 25, 2014 2:00 am

Hello,

Please ensure your dlls are in bin folder of your application. I suggest you use Spire.Doc Pack(hot fix) Version:5.2.16 which is the last version at present, remove the old dlls from your application and add the dlls for .NET3.5 from the package as references into it.
Downloading link: http://www.e-iceblue.com/Download/downl ... t-now.html


Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Jul 29, 2014 8:41 am

Hello,

Has your issue been resolved?
Thanks for your feedback.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Aug 20, 2014 7:18 pm

Hello, I am also trying to do this. I can get the image in the appropriate place, but because it is contained in a new paragraph (I assume), it is showing up in the next line. I need it to be replaced inline with my template.

Here is the code:

Code: Select all
navigator.MoveToBookmark("Signature");

var p = new Paragraph(navigator.Document);
DocPicture pic = p.AppendPicture(img);

navigator.InsertParagraph(p);


The bookmark is setup as follows:


Signed: [Signature]
Name: [Name]
Date: [Date]


What I'm getting is this:

Signed:
<IMAGE>
Name: Test McTesterson
Date: 8/18/2014


What I want is this:

Signed: <IMAGE>
Name: Test McTesterson
Date: 8/18/2014



Any suggestions. I tried all sorts of different workarounds, even getting the OwnerParagraph from BookmarkStart and appending to that paragraph instead of a new one, but that just put the image at the beginning (above "Signed:"). When I tried setting the TextWrappingStyle of the image, the docx file won't open in Office 2013, shows an general error on line 1. I don't have the ability to change the doc template and we were currently doing this as expected using Office Interop (but we are ditching that).

bobradu
 
Posts: 9
Joined: Wed Aug 20, 2014 7:06 pm

Thu Aug 21, 2014 2:24 am

Dear bobradu,

Thanks for your inquiry.
Please try the following code:
Code: Select all
  Document document = new Document();
            document.LoadFromFile("..\\..\\Test.docx");

            Paragraph paragraph = new Paragraph(document);
            Image image = Image.FromFile("..\\..\\pic.png");
            DocPicture picture = paragraph.AppendPicture(image);

            Bookmark bookmark = document.Bookmarks["Signature"];
            Paragraph bookPra=bookmark.BookmarkStart.OwnerParagraph;
            int index = bookPra.ChildObjects.IndexOf(bookmark.BookmarkStart);
            bookPra.ChildObjects.Insert(index, picture);

            string output = "sample.docx";
            document.SaveToFile(output, FileFormat.Docx);
            System.Diagnostics.Process.Start(output);


Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Aug 21, 2014 4:18 am

Worked like a charm. Thanks so much.

bobradu
 
Posts: 9
Joined: Wed Aug 20, 2014 7:06 pm

Thu Aug 21, 2014 7:42 am

Dear bobradu,

:)
Please feel free to contact us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc