Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Jun 18, 2020 12:35 am

Hi I|m looking a sample of
I have 3 bookmarks in a single line, I wanted to replace these 3 bookmarks with images but as per my code images are generating new lines.
I found the issue is with BookmarksNavigator.InsertParagraph(paragraph); everytime inserting a new paragraph.
This is not what I want.
I'm looking for any workaround here to reach my expectations.

GOPIKRISHNA1212
 
Posts: 1
Joined: Thu Jun 18, 2020 12:13 am

Thu Jun 18, 2020 2:31 am

Hello,

Thanks for your inquiry.
Please refer to the following code. If there is any question, please provide your input file and your desired output for further investigation.
Code: Select all
    string filePath = @"bookmarks.docx";
    string FileSourcePath = @"Images\";
    Document document = new Document();
    document.LoadFromFile(filePath);
    string[] files = Directory.GetFiles(FileSourcePath, "*.png");
    int i = 0;
    foreach (var imagePath in files)
    {
        if (i < 3)
        {
            i++;
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("image" + i);
            //Get bookmarkStart
            BookmarkStart start = bn.CurrentBookmark.BookmarkStart;
            //Get the owner paragraph
            Paragraph para = start.OwnerParagraph;
            //Get bookmarkStart index
            int index = para.ChildObjects.IndexOf(start);
            //Add an image
            Image image = Image.FromFile(imagePath);
            DocPicture picture = new DocPicture(document);
            picture.LoadImage(image);
            //Insert the image into the owner paragraph
            para.ChildObjects.Insert(index+1, picture);
            picture.Width = 50;
            picture.Height = 50;
        }
    }
    document.SaveToFile("out.docx", FileFormat.Docx);
    Process.Start("out.docx");


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF

cron