-
Notifications
You must be signed in to change notification settings - Fork 474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
About ReplacePicture method #475
Comments
Hi, Thank you for the suggestion. We will look into adding this feature. In the meantime, could you add the following to detect which paragraph in your row contains the old picture and then replace the oldPicture with the new one: var paragraphContainingOldPicture = row.Paragraphs.FirstOrDefault( paragraph => paragraph.Pictures.FirstOrDefault( picture => picture.Id == oldPicture.Id ) != null ); Thanks |
Dim pNew = img.CreatePicture(100, 100)
Dim pOld = row.Pictures(0)
'row.ReplacePicture(pOld, pNew) 'I hope,but error...
'Dim idx = xxx '' Associate with template
'row.Paragraphs(idx).ReplacePicture(pOld, pNew)
'
'find Paragraphs(idx)
Dim paragraph= row.Paragraphs.FirstOrDefault(Function(p) p.Pictures.FirstOrDefault(Function(pic) pic.Id = pOld.Id) IsNot Nothing)
If paragraph IsNot Nothing Then
paragraph.ReplacePicture(pOld, pNew)
End If Ok! |
Hi, Thank you |
seek help,thinks!
Cloning an existing segment containing Picture was unsuccessful
//file: \Xceed.Words.NET.Examples\Samples\Paragraph\ParagraphSample.cs
public static void AddObjectsFromOtherDocument()
{
Console.WriteLine("\tAddObjectsFromOtherDocument()");
// Load a template document.
using (var templateDoc = DocX.Load(ParagraphSample.ParagraphSampleResourcesDirectory + @"Template.docx"))
{
// Create a document.
using (var document = DocX.Create(ParagraphSample.ParagraphSampleOutputDirectory + @"AddObjectsFromOtherDocument.docx"))
{
var p0 = templateDoc.Paragraphs[0];
var p1 = document.InsertParagraph(p0);//I would like to copy a segment containing Picture (utilize the format of existing Picture)
var n0 = p0.Pictures.Count; //1
var n1 = p1.Pictures.Count; //error ?????
Debug.WriteLine($"Pictures:{n0},{n1}");
// Add a title.
//...
document.Save();
}
}
}
|
Hi, Thank you |
hi!
Following your guidance, I modified the code:
//Xceed.Document.NET\Src\Document.cs
public override Paragraph InsertParagraph(Paragraph p)
{
// copy paragraph's pictures and styles.
this.InsertParagraphPictures(p);
this.InsertParagraphStyles(p);
//p.PackagePart = this.PackagePart;
//return base.InsertParagraph( p );
var newParagraph = base.InsertParagraph(p);
newParagraph.PackagePart = this.PackagePart;
return newParagraph;
}
public override Paragraph InsertParagraph(int index, Paragraph p)
{
//...Should it also be modified?
return returnParagraph;
}
Later recompiled, successful;
Afterwards, in the example, it was still incorrect, that is, why did the Picture in the original p0 disappear?
//Xceed.Words.NET.Examples\Samples\Paragraph\ParagraphSample.cs
public static void AddObjectsFromOtherDocument()
{
Console.WriteLine("\tAddObjectsFromOtherDocument()");
// Load a template document.
using (var templateDoc = DocX.Load(ParagraphSample.ParagraphSampleResourcesDirectory + @"Template.docx"))
{
// Create a document.
using (var document = DocX.Create(ParagraphSample.ParagraphSampleOutputDirectory + @"AddObjectsFromOtherDocument.docx"))
{
// Add a title.
document.InsertParagraph("Adding objects from another document").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center;
var p0 = templateDoc.Paragraphs[0];
var n00 = p0.Pictures.Count; //1
var p1 = document.InsertParagraph(p0);
var n0 = p0.Pictures.Count; //0,why? disappear?? (I also hope to clone p0 in a loop)
var n1 = p1.Pictures.Count; //1
Debug.WriteLine($"Pictures:{n0},{n1}");
//...
// Get the image from the other document.
var pictureToAdd = templateDoc.Pictures.FirstOrDefault();//null
// Add the image in the new document.
var newImage = document.AddImage(pictureToAdd.Stream);//error
var newPicture = newImage.CreatePicture(pictureToAdd.Height, pictureToAdd.Width);
p.AppendPicture(newPicture);
//...
document.Save();
}
}
}
How to solve it? thank you.
|
Hi, You are right ! this.InsertParagraphPictures( clonedParagraph ); var updatedParagraph = clonedParagraph.Document.Paragraphs.First( paragraph => paragraph.StartIndex == p.StartIndex ); var newParagraph = base.InsertParagraph( updatedParagraph ); return newParagraph;` We will try to add a uniform fix for the next release. |
What I hope is that the ReplacePicture method can be implemented in Document, Container, and Paragraph, and that the executed new Picture will be placed in the original Picture's position, Just like ReplaceText。
My needs stem from:
There is a table with multiple columns in the template, and a certain row that needs to be replaced with Picture or text fields
This is great.
But the code to replace the Picture must be as follows: row.Paragraph (idx), idx is difficult to determine
Thank you!
The text was updated successfully, but these errors were encountered: