How to programatically (VBA/NetOffice) insert an Bitmap Excel Object? - c#

I have the requirement to create an Bitmap Object from a Excel Path inside a ms-word document, and I can't seem to find a way to do it in one step.
The current solution is the following.
Create a Field Object
Unlink the Field Object
Like this:
Dim fld As Field
Set fld = Selection.Fields.Add(Selection.Range, WdFieldType.wdFieldLink, "Excel.Sheet.12 ""[Path to Xlsx]"" ""[Sheet+Range]"" \b", False)
fld.Update
fld.Unlink
\b says the field should represented as a Bitmap object.
With fld.Unlink I am dropping the link and the Bitmap stays.
Is there a way to directly create the Bitmap Object (InlineShape) without the field approach?
I tried to record a macro in ms-word to see how ms-word is doing it but saddly it only gives:
Selection.PasteSpecial Link:=False, DataType:=wdPasteBitmap, Placement:= wdInLine, DisplayAsIcon:=False
If possible I would like to avoid working with Clipboard and PasteSpecial.
Edit:
Since the given answer has shown me that my question wasn't as clear as I hoped it to be.
So first of all, the only Information I currently have are
The path to an XLSX file
The Sheet and Range of said XLSX File
eg.
C:\test\myWorkbook.xlsx
Sheet1!A1:D10
Thats all the Information I have.
With this Information I need to programatically create a Bitmap inside my ms-word document.
As shown above I already have a solution which does exactly this, by doing a LINK to the said XLSX file and after it got updated dropping that link to only have the Bitmap left.
A Bitmap is staying here because of the \b flag which tells the LINK to produce a Bitmap, instead of a RTF/HTML.
My question now is, if there is another way I am currently doing it.
The current answer, does indeed insert a Bitmap into my ms-word document, but where do I get the Bitmap from, as the answer isn't aware of the fact that I need an image of an excel workbook? As I said earlier, I only have limited information and no Bitmap.
What I want to achieve can be done without programatically by using the Paste Special functionality
https://support.office.com/en-us/article/paste-special-e03db6c7-8295-4529-957d-16ac8a778719
Open Xlsx/Docx
Copy Cells from Xlsx
Go to Docx
Use Paste Special
Select Bitmap
Hit OK
The output you receive in your Docx is what I desire to make programatically, without Clipboard and Paste Special and with a, if possible, better solution than I currently have.

You can use the following code:
string fileName = "c://msdn.bmp"; //the picture file to be inserted
Object oMissed = doc.Paragraphs[2].Range; //the position you want to insert
Object oLinkToFile = false; //default
Object oSaveWithDocument = true;//default
doc.InlineShapes.AddPicture(fieldName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
See insert a picture into a word document using c#? for more information.

Related

Embed file in excel worksheet/cell

I hope someone can help me. Is there a way to embed a specific file (.txt) into an excel cell? I'm currently using epplus, and I would like to embed programmatically a file into a specific excel cell. I did manage to add a hyperlink, but my goal is to have it embedded.
Worksheet.Cells[rowNumber, colNumber].Value = ....
Is there any way to do it? I couldn't find anything online.
As mentioned in the comments, you can certainly put text within a cell, but bear in mind Excel does have a limit to the number of characters it will allow in a single cell. It's pretty large, but conceivably the contents of a text file could exceed that limit -- even if future versions of Excel keep increasing what the limit is (as they have in the past).
You can also embed an OLE object in your worksheet, and a text file qualifies for that. I don't know that you can assign it to a cell, per se. You can change the location, shape and behavior to fit in a cell and behave as though it's part of a cell, but I don't know that it ever belongs to a range the way formulas do. I could be wrong.
The basic construct of how to embed an OLE object into a worksheet is as follows:
Excel.OLEObject ole = ws.OLEObjects().Add(Filename: #"C:\Users\hambone\Documents\foo.txt");
This is the equivalent of the VBA:
Set ole = sh.OLEObjects.Add(Filename:="C:\Users\hambone\Documents\foo.txt")
The method returns an OLEObject object, which you can then shape to behave the way you want:
ole.Height = 5;

How can I parse through a table in a pdf file?

I have a custom table with name, firstname, place of birth and place of living in a PDF file which I want to parse through in C#. One of the simplest way of doing it would be:
using (PdfLoadedDocument document = new PdfLoadedDocument("foobar"))
{
for (var i = 0; i < document.Pages.Count; i++)
{
Console.WriteLine($"============ PAGE NO. {i+1} ============");
Console.WriteLine(document.Pages[i].ExtractText());
}
}
But the problem is the output:
============ PAGE NO. 38 ============
John L.SmithSan Francisco5400 Baden
There's no way I can seperate this with a regex so I need a way to parse through each column of each row in order to get all the values of the customers separated. How can I parse through a table in a pdf file with syncfusion?
You will need a methods that returns you the coordinate of each character found in the pdf. Then you have some math to do (basically to compute the distance between characters) in order to know if the character is part of a word and where the word itself is located along the x-axe. It requires quite a lot of work and efforts and I didn't find such a method in syncfusion documentation.
I wrote a class which do what you want but this is for java project:
PDFLayoutTextStripper (upon PDFBox)
Syncfusion control extracting the text from PDF document based on the structure of content present in the PDF document. So, based on current implementation of Syncfusion control we cannot recognize the rows and columns present in the table of the PDF document.
Also, it is not possible to extract the text in correct order as same as the PDF document displayed using Syncfusion control since the content present in the PDF document follows fixed layout.
But we can populate the table of the PDF document in Excel using Tabula (Open source library). I have modified the Tabula java (Open Source) to achieve layout based text extraction from the PDF document based on your requirement.
Please find the sample for this implementation in below link:
http://www.syncfusion.com/downloads/support/directtrac/171585/ze/TextExtractionSample649531336
Kindly ensure the following things before executing the sample:
Install Java Runtime Environment (JRE) from the below link.
http://www.oracle.com/technetwork/java/javase/downloads/
Restart your machine.
Execute the above sample.
Try this and check whether it meets your requirement.

Save bitmap to any format

I'm kind of confused as to the way C# saves bitmaps. I currently use this statement to save a bitmap to (virtually) any extension (.ico, .dds, etc.).
Bitmap.Save(FileName);
This actually works for any extension I choose, but I'm having doubts it's saving in correct format. Reason for said doubts is because I found another article here,
How to save Bitmap as icon?, which does so like:
// Create a Bitmap object from an image file.
Bitmap bmp = new Bitmap(sFn);
// Get an Hicon for myBitmap.
IntPtr Hicon = bmp.GetHicon();
// Create a new icon from the handle.
Icon newIcon = Icon.FromHandle(Hicon);
//Write Icon to File Stream
System.IO.FileStream fs = new System.IO.FileStream(destFileName, System.IO.FileMode.OpenOrCreate);
newIcon.Save(fs);
Which is obviously way different. So two questions:
How can/does the first approach work if the second is the "correct" way?
Would I need to customize save properties for each individual extension I wish to support or is the first method sufficient for any extension?
The proper way would be to use the original Bitmap.Save(Filename) but you are missing the ImageFormat variable which will basically do the actualy Conversion from an image obejct ot the specified format.
Currently the way you are using it results in a file named "somefile.somextension" but does not have the actual contents of an icon object for example.
best way would be to use :
Bitmap.Save(String, ImageFormat)
for example, to save a .ico file :
Bitmap.Save(String, System.Drawing.Imaging.ImageFormat.Icon)
Hope this helps, good luck.

Make every Copy and cut paste opearation Paste Special

I am working on C# VSTO(excel). I have created excel workbook project.
I have been trying to implement Paste Special for my workbook.
Manually, we do paste special like This
But i need C# code to do the same.
Is there any way so i can apply it for every copy/cut paste operation in my excel workbook?
I don't want to use VBA Macros as it asks everytime user to whether he wants to ENABLE MACROS OR NOT and hence is there another way o accomplish this.
Have you tried PasteSpecialmethod over ranges, it provides numerous options to copy formats/ column widths/ fomats with values etc.
Also there is another copy special to copy/paste as picture.
to copy Range:
Range.CopyPicture(xlPrinter, xlPicture);
// the range gets copied in clipboard
// there are options available like xlScreen in case you want to copy as it appears on screen.
Sheet.Paste()
// this will paste the shape in the sheet, to paste in some range you could use range.Paste()

Is there an expectable function to find out what type an image is in C# using OpenXML?

I hooked into the question located at "Replace image in word doc using OpenXML". I noticed that there are several ImagePartType types. Is there any easy "built in" way to determine which type a specific image should be other than going by it's extension? The ImagePartType enum is also used in PowerPoint as is alot of the WordProcessingML structures.,
For instance,
ImagePartType.Bmp on image1.bmp
ImagePartType.Emf on image1.emf
ImagePartType.Gif on image1.gif
ImagePartType.Icon on image1.ico
ImagePartType.Jpeg on image1.jpeg or image1.jpg
ImagePartType.Pcx on image1.pcx
ImagePartType.Png on image1.png
ImagePartType.Tiff on image1.tiff or image1.tif
ImagePartType.Wmf on image1.wmf
There is not - because you can have filename.png which is actually a bmp file and it all works fine. The only way to know for sure is to read the actual bitmap file and see what format it is in.
You can do this either by loading it into an Image class, or just read the first couple of bytes and look for the signature of each of the formats.

Categories

Resources