Skip to content
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

Open
qylwhetc opened this issue Apr 6, 2024 · 7 comments
Open

About ReplacePicture method #475

qylwhetc opened this issue Apr 6, 2024 · 7 comments

Comments

@qylwhetc
Copy link

qylwhetc commented Apr 6, 2024

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

dim txt="..."
row.ReplaceText(New StringReplaceTextOptions With {.SearchValue = "<txt>", .NewValue = txt}) 

This is great.

But the code to replace the Picture must be as follows: row.Paragraph (idx), idx is difficult to determine

Dim pNew = img.CreatePicture(100, 100)
Dim pOld = row.Pictures(0) '0...,easy to determine
'row.ReplacePicture(pOld, pNew)  'I hope,but error...
Dim idx = xxx	''no easy; Associate with template
row.Paragraphs(idx).ReplacePicture(pOld, pNew) 'replace(remove pOld) and  placed in Paragraphs(idx),not pOld's position

Thank you!

@XceedBoucherS
Copy link
Collaborator

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 );
if( paragraphContainingOldPicture != null )
{
paragraphContainingOldPicture.ReplacePicture( oldPicture, newPicture );
}

Thanks

@qylwhetc
Copy link
Author

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!
Thanks

@qylwhetc
Copy link
Author

Hi,
I could have used row.ReplaceTextWithObject solves the problem, but replacing text with picture cannot be formatted in the template;
Still row.ReplacePicture is good;
Looking forward to providing in doc, table, row,and cell,Just like in the Paragraph.

Thank you

@qylwhetc
Copy link
Author

qylwhetc commented Nov 30, 2024 via email

@XceedBoucherS
Copy link
Collaborator

Hi,
thank you for reporting this.
The issue has been found and the fix will be part of the next release.
In the meantime, if you have access to the code, you can go in file Xceed.Document.NET/Src/Document.cs,
In method : public override Paragraph InsertParagraph( Paragraph p )
dans replace :
p.PackagePart = this.PackagePart; return base.InsertParagraph( p );
with:
var newParagraph = base.InsertParagraph( p ); newParagraph.PackagePart = this.PackagePart; return newParagraph;

Thank you

@qylwhetc
Copy link
Author

qylwhetc commented Dec 17, 2024 via email

@XceedBoucherS
Copy link
Collaborator

Hi,

You are right !
Here's a temporary workaround until a valid fix is implemented.
Go in file Xceed.Document.NET/Src/Document.cs,
in method : public override Paragraph InsertParagraph( Paragraph p )
and replace its content with:
` var clonedParagraph = new Paragraph( p.Xml );
clonedParagraph.Document = p.Document.Copy();
clonedParagraph.PackagePart = p.PackagePart;

this.InsertParagraphPictures( clonedParagraph );
this.InsertParagraphStyles( clonedParagraph );

var updatedParagraph = clonedParagraph.Document.Paragraphs.First( paragraph => paragraph.StartIndex == p.StartIndex );

var newParagraph = base.InsertParagraph( updatedParagraph );
newParagraph.PackagePart = this.PackagePart;

return newParagraph;`

We will try to add a uniform fix for the next release.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants