Hi,
I would like to have a company logo to be dynamically replaced.
How would I know which is the logo if there happen to be multiple images on the page ?
I noticed that the Shape object has Name property. How or when does this get set ?
Hi,
I would like to have a company logo to be dynamically replaced.
How would I know which is the logo if there happen to be multiple images on the page ?
I noticed that the Shape object has Name property. How or when does this get set ?
@pooja.jayan You can specify shape in in the Selection Pane
in MS Word. Please see the attached screenshot:
hai,
Thanks for your reply.
in word this woks, but is it possible to set Name in onedrive?
@pooja.jayan I am afraid I do not see to set shape name in OneDrive. however, you can specify alt text and use it to identify shape:
I tried. uploaded a file to onedrive and set an altText to the logo, and I downloaded that file and using aspose I tried to extract logo with the altText set.
but shape.Name property is returning null.
Is there any way i can handle this scenario
@pooja.jayan I mean you can use alt text instead of shape name. Please see Shape.AlternativeText property.
@pooja.jayan Could you please attach your document here for testing? We will check it and provide you more information.
new.docx (23.6 KB)
this file has only one image, which I downloaded from Onedrive after setting the altText to ‘postman’
Hi,
Shape.Title is working., thankyou.
and also I wanted to ask whether it is possible to replace current image with a new image byte array?
@pooja.jayan Sure, you can use Shape.ImageData.ImageBytes property to get and set image bytes of the shape.
Got it. Working.
iamges.docx (327.9 KB)
labelled1.docx (25.6 KB)
these are the 2 documents on which I replaced existing image with new logo(sample - amazon logo).
It is replaced. But in different dimensions.
What can I do to replace the image in the same dimension in all documents.
code I have tried:
Aspose.Words.Document document = new Aspose.Words.Document(@"D:\POC\kb pdfs\new1.docx");
NodeCollection pictures = document.GetChildNodes(NodeType.Shape, true);
byte[] logo = null;
double height = 0, width = 0, left = 0, right = 0;
double dLeft = 0, dRight = 0, dTop = 0, dBottom = 0;
Shape shapeLogo = null;
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Default;
//int imageindex = 0;
foreach (Shape shape in pictures)
{
if (shape.HasImage)
{
if (shape.Name == "logo" || shape.AlternativeText == "logo" || shape.Title == "logo")
{
Console.WriteLine("logo found");
string imageFileName = @"D:\POC\kb pdfs\others\images\logo" + "." + shape.ImageData.ImageType;
shape.ImageData.Save(imageFileName);
logo = shape.ImageData.ImageBytes;
width = shape.Width;
height = shape.Height;
left = shape.Left;
right = shape.Right;
dLeft = shape.DistanceLeft;
dRight = shape.DistanceRight;
dTop = shape.DistanceTop;
dBottom = shape.DistanceBottom;
horizontalAlignment = shape.HorizontalAlignment;
}
}
}
string path = @"D:\POC\kb pdfs\SampleFiles";
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
Aspose.Words.Document newdoc = new Aspose.Words.Document(file);
NodeCollection nodes = newdoc.GetChildNodes(NodeType.Shape, true);
//int imageindex = 0;
foreach (Shape shape in nodes)
{
if (shape.HasImage)
{
if (shape.Name == "logo" || shape.AlternativeText == "logo" || shape.Title == "logo")
{
Console.WriteLine("replacing with new logo...");
shape.Width = width;
shape.Height = height;
shape.Left = left;
shape.DistanceLeft = dLeft;
shape.DistanceRight = dRight;
shape.DistanceTop = dTop;
shape.DistanceBottom = dBottom;
shape.HorizontalAlignment = horizontalAlignment;
//shape.ImageData.ImageBytes = logo;
shape.ImageData.SetImage(@"D:\POC\kb pdfs\others\images\logo.Png");
}
}
}
newdoc.Save(file);
}
Just check amazon logo in 2 documents
@pooja.jayan Could you please clarify, it is required to preserve original logo placeholder dimensions or it is required to check dimensions of the placeholder? If it is required to keep original dimensions, you can simply set image bytes or set image of the shape.
I want the logo in the document to be replaced with the new logo dimensions.
But here in this case, I tried to replace a image with new logo, but it takes different dimensions in 2 files(eg: you can check amazon logo in the shared documents).
@pooja.jayan Shape in your document Lock aspect ratio
option enabled, so changing one dimension affects the other one:
You should disable this options before setting dimensions:
// ......................
shape.AspectRatioLocked = false;
shape.Width = width;
shape.Height = height;
// ......................
Tried this. But still the same.
Image.PNG (13.9 KB)
here 1st image is the oroginal logo, and second one is the replaced one.see, it is not taking (second one)the oroginal file dimensions
@pooja.jayan I have tested with the following code for testing and the result is correct:
Document document = new Document(@"C:\Temp\iamges.docx");
// Get source logo shape.
Shape srcLogoShape = document.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
.Where(s => (s.Name == "logo" || s.AlternativeText == "logo" || s.Title == "logo")).FirstOrDefault();
Document newdoc = new Document(@"C:\Temp\labelled1.docx");
// Get destination logo shape.
Shape dstLogoShape = newdoc.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
.Where(s => (s.Name == "logo" || s.AlternativeText == "logo" || s.Title == "logo")).FirstOrDefault();
dstLogoShape.AspectRatioLocked = false;
dstLogoShape.Width = srcLogoShape.Width;
dstLogoShape.Height = srcLogoShape.Height;
dstLogoShape.ImageData.ImageBytes = srcLogoShape.ImageData.ImageBytes;
newdoc.Save(@"C:\Temp\out.docx");
labelled1.docx (25.6 KB)
new.docx (2.8 MB)
new1.docx (2.9 MB)
the same I tried replacing in all three documents, but the image dimensions are different. Is that a document specific issue?
@pooja.jayan Could you please specify, which is source and which is target document? I will test your scenario.