I want to provide links to files uploaded on the server in iTextSharp documents.
The problem I'm facing is the string containing my file path is getting mangled en-route from my code onto the page.
Let's say the full path for the file, I'm trying to link is "C:\site uploads\some_file.txt".
I'm trying to create the link using the "Anchor" object, like so:
string path = "C:\site uploads\some_file.txt";
string name = "some_file.txt";
Anchor anchor = new Anchor(name, new Font(Font.FontFamily.UNDEFINED, 12));
anchor.Reference = path;
pdfDoc.Add(anchor);
ASP.NET C# will double up those backslashes in "path", as it does, but iTextSharp will further alter the string to something like C%5%20site%HCuploads%20some%34file.txt, which does not work as a clickable link in my document. FYI, I know I didn't get my % codes just right; those are offered for example only.
I'm not trying to launch any external applications from my document, I just want to enable the user to download this file. Any advice would be appreciated.
Why do you link to a local file? If someone else downloads that PDF on their machine, it won't have that directory.
The backslashes and colon are escaped.
Use https:// links. If you do want to link to local files, use a file:// link.
Related
I'm using webbrowser control in C# application to display different local files, not necessarily web pages but also images and pdf.files.
I noticed that it works:
string path = "C:\MyDirectory\file.png";
webBrowser1.Navigate(path,false);
but if there are spaces in directories names it fails:
string path = "C:\My Directory\file.png";
webBrowser1.Navigate(path,false);
What is the trick to display such files correctly in webbrowser control?
Prefix your string with # for local and UNC paths.
string localpath = #"drive:\Some Folder\file with spaces.extension";
string uncPath = #"\\network machine\Resources\Some Resource.extension";
Hi sorry I can't post a comment without more rep but if I understand correctly, the WebBrowser class is basically like a IE wrapper, and so URLs with spaces or special characters in need to be encoded like so:
string path = System.Uri.EscapeDataString(url)
So that it will replace spaces with %20 etc and WebBrowser should know where to go.
Checkout the docs for EscapeDataString
I have c# dynamic aspx page after new property add I create for record brochure
http://veneristurkey.com/admin/Brochure.aspx?Admin=PropertiesBrochureA4&id=36
but I want to this convert image file I am searching on internet but all with webbrowser and windows forms. I need on page load show not css type also image file. jpg, png or tiff how i can do this. i need to see sample code..
saving aspx page into an image 2
As I mentioned in my comment, your best bet is to opt for attempting to render HTML to an image.
Here is the link for a library that will allow your to render html to an image:
http://htmlrenderer.codeplex.com/
Here is code that does exactly what you're asking:
http://amoghnatu.wordpress.com/2013/05/13/converting-html-text-to-image-using-c/
Now all you have left is to get the html, since I'm assuming you don't want this to render to the browser prior to generating this image - you should look into grabbing the rendered html from the aspx page on the server prior to returning it, and then just return the image. To render a page:
https://stackoverflow.com/a/647866/1017882
Sorted.
If you do not mind using a commandline tool you can have a look at wkhtmltopdf. The package include a wkhtmltoimage component that can be used to convert HTML to image, using
wkhtmltoimage [URL] [Image Path]
Codaxy also wrote a wkhtmltopdf c# wrapper available through the NuGet package manager. I'm not sure if the wkhtmltoimage component was included, but it should be easy enough to figure out how they wrap the wkhtml components.
i fixed my problem with screenshot machine API they are my code..
public void resimyap()
{
var procad = WS.Satiliklars.Where(v => v.ForSaleID == int.Parse(Request.QueryString["id"])).FirstOrDefault();
var imageBytes = GetBytesFromUrl("http://api.screenshotmachine.com/?key=xxxxxx&size=F&url=http://xxxxxxx.com/a4.aspx?id=" + procad.ForSaleID);
string root = Server.MapPath("~/");
// clean up the path
if (!root.EndsWith(#"\"))
root += #"\";
// make a folder to store the images in
string fileDirectory = root + #"\images\a4en\";
// create the folder if it does not exist
if (!System.IO.Directory.Exists(fileDirectory))
System.IO.Directory.CreateDirectory(fileDirectory);
WriteBytesToFile( fileDirectory + + procad.ForSaleID + ".png", imageBytes);
Yes i also try wkhtmltopdf c# wrapper but in pdf or image converting time my computer fan goin crayz. also i must upload server exe file and my hosting firm didnt support them
I export data from my database to word in HTML format from my web application, which works fine for me , i have inserted image into record,
the document displays the image also , all works fine for me except when i save that file and send to someone else .. ms word will not find link to that image
Is there anyway to save that image on the document so path issues will not raise
Here is my code : StrTitle contains all the HTML including Image links as well
string strBody = "<html>" +
"<body>" + strTitle +
"</body>" +
"</html>";
string fileName = "Policies.doc";
//object missing = System.Reflection.Missing.Value;
// You can add whatever you want to add as the HTML and it will be generated as Ms Word docs
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader("Content-disposition", "attachment; filename=" + fileName);
Response.Write(strBody);
You can create your html img tag with the image data encoded with base64. This way the image data is contained in the html document it self.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
You images are probably only available via filesystem (i.e. their src starts with file).
There are a few ways
Make the image available via the internet: make sure their src starts with http and that they are hosted on a web server visible to the downloader (for example, the same server from which they are dowonloading the image)
Use a library, for example see NuGet
You can inline the images as #DevZer0 suggests.
Based on experience
Is the simplest to implement but has some annoyances (the server needs to be available to the user)
Is probably the best way if you do a lot of Word or Office files manipulation.
Can be done and it would solve the problem, although you wouldn't have a full library to support further use cases.
Use a word document creation library if you really want to have flexibility in creating doc or docx type files. Like all other popular document formats, the structure needs to be accurate enough for the program that opens up the documents. Like you obviously cannot create a PDF file just by setting content type "application/PDF", if your content is not in a structure that PDF reader expects. Content type would just make the browser identify the extension (incorrectly in this case) and download it as a PDF, but its actually simple text. Same goes for MS word or any other format that requires a particular document structure to be parsed and displyed properly.
Since every picture, table is of type shape in Word/Excel/Powerpoint, you could simply add with your program an AlternativeText to your picture, which would actually save a URL of the download URL and when you open, it will retrieve its URL and replace it.
foreach (NetOffice.WordApi.InlineShape s in docWord.InlineShapes)
{
if (s.Type==NetOffice.WordApi.Enums.WdInlineShapeType.wdInlineShapePicture && s.AlternativeText.Contains("|"))
{
s.AlternativeText=<your website URL to download the picture>;
}
}
This would be the C# approach, but would require more time for the picture. If you write a small software for it, which replaces all pictures which contain a s.AlternativeText, you could replace a lot of pictures at same time.
NetOffice.WordApi.InlineShape i=appWord.ActiveDocument.InlineShapes.AddPicture(s.AlternativeText, false, true);
It will look for the picture at that location.
You can do that for your whole document with the 1 loop I wrote you. Means, if it is a picture and contains some AlternativeText, then inside you loop you use the AddPicture function.
Edit: Anoter solution, would be to set a hyperlink to your picture, which would actually go to a FTP server where the picture is located and when you click on the picture, it will open it, means he can replace it by himself(bad, if you have 200 pictures in your document)
Edit according Clipboard:
string html = Clipboard.GetText(TextDataFormat.Html);
File.WriteAllText(temporaryFilePath, html);
NetOffice.WordApi.InlineShape i=appWord.ActiveDocument.InlineShapes.AddPicture(temporaryFilePath, false, true);
The Clipboard in Word is capable to transform a given HTML and when you paste it to transform that table or picture into Word. This works too for Excel, but doesn't for Powerpoint. You could do something like that for your pictures and drag and drop from your database.
I'm trying to use HtmlTextWriter to create a html page which is all working fine until I try to create images into a folder that contains spaces in its file path
C:\Documents and Settings....
What seems to be happening is
m_htmlWriter.AddAttribute(HtmlTextWriterAttribute.Src, imageName);
is converting spaces into %20 which as a result, the file path for the source becomes invalid and results in my webbrowser and installed internet browsers not being able to display said images, and instead displaying the broken image icon/image..
I've tried multiple different things to get this to work including
Uri.UnescapeDataString, including an # symbol infront of the imageName
I've also found that if i copy the link from the page source (C:\Documents%20and%20Settings\... then windows is unable to find the file(expected this)
I'm unable to use HtmlAgilityPack due to restrictions I am under.. Anyone have any ideas?
Just add a boolean parameter to tell the HtmlTextWriter class that you don't want it encoded: -
m_htmlWriter.AddAttribute(HtmlTextWriterAttribute.Src, imageName, false);
There are two similar methods available:
AddAttribute(HtmlTextWriterAttribute, String)
AddAttribute(HtmlTextWriterAttribute, String, Boolean)
Using the second one should fix the problem.
I have a web browser in C# that I want to make navigate to a path (html file) on my local pc.
I tried using this:
if (File.Exists(Path + b.HTML))
{
browserCom1.Navigate(Path + b.HTML);
}
The file Exists, but the browser is keep opening an error of Internet Explorer: "cannot find file:///(my path here)"
It is weird because the file is correct. for example if I use:
System.Windows.Forms.OpenFileDialog browseFile = new
System.Windows.Forms.OpenFileDialog();
browseFile.ShowDialog();
String path = browseFile.FileName;
browserCom1.Navigate(path);
and I select the same file that it tried navigating to before, it works.
If I print the above brwseFile File Name to Console(which is the same as my Path+b.HTML by the way), and copy-paste it into the Navigate(...) Function (changing each '\' to '//') it Doesn't work.
I have no Idea what to do.
I tried something else like:
String path=(File.Open(Path + b.HTML, FileMode.Open).Name);
browserCom1.Navigate(path);
but the application keep getting freezed upon this.
I also tried with new URI(path) and all.
How can I simpley navigate to a HTML file on my computer?
You have http slashes, but should have file system slashes, like c:\something\something.html
I had the same problem. Resolved when I cleaned for double \\ in the code.
If that's not your problem - your problem may be some else problem related to parsing from string to uri.
my path was like this: c:\users\someone1\\myFolder\protocol.htm