PPTX with hyperlinks conversion - Problem Replacing hyperlink with another

Hi All,
I am trying to convert PPTX file having hyperlinks to PDF file. Problem goes like this.

Before converting PPTX file to PDF, I am trying to replace some of the hyperlinks in the PPTX.

Suppose PPTX has hyperlink “http://eval.ups.com”, I am trying to replace eval.ups with ups. so finally when PPTX is converted to PDF it should have "http://ups.com"

Here is the code which I have written. This code replaces only the first link in the first slide. I don’t see the conversion in other links/slides.

I am spared lot of time on this. Can anyone help with this please.sample pptx file attached.

------------------------------------------------------------------------------
System.IO.Stream iStream = null;

iStream = new System.IO.FileStream(localSourceFile, System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);

PresentationEx pres = new PresentationEx(iStream);

foreach (Aspose.Slides.Pptx.SlideEx slide in pres.Slides)
{
foreach (Aspose.Slides.Pptx.ShapeEx shape in slide.Shapes)
{
if (shape.ToString() == “Aspose.Slides.Pptx.AutoShapeEx” ||
shape.ToString() == “Aspose.Slides.Pptx.SlideEx”)
{

Aspose.Slides.Pptx.AutoShapeEx shape1 = (Aspose.Slides.Pptx.AutoShapeEx)shape;
Aspose.Slides.Pptx.TextFrameEx txtframe = shape1.TextFrame;

foreach (Aspose.Slides.Pptx.ParagraphEx paragraph in txtframe.Paragraphs)
{
foreach (Aspose.Slides.Pptx.PortionEx portion in paragraph.Portions)
{
for (int i = 0; i < (paragraph.Portions.Count); i++)
{
string OldUrl = “”;
if (paragraph.Portions[i].HLinkClick != null)
{
OldUrl = paragraph.Portions[i].HLinkClick.ExternalUrl;
if (OldUrl != “”)
{

string NewUrl = “”;
NewUrl = OldUrl.Replace(“eval.ups”, “ups”);
HyperlinkEx HLink = new HyperlinkEx(NewUrl);
paragraph.Portions[i].HLinkClick = HLink;
pres.RemoveWriteProtection();
pres.Write(iStream);
}
}

}
}
}
}
}
}
pres.Save(outputFile, Aspose.Slides.Export.SaveFormat.Pdf); iStream.Close();

Hi Chethan,


Please try using the following sample code on your end. It worked fine on my end. Please share, if I may help you further in this regard.

String path = @“C:\Users\Mudassir\Downloads”;
String localSourceFile = path + “Sample.pptx”;

String outputFile = path + “Sample.pdf”;
System.IO.Stream iStream = null;

iStream = new System.IO.FileStream(localSourceFile, System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);

PresentationEx pres = new PresentationEx(iStream);

foreach (Aspose.Slides.Pptx.SlideEx slide in pres.Slides)
{
foreach (Aspose.Slides.Pptx.ShapeEx shape in slide.Shapes)
{
if (shape.ToString() == “Aspose.Slides.Pptx.AutoShapeEx” ||
shape.ToString() == “Aspose.Slides.Pptx.SlideEx”)
{

Aspose.Slides.Pptx.AutoShapeEx shape1 = (Aspose.Slides.Pptx.AutoShapeEx)shape;
Aspose.Slides.Pptx.TextFrameEx txtframe = shape1.TextFrame;

foreach (Aspose.Slides.Pptx.ParagraphEx paragraph in txtframe.Paragraphs)
{
foreach (Aspose.Slides.Pptx.PortionEx portion in paragraph.Portions)
{
for (int i = 0; i < (paragraph.Portions.Count); i++)
{
string OldUrl = “”;
if (paragraph.Portions[i].HLinkClick != null)
{
OldUrl = paragraph.Portions[i].HLinkClick.ExternalUrl;
if (OldUrl != “”)
{

string NewUrl = “”;
NewUrl = OldUrl.Replace(“eval.ups”, “ups”);
HyperlinkEx HLink = new HyperlinkEx(NewUrl);
paragraph.Portions[i].HLinkClick = HLink;
// pres.Write(iStream);
}
}

}
}
}
}
}
}
pres.RemoveWriteProtection();
pres.Save(outputFile, Aspose.Slides.Export.SaveFormat.Pdf);
iStream.Close();
pres.Save(localSourceFile, Aspose.Slides.Export.SaveFormat.Pptx);


Many Thanks,

Hi Mudassir
Thank you so much for your reply. I thought your code would work but when I tried, it had some problem.

I have attached another sample file where in I placed 3 different hyperlinks.


1.Click here - http://eval.ups.com
2.Google - http://google.com/
3.NewyorkTimes - http://eval.ups.newyorktimes.com/

when I run this code I found this in the pdf file. google and newyorktimes are having same urls.
strange thing is that i have the same links in the slide2 also where i see no problem. only slide 1 has the problem.

1.Click here - http://ups.com
2.Google - http://ups.newyorktimes.com/
3.NewyorkTimes - http://ups.newyorktimes.com/

It would be great if you you can have a look at it. attached the pptx file.

Hi Chetan,


I have used the previously shared code using Aspose.Slides for .NET and have not been able to observe any issue in the generated PDF and PPTX. For your kind reference, I have attached both generated PDF and PPTX.

Can you please try using the mentioned version with my sample code on your end.

Many Thanks,

Thanks a lot Mudassir.

Your code is working fine. There was some problem in the code extension which I added to customize the existing code. You saved lot of my time. This is my first code with Aspose.net. Hope I will improve in my next coding :slight_smile:

Thanks a lot again for your quick response.

Hi Chethan,

That is appreciable that things worked on your end. I like to share that Aspose.Slides is a very user friendly API and you will hopefully have a grasp on it quickly. Please share, if I may help you further in this regard.

Many Thanks,