reading full address using inerop.excel.hyperlinks - c#

I've added some hyperlinks (images) into an excel file (.xls 2003).
I'm using the Microsoft.Office.Interop.Excel Reference to read this hyperlink's address.
But it doesn't show up as the full path location of the image, but only the name (eg image1.jpg).
Here is how I'm doing it.
Below three statements are in a loop.
object index = (object)i;
Microsoft.Office.Interop.Excel.Hyperlink link = links.get_Item(index);
Debug.WriteLine(link.Address);

I think I've found a temporary solution which needs further testing with different locations.
I already have an absolute path to the excel file.
And the hyperlink's address is relative to above excel file.
so here is what I did.
object index = (object)i;
Microsoft.Office.Interop.Excel.Hyperlink link = links.get_Item(index);
string absolutePath = System.IO.Path.GetFullPath(xlpath+link.Address);
Debug.WriteLine(absolutePath);
Bitmap image = (Bitmap) Image.FromFile(absolutePath,true);
This seems to be working for now but not appropriate solution.
I'm still looking for a better solution.

Related

Xamarin C# - How to reference a resource file programatically from a variable

I'm using Xamarin(C#) and trying to access the files in my Resources/drawable folder so that I can fill an ImageView with that picture. I have all the names of these resources(which are .png files) stored in an array and would like to loop through it to access each one.
I know you can access one file using Resource.Drawable.whateverTheNameIs but I don't want to hard code in the name, I want to be able to get it from a string variable. Is this possible? I tried the GetDrawables method but it's not finding it, and it also says its deprecated so I'd prefer the current way to do it.
Here's the exact code:
ImageView daButton = row.FindViewById<ImageView>(Resource.Id.theButtonThing);
Drawable d = myContext.Resources.GetDrawable(myContext.Resources.GetIdentifier(mItems[position], "button", myContext.PackageName));
daButton.SetImageDrawable(d);
Thanks!
you can find resource Id this way, it works for me:
int resID = Resources.GetIdentifier(mDrawableName, "drawable", PackageName);

How to create PDF file links with relative file path and PDFsharp?

My App creates a Content.pdf file that links to Server.pdf and Client.pdf. This is the folder structure:
..\MyApp\Ressources\Content.pdf
..\MyApp\Ressources\Server\Server.pdf
..\MyApp\Ressources\Client\Client.pdf
Furthermore, the folders and files get burned on a CD/DVD or get backed up on a network drive - that causes me to use relative file paths. I use following code to create the Content.pdf:
relative WebLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddWebLink(pdfrect, relativeFileLinkPath);
relative FileLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddFileLink(pdfrect, relativeFileLinkPath);
Both work fine on local drive, but my issues are:
WebLinks do not work/open on network drive
FileLinks close the Content.pdf and replace it with the linked pdf file
Questions:
How can I modify the FileLinks to get opened in a new (PDF reader) instance/window?
Holding the CTRL-Key when clicking on the FileLink is an option but not a nice solution.
What’s the right syntax for relative PDF web links?
AddFileLink is the correct method for files sitting side by side in a folder.
To control whether a new window will be opened, a small modification of PDFsharp's PdfLinkAnnotation class will be needed. PDF supports a NewWindow attribute which PDFsharp cannot currently set.
Get the source for PDFsharp, locate "<</Type/Action/S/Launch/F<</Type/Filespec/F{0}>> >>" in PdfLinkAnnotation.cs and replace it with "<</Type/Action/S/Launch/NewWindow true/F<</Type/Filespec/F{0}>> >>".
Disclaimer: I did not test the proposed change - I hope it is syntactically correct and I hope it does what you want.
Disclaimer 2: This change is needed for PDFsharp 1.50 beta 3. Later versions may have support for that flag.

Finding the exact path of the file

I am using the fileUpload control. When I upload the file, I want to find the exact location of the file.
I tried using:
string fname= Server.MapPath(FileUpload2.FileName);
string fname= FileUpload2.FileName;
string fname= FileUpload2.PostedFile.FileName;
Numbers 2 & 3 gave me the name of the file. Number 1 gave me the the path of my website location. I do not know what is the difference between 2 and 3, why both gave me same results.
I read somewhere, that you cannot get the path. Is it true? If not, what code should I use?
There is no actual file path because a file uploaded to the server is simply held in memory.
The FileUpload control is just a wrapper around an HttpPostedFile instance, which itself is basically just a wrapper around an InputStream.
It's up to you to actually save the file somewhere. Until then it doesn't exist in any physical location.
The FileName property simply corresponds to the filename from the client's machine, minus the path. It has no correlation to anything on the server's file system.
There are a couple of different ways you can deal with the file.
Save The File To Disk:
The FileUpload control provides a SaveAs method that will allow you to save the file locally, or some UNC that you have access to.
FileUpload2.SaveAs("C:\\Temp\\" + FileUpload2.FileName);
Process The File In Memory:
Since you have access to the FileContent, you could simply manipulate and process the file directly. Assuming you know what type of file it is (txt, pdf, csv, etc...)
using (var sr = new StreamReader(FileUpload2.FileContent))
{
while ((var line = sr.ReadLine()) != null)
{
//Do something with 'line'
}
}

Linked image's SourceFullName differs from actual link

I have a weird case in a word automation program I'm developing using the Office Interop and C# 3.5
One task of the program is to copy any linked images in the word document to a different location and rewrite the Linked Source of those images to the new location.
Now, in one document, when I check the linked files (using Word 2010), it points an image to a location similar to images\image_file.jpg - So, the image is in a subfolder of the folder where the document is. That's totally correct.
But, when my program runs into that image, the LinkFormat.SourceFullName of that same image gives me a path on our local network, e.g. \\net-storage\customer\001 - customername\data\images\documents\image_file.jpg, without any correlation whatsoever to the actual image link I'm expecting.
What's going wrong here? How do I get the correct image source in my program?
Edit to sw_lasse: I'm sure this is a relative path, because (in other documents) after deleting the image in the relative path and updating the fields in word, the image is not found. So it's definitely a relative path.
Also, the two paths (network and relative) have no correlation to each other. The images on the network use a completely different folder hierarchy, so that's why there's a document subfolder, while it doesn't exist in the relative path.
I know your writing VSTO C# code and this is VBA, but I believe this explains the odd behaviour your seeing.
You cannot use:
myPicPath = Options.DefaultFilePath(wdPicturesPath)
... because if the user has inserted a picture from a different folder, the Default File Path returns that folder rather than the actual setting from the dialog box. Instead, you can use:
With Dialogs(wdDialogToolsOptionsFileLocations)
.Path = "PICTURE-PATH"
.Update
myPicPath = .Setting
If Not Right$(myPicPath, 1) = "\" Then
myPicPath = myPicPath + "\"
End If
MsgBox myPicPath
End With
Similarly, you cannot use:
myDocPath = Options.DefaultFilePath (wdDocumentsPath)
to get the default document path, because this returns the current FileOpen path, not the default documents path!!
Instead, use:
Dim myDocPath As String
myDocPath = Dialogs(wdDialogToolsOptionsFileLocations).Setting
'Add a "\" at the end of the path, unless the setting is already followed by a "\" -
'which it will be if the setting is set to a root folder
If Not Right$(myDocPath, 1) = "\" Then
myDocPath = myDocPath + "\"
End If
MsgBox myDocPath
These days I'm pretty good at converting VBA to C#, if you provide some code that doesn't work I'll convert this VBA to suit.
Ref: How to retrieve Word's default Documents path or Pictures path setting

Portability worries because of custom cursor path in C#

I am using a custom cursor named hand2.cur in my C#-WPF application. I have added the cursor to a folder named Images which has all the images that I use in my application. However I've realized that I cannot add relative path to use my custom cursor as:
Cursor newCur = new Cursor("Images\\hand2.cur");
window.Cursor = newCur;
So I used this:
string absolute = System.IO.Path.GetFullPath("hand2.cur");
Cursor newCur = new Cursor(absolute);
window.Cursor = newCur;
This tries to find the hand2.cur file in the \bin\Release folder. So I added the file there and I got it working.
But the problem is, if I Publish this application and use it on a different computer, it does not work. Now the problem is with the cursor file path, because if I deploy it after commenting those 3 lines, it works correctly. So what do I do to rectify this problem?
I am using other images from the Image folder in my XAML code and they seem to port fine. But then again my knowledge of WPF is limited so if anyone has any ideas, that would help.
EDIT: I have added my Images folder to the project. I have also set the Build Action of the cursor file hand2.cur to Embedded Resource. However when I use the following two lines, I get an XAMLParseException.
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/Slideshow;component/Images/hand2.cur"));
window.Cursor = new System.Windows.Input.Cursor(info.Stream);
The Inner Exception field when I view the details of the error reads: {"Cannot locate resource 'images/hand2.cur'."}
You could make the cursor a resource in your app/assembly and then use GetResourceStream with the pack Uri to the resources location. Pass the Stream of the StreamResourceInfo to the ctor of the Cursor. e.g.
var info = Application.GetResourceStream(new Uri("pack://application:,,,/Images/hand2.cur"));
var cursor = new Cursor(info.Stream);
I've got this working after I added the cursor file hand2.cur to my Resource1.resx resource file. Then I used the following statement in my code:
window.Cursor = new Cursor(new System.IO.MemoryStream(MyNameSpace.Resource1.hand2));

Categories

Resources