Hello,
I am using Aspose.Page for Manipulate XPS File. Actually I have to Replace the existing text with the new text. Is it Possible using Aspose.Page?
Please refer the below link for the same.
Thanks
Hello,
I am using Aspose.Page for Manipulate XPS File. Actually I have to Replace the existing text with the new text. Is it Possible using Aspose.Page?
Please refer the below link for the same.
Thanks
Regretfully, the feature to replace text in XPS has not been yet investigated. Can you please share a sample source XPS with expected output document? We will log an investigation ticket and share the ID with you.
Thanks for the reply @asad.ali,
Actually What I have to do is I have to Update the hyperlinks from the .XPS file same as we are updating in .pdf file using aspose.pdf. Can you please guide how this will be possible or it is possible using Aspose.Page?
We are trying to check the feasibility of this feature and preparing a code snippet if possible. We will share our feedback with you soon.
Thanks for the update,
Can you please update ASAP.
Here it is
public void UpdateHyperlink()
{
System.Collections.Generic.List<XpsHyperlinkElement> hyperlinks = new System.Collections.Generic.List<XpsHyperlinkElement>();
string dataDir = "path to the data";
using (XpsDocument document = new XpsDocument(System.IO.Path.Combine(dataDir, "filename.xps")))
{
document.SelectActiveDocument(1); // Not necessary, the first page of the first document is loaded by default
document.SelectActivePage(1); // Not necessary, the first page is loaded by default
XpsPage page = document.Page;
ExtractExternalLinks(page, hyperlinks);
hyperlinks[0].HyperlinkTarget = new XpsExternalLinkTarget("https://www.aspose.com");
document.Save(System.IO.Path.Combine(dataDir, "filename_updated.xps"));
}
}
private void ExtractExternalLinks(XpsElement element, System.Collections.Generic.List<XpsHyperlinkElement> hyperlinks)
{
for (int i = 0; i < element.Count; i++)
{
if (element[i].HyperlinkTarget != null && element[i].HyperlinkTarget is XpsExternalLinkTarget) // Filter out external hyperlinks only
hyperlinks.Add(element[i]);
if (element[i].Count > 0)
ExtractExternalLinks(element[i], hyperlinks);
}
}
The UpdateHyperlink() method is supposed to contain the main() functions’s code. In other words, it’s the entry point.
Thank you So much MR. Asad ali. It will surely worked for me.
But how should I found existing hyperlinks “hyperlinks[i].HyperlinkTarget.Value”.
i.e. If the .XPS file has the Hyperlinktarget like “https://www.aspose.com” and I have to replace as “https://www.google.com”, so I have to first of all compare the hyperlink and for that I have to fetch the string from it. Can you please tell me how should I fetch that existing hyperlink?
Thank you in advance…
Hello Asad Ali,
Your trick has helped me a lot, I have stuck now by seeing that after replacing the hyperlinks the size and the structure of the xps file is changed. The Size is reducing and the structure of the file is changing. Some of the structure file is deleted and some new file is added to the new file.
Please find the attached images for your ready reference.Changes.png (80.8 KB)
Secondly what I have to do is, right now we have change the hyperlinks, and that works fine. But I have to change the text of that hyperlinks too. Can we change the text along with hyperlinks?
Thank You in Advance.
We need sample XPS file along with the complete code snippet with which you are able to generate the output. We will further proceed to assist you accordingly.
Here I am attaching the sample xps fileBasic_Sample.zip (179.8 KB).
In which I have to Update the text which is “https://www.google.com” along with the hyperlink to “https://www.apple.com”.
Below is the Code Snippet:
private void ExtractExternalLinks(XpsElement element, System.Collections.Generic.List hyperlinks)
{
try
{
for (int i = 0; i < element.Count; i++)
{
if (element[i] != null && element[i].HyperlinkTarget is XpsExternalLinkTarget) // Filter out external hyperlinks only
hyperlinks.Add(element[i]);
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
Type type = element[i].GetType();
FieldInfo[] info = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
FieldInfo info1 = type.GetField("#=zSFjECLg=", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
var str1 = info1.GetValue(element[i]);
if (element[i].HyperlinkTarget != null && element[i].HyperlinkTarget is XpsExternalLinkTarget) // Filter out external hyperlinks only
hyperlinks.Add(element[i]);
if (element[i].Count > 0)
ExtractExternalLinks(element[i], hyperlinks);
}
}
catch(Exception ex) { }
}
private void btnCreate_Click(object sender, EventArgs e)
{
try
{
System.Collections.Generic.List<XpsHyperlinkElement> hyperlinks = new System.Collections.Generic.List<XpsHyperlinkElement>();
string dataDir = txtFilePath.Text;
using (XpsDocument document = new XpsDocument(dataDir))
{
document.SelectActiveDocument(1); // Not necessary, the first page of the first document is loaded by default
//document.SelectActivePage(2); // Not necessary, the first page is loaded by default
for(int i=1; i<=document.PageCount;i++)
{
document.SelectActivePage(i);
XpsPage page = document.Page;
ExtractExternalLinks(page, hyperlinks);
}
//XpsPage page = document.Page;
//ExtractExternalLinks(page, hyperlinks);
for(int i=0; i<hyperlinks.Count;i++)
{
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
Object obj = hyperlinks[i].HyperlinkTarget;
Type type = obj.GetType();
//FieldInfo[] info = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
FieldInfo info1 = type.GetField("#=zbZMwy4Q=", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
var str1 = info1.GetValue(hyperlinks[i].HyperlinkTarget);
//Fetch the existing external link
if (str1.ToString() == txtExisting.Text)
{
hyperlinks[i].HyperlinkTarget = new XpsExternalLinkTarget(txtReplace.Text);
//Here I have to replace the text along with the hyperlink
}
}
document.Save(UpdatedPAth);
MessageBox.Show("Done");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
An investigation ticket as PAGENET-478 has been logged in our issue tracking system to further analyze this case of yours. We will look into its details and feasibility of the task. As soon as the ticket is resolved, we will update you in this forum thread. Please be patient and spare us some time.
We are sorry for the inconvenience.