Linked image's SourceFullName differs from actual link - c#

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

Related

C# winforms get nth folders from path

I've got an absolute path available to me. Say: C:/Program/CoreFiles/Folder1/Folder2/File.txt.
I need to copy that file to C:/Program/Projects/ProjectName/ but it needs to keep Folder1/Folder2/File.txt intact. So the end result should be C:/Program/Projects/ProjectName/Folder1/Folder2/File.txt.
My first attempt at solving this was to try and get the relative path between 2 absolute paths. Found Path.GetRelativePath(string, string) which obviously didn't help as it wasn't meant for WinForms. It would mess up anyway as the final result would be C:/Program/Projects/ProjectName/CoreFiles/Folder1/Folder2/File.txt.
The target directory is empty and I don't know the relative path to copy beforehand other than somehow getting that info out of the absolute path. Since File.Copy won't create folders that don't exist yet, I need to create them first. So how do I get the path that leads up to the file from the CoreFiles directory out of the absolute path?
The only working solution I can come up with is using regex to just replace CoreFiles with Projects/ProjectName in the path string and work with that. But that somehow seems the wrong approach.
Since you can't use Path.GetRelativePath. I suggest looking at another answer that describes how to do this yourself.
Like here...
How to get relative path from absolute path
Using the method in that answer, you can do the rest of your task as shown below.
string sourcePath = "C:/Program/CoreFiles/Folder1/Folder2/File.txt";
string sourceRoot = "C:/Program/CoreFiles/";
string destinationRoot = "C:/Program/Projects/ProjectName/";
// Use built-in .NET Path.GetRelativePath if you can. Otherwise use a custom function. Like here https://stackoverflow.com/a/340454/1812944
string relativePath = MakeRelativePath(sourceRoot, sourcePath);
// Combine the paths, and make the directory separators all the same.
string destinationPath = Path.GetFullPath(Path.Combine(destinationRoot, relativePath));
// Create nested folder structure for your files.
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
// Copy the file over.
File.Copy(sourcePath, destinationPath);

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.

reading full address using inerop.excel.hyperlinks

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.

Unable to open PDF programmatically

I'm struggling to open a PDF file inside of Unity. Currently, my application will open up the folder location instead of opening the actual PDF itself.
I've tried using both System.Diagnostics.Process.Startand Application.OpenURL but they all act the same.
Right now, my code looks like:
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Now when I hard code in the file location like below, it opens up the PDF correctly:
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Normally I'd leave it hard coded, but I need to allow one button to open any PDF. How can I resolve this?
The string output of the two lines below are most likely not equal.
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Ensure the paths are the same and you should get the result you expect.
See the documentation for Application.OpenURL here:
http://docs.unity3d.com/ScriptReference/Application-dataPath.html
If you read to the bottom, you'll notice:
"Note that the string returned on a PC will use a forward slash as a folder separator."
This likely the reason why you're getting different results.
Also note that the value of Application.OpenURL changes based on the platform.
string pdfURL = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "Documents/keyboard-shortcuts-Visual-Studio-Code.pdf";
it return below url and run in browser
http://localhost:1727/Documents/keyboard-shortcuts-Visual-Studio-Code.pdf
thanks
upendra

Get version info of a patch file in c#

I am uploading a .msi file using fileupload control to a central location. Now i need to get version info of this file. I am using the following code.
FileVersionInfo patchFile = FileVersionInfo.GetVersionInfo(completeFilePath)
completeFilePath is the full path of the uploaded file. This code breaks and throws file not found exception.however, if i look down in the physical directory,file exists there.
Am i missing something or will i have to download this uploaded file again to some temp location and then extract version info from this file.
Second option i had was to get version info before uploading the file. In this case i am not able to get complete path of this patch file as fileupload control just gives the fileName and not the complete location.
Please suggest how to proceed.
I think the problem is in how to define "completeFilePath"
Remember that if the completeFilePath is a non-literal string then you must escape the special characters.
For example: [string filePath = "C:\\Windows\\FolderName\\FileName.txt";]
(notice the escape character ()
Another option is to use the literal string which enables you to use the special characters without having to use the escape character. An example is:
[string filePath = #""C:\Windows\FolderName\FileName.txt"";]
If this still doesn't work then could you please post how you are inputting this?

Categories

Resources