Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert image at specific point of paragraph

*I have a Google Document with a string like "text {logo} text" How do place an image where {logo} is? So far I tried:

var logoElement = s.findText("{logo}").getElement();
logoElement.getParent().insertInlineImage(0,logoBlob);
s.replaceText("{logo}", "");

But this inserts the image before the found paragraph (or with 1: after). How do I place it inside the paragraph at the exact location?

like image 873
wivku Avatar asked Dec 11 '13 17:12

wivku


People also ask

How do I insert an image at a specific point in the document?

Insert a picture in Word, PowerPoint, or Excel Click the location in your document where you want to insert a picture. On the Insert tab, click Pictures. Select the option you want to use for inserting pictures.

How do I insert an image at a specific point in the document in LaTeX?

Including images in your LaTeX document requires adding: \usepackage{graphicx} to the beginning/preamble of your document. \includegraphics{ } command tells LaTeX to insert the image. To upload an image, click the upload button, and upload your image file.

How do I add a figure to a specific place in LaTeX?

You can control the position of an image using options for the figure environment, e.g. the [h!] in the example below tells LaTeX to put the figure exactly where it appears in the text, instead of letting it 'float' to a particular place in the document.


2 Answers

I hope will be helpful, The following code is fine to me.

function insertImage(doc) {
  // Retrieve an image from the web.
  var resp = UrlFetchApp.fetch("http://www.google.com/intl/en_com/images/srpr/logo2w.png");
  var image = resp.getBlob();
  var body = doc.getBody();
  var oImg = body.findText("<<Logo1>>").getElement().getParent().asParagraph();
  oImg.clear();
  oImg = oImg.appendInlineImage(image);
  oImg.setWidth(100);
  oImg.setHeight(100);
}
like image 85
Marisol Calderón Pérez Avatar answered Sep 28 '22 01:09

Marisol Calderón Pérez


Thanks to the comment from Serge and the original post of the link for pointing me in the right direction. Here's what I have now:

var s = d.getHeader();
var logoResult = s.findText("{logo}"); // search result
var logoElement = logoResult.getElement(); // the paragraph that contains the placeholder
var text = logoElement.getText();
var placeholderStart = logoResult.getStartOffset(); // character position start placeholder
var placeholderEnd = logoResult.getEndOffsetInclusive(); // char. position end placeholder
var parent = logoElement.getParent(); 
var parPosition = parent.getChildIndex(logoElement);

// add new paragraph after the found paragraph, with text preceding the placeholder
var beforeAndLogo = s.insertParagraph(parPosition+2, text.substring(0, placeholderStart));
var logo = beforeAndLogo.appendInlineImage(logoBlob); // append the logo to that new paragraph

// add new paragraph after the new logo paragraph, containing the text after the placeholder
var afterLogo = s.insertParagraph(parPosition+3, text.substring(placeholderEnd+1));
afterLogo.merge(); // merge these two paragraphs

// finally remove the original paragraph
parent.removeFromParent(); // remove the original paragraph

It is not complete, I should also copy all the attributes. More importantly, it does not copy the tab settings (e.g. center tab). Have not found a way to set tab positions.

like image 22
wivku Avatar answered Sep 28 '22 01:09

wivku