How to paste HTML ( tables ) code into Excel or PowerPoint?
I've overcome some issues concerning pasting HTML into Excel and PowerPoint and noticed that a lot of people are asking that.
I'd like to share my research, solution I made out for it.
Let's say we have a html file named html and we would like to access it in Excel, let's do following:
Clipboard.SetText(html);
We copy our html into the Clipboard. The clipboard generates from the html a real table or image/chart from the input file.
System.Threading.Thread.Sleep(2000);
Let's wait a second to have a preview
sheet.Range(cellmapp).PasteSpecial();
Now, we paste the content into a range that we could like to paste it, by defining cellmap.
System.Threading.Thread.Sleep(1000);
Let's wait a second to see the output
sheet.UsedRange.Copy(Missing.Value);
Now, in order to copy the table image into PowerPoint, we must work the with UsedRange.Copy, because it will copy the currently selected Excel area.
In order to check that we paste it into the correct Powerpoint slide
foreach (PowerPoint.Slide slide in presentation.Slides)
{
foreach (PowerPoint.Shape pptshape in slide.Shapes)
{
if(<your condition satisfies>)
{
slide.Select(); // some position in any slide
pptshape.Delete();//delete old content that was in that slide
ppApp.ActiveWindow.View.PasteSpecial(); //paste the Excel content
}
}
}
Of course there are other solutions, like making an image out of the html code and pasting that, which was my initial idea.
Another post refering that manipulation:
Showing HTML in PowerPoint
Related
I have a pdf file created with itextsharp with images in the file. I would like to put a hyperlink in the file that if you pick the picture it will open that picture in a picture viewer. I can set a hyperlink to a web address but have no idea how to get it to open a file. Below is the code, yes I know that c:\test.jpg is a bad hardcoded file name but it is just a test. When you click the picture it does nothing but I have no idea how to tell it what to do.
iTextSharp.text.Image pic =TextSharp.text.Image.GetInstance(comment.examplePic);
pic.ScaleToFit(200f, 200f);
Chunk cImage = new Chunk(pic, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "c:\\test.jpg";
doc.Add(pic);
doc.Add(anchor);
A PDF is self-contained. This means that all the resources needed to show the PDF are (usually) stored inside the PDF (exceptions are for instance fonts that can be retrieved from the operating system).
When you have an image that is shown on a PDF page, the bytes of that image are stored in what we call an Image XObject. An XObject is an object that is external to the page, but that is stored as a separate object inside the PDF file.
You are asking to serve the image bytes stored inside this separate object to a viewer on the operating system. This is impossible. I don't know of any viewer that can take those bytes and somehow forward them to an image viewer.
I can think of three possible workarounds. I don't know if any of these workarounds is acceptable to you.
1. Serve the image online
You could put the image on a server and use the code you have in your snippet to link to that online image. Of course: this will only work if the person viewing the document is online and clicks OK when his viewer asks him if it's OK to link to a resources on the internet.
2. Serve the image as an annotation
In this case, you create an annotation for which you create an appearance that renders that same image XObject in the annotation layer (all annotations are shown on top of the page content). You can easily change the visibility status of an annotation to make it invisible (in your case, this would be the default status) or visible (in your case, this would be triggered by a JavaScript action when clicking the link).
There's an example of such an annotation here: Advertisement. If you open advertisement.pdf, you see an image with a button that says "Close this advertisement". Once you click that, the status of the annotation will be changed to invisible. You could do something similar, but the other way round: click a link to make it visible instead of invisible.
This solution doesn't depend on an external viewer, the image is shown in the PDF viewer.
3. Add the image as optional content
Starting with PDF 1.5, PDF supports optional content. See for instance the OptionalContentExample. In this example, we have some questions and answers, but the answers are not visible by default. See layer_actions.pdf. There are links "on / off / toggle" to make the answers visible or invisible.
You could do the same with images: you could add them to a layer that is invisible by default, but that can be made visible if somebody clicks a link. However: this requires a viewer that supports OCG (optional content groups) and the actions to change the status of these OCGs. For instance: if you would try the layer_actions.pdf example in the PDF viewer in Chrome, it won't work, but if you download the PDF and open it in Adobe Reader, you'll see the behavior I described.
Summarized:
You are asking something that is impossible, but there are workarounds. Please post another question if you have chosen a workaround and you don't succeed in making that workaround word (but please take into account that not all viewers support every workaround).
no offence but too much knowledge sometimes makes you ignorant of small things.
simple solution to this problem is here
http://kuujinbo.info/iTextSharp/imageAnchor.aspx
sample code that i implemented works like charm
PdfPCell p1 = new PdfPCell();
p1 = new PdfPCell();
p1.Padding = 0;
p1.Border = 0;
PdfPTable nav = new PdfPTable(1);
nav.WidthPercentage = 100;
nav.SpacingAfter = 12;
navbarImg.Annotation= new Annotation(0, 0, 0, 0, ur);
p1.Image = navbarImg;
nav.AddCell(p1);
_doc.Add(nav);
My application gets the string from the clipboard:
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
Then it manipulates the data and copies the result string to the clipboard:
Clipboard.SetText(result);
It's supposed to have a table from the Excel worksheet as the input clipboard and a string separated by [\t\r\n] as the output clipboard.
SetText() is supposed to clear the clipboard and I really have the required output clipboard. However when I manually paste it to the same Excel worksheet the previous (input) clipboard content is pasted.
Meanwhile if I previously close my application the correct output string is pasted. Moreover, if I previously finish the Excel process and then start it again the correct output is pasted as well.
Where is the bug?
I noticed that the problem disappears when the application window is closed. Moreover, I decided to change the program logic: if it is supposed to work only with the Excel why not to turn it into the add-in? So, thank you guys for the assistance, the problem is solved for now.
I've got some code that builds a PDF from an HTML template, then attaches several other PDFs to make one big PDF using abcPDF 7.
All this works fine and dandy -- however, I'd like to make some links in the HTML portion of the PDF to jump down to one of the several attached PDFs.
I tried creating links and anchors using the technique referenced here, by putting the
Link to another page
link in the HTML, then putting the anchor
<div><a name="elementId">A div that's on another page</a></div>
as an added-on paste-over on the top of the first page of the PDF I wanted to jump to.
I can see the text of the anchor just fine, and the link to it is blue, but it doesn't do anything.
As the next attempt, I've created bookmarks that work as well. Can someone point me in the direction to go back and adjust the links in the HTML portion to use them to jump to the bookmarks?
I apologize in advance for a lack of code, and I'm not asking for any code now.. I'd just like a more general way to go about it, like "try something like this." I'm not having much luck finding anything that is close to what I'm trying to do, not even on WebSuperGoo's website.
This method has worked for me in the latest ABCpdf version (9) Add a bookmark to each page in your document:
For i = 1 to pdf.PageCount
pdf.PageNumber = i
pdf.AddBookmark("Page " & i, True)
Next
Then where you want to insert a link you can reference the bookmark - in this case we create a table of contents by looping through each bookmark we've created:
For Each bm As Bookmark In pdf.Bookmark
toc &= "<Font annots='goto:" + bm.Page.PageNumber.ToString() + "'>" & bm.Title & "</Font><br>"
Next
pdf.AddHtml(toc)
The Websupergoo team supplied me with some example code and that's what this is based off of - so thanks to them!
I have a WebBrowser displaying text.
If i copy it to clipbaord it copy's all the html tags to and i don't want that.
I want to be able to select all then copy to clipboard.
I want to copy the text and its formatting to the clipboard.
When i highlight the text my self and click copy when i paste, its perfect just how i want it.
But when i use this code to copy just the Document text i get the Html tags to.
This is how i copy to clipboard:
void CopyCellText()
{
Clipboard.Clear();
if (webBrowser1 != null)
{
Clipboard.SetText(webBrowser1.DocumentText.ToString().Trim());
}
}
To Select all and copy to clipboard:
webBrowser1.Document.ExecCommand("SelectAll", true, null);
webBrowser1.Document.ExecCommand("Copy", true, null);
You wont see the html tags but have all there formatting.
You mean you want to convert your html code to text and copy to clipboard? You will need HTML Agility Pack. Check this page for an easy guide.
http://www.dreamincode.net/code/snippet1921.htm << check this code snippet. it would be better, if you strip the string while using regex!
I think the reason you are getting the HTML tags is webBrowser1.DocumentText will take the entire content of the HTML document itself, which will include all the generated HTML.
A quick search gave me the following:
Retrieving Selected Text from Webbrowser control in .net(C#)
Get all text from WebBrowser control
I have two documents, I need to copy a picture from one document to the other. I can't use altChunks because I need to do further editing on the file.
I tried taking a clone of the sdtBlock that the image is in and appending that to the other document.
Like..
sdtBlock = document2.Decendants<StdBlock>.First().Clone(); //Block with image and text etc...
WordprocessingDocument oDoc = WordprocessingDocument.Open(document1, true);
Body body = oDoc.MainDocumentPart.Document.Body;
body.InsertAfter(sdtBlock, body.Elements<Paragraph>().First()); //insert block into new doc
That works for everything except the image. The image appears as either a red X or the document shows as corrupt. If you take the image out this method works fine.
Looking on msdn I think its because I need to create a relationship for that image?
Can anyone show me how this is possible?
Thanks!
Yes, you need to copy the image part across, and add a rel pointing to it. You need to make sure the relId in the paragraph matches the relId of the rel you added.
Have a look at DocumentBuilder in http://powertools.codeplex.com/ for how to do this.