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,
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);
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,
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
Thanks a lot again for your quick response.