VB.NET: Preserve image metadata when moved to clipboard as an image - vb.net

Visual Studio 2010, .NET 4, VB.NET
Hello,
I am writing a little program to convert LaTeX snippets to images which can be pasted into whatever program one can paste images into. It's working alright but the next obvious step is to include the source LaTeX code as a piece of metadata in the image so that the results can be modified without having to retype everything.
I have succeeded in adding a title PropertyItem with the latex encoded as an ASCII byte array as its value (id=800, type=2, value=System.Text.Encoding.ASCII.GetBytes(codestring)). I verify that the PropertyItem is really there before trying to put the image on the clipboard.
Then I do Clipboard.SetImage(myImage). The result is all of the PropertyItems are removed (my title plus anything else that was there)! I check this by doing MsgBox(Clipboard.GetImage.PropertyItems.Count.ToString) which gives zero.
This makes me very sad. Anyone know what's up?
Thanks in advance!
Brian
Update: I have figured out how to move the image onto the clipboard and then back off while preserving the PropertyItems like so:
Format = DataFormats.GetFormat(GetType(Image).FullName)
Dim dataObject As New DataObject
dataObject.SetData(Format.Name, image)
Clipboard.SetDataObject(dataObject)
Dim copiedImage As Image = CType(Clipboard.GetDataObject.GetData(Format.Name), Image)
This way, the copiedImage has the same PropertyItems as the original. However, new problem:
Other programs don't recognize what's on the clipboard as an image anymore, which defeats the whole purpose. I.e., if I put an image on the clipboard this way, when I try pasting into some context that accepts pasted images, nothing happens.
What to do?!

I believe the Windows clipboard image has no metadata. If you change the format of the image to add metadata, it is no longer a clipboard image. If the other programs can accept it, you could copy and paste the image file (instead of the image) to the clipboard, and the metadata will of course be intact when it's read by the target app.

Have you tried Clipboard.SetData or Clipboard.SetDataObject? SetImage only copies the image in bitmap format, so I am not surprised that it strips the property items. You might try:
Clipboard.SetData(DataFormats.EnhancedMetafile, myImage)
or
Clipboard.SetData(DataFormats.MetafilePict, myImage)

Related

Save InlineShape picture to file in Word VBA using WordXML

Reviewing this question Save InlineShape picture to file in Word VBA, the accepted answer works fine for our case but the saved png doesn't have the size used in the document and simply has the full original size.
As in the original question, the alternative that uses ADODB generates some artifacts in the file (white spaces) that I couldn't fix in code (don't know what causes them).
The HTML or the zip ways don't work for me because I have to keep track of each picture's name and location in the document (not that is important for this question but is a constrain of the process, I save those names in a CSV that also relates to the table in which appears the picture).
The way I used to do it was to copy the inlineshape range as a picture and save the clipboard content to a file with a powershell command run from within the vba code, but the antivirus where flagging the code and didn't let it to go through (I can't touch the antivirus, as is a macro for the client to execute)
I have searched but can't find a way to save in a folder the picture (inline shapes) with the size used in the document.
The saved picture could be in png, jpg or bmp.
Edit: I believe that the trick is in how the range is created from the Inline Shape's Range
Dim r As Range
Set r = shp.Range.Duplicate
r.Start = r.Start - 1
r.End = r.End + 1
I don't know if it is possible that when the creation of the duplicated range it could respect the configuration of the original object

Catia Screen Capture Code

Can someone help me with a code for capturing a 250X250 pixcel screen and copying it into clipboard. I would like to paste the image into an excel i can code that part. I have no clue where to start. I have made a code till reframing on the a point where i need the macro to capture.
Dim objviewer3d as viewer3d
Selection1.search("Name="+Textbox1.value+"*,all")
Caita.startcommand("reframe On")
Catia.refreshDisplay=true
Set objviewer3d=catia.activewindow.activeviewer
Objviewer3d.viewpoint3d.zoom=0.017
objviewer3d.update
This is the code i have written. I need the capture code. I Have no clue how to do that. And Last thing I know code to capture full screen and capture to file code. I don't want that.
Thank you
The CATIA method which captures the current window to a file is Window.CaptureToFile:
CATIA.ActiveWindow.ActiveViewer.CaptureToFile catCaptureFormatJPEG,"C:\Temp\Capture.jpg"
This captures the entire window, so to make it 250x250, set the ActiveWindow.Height and .Width to 250 before capturing the image. Then set it back afterwords to it's original size only. Alternatively you can resize the image after you insert it into Excel as a shape.
If you want the tree off you can also call ActiveWindow.Layout = catWindowGeomOnly

Original Image Size

Using interop.powerpoint in VB.Net I am inserting an EMF file with the code:
' add picture shape to slide
AvailableSlide.Shapes.AddPicture(FileName:=file, LinkToFile:=MsoTriState.msoFalse, _
SaveWithDocument:=MsoTriState.msoTrue, Left:=SlideHorizontalPosition, Top:=SlideVerticalPosition)
' save original image size
mOriginalImageWidth = CurrentShape.Width
mOriginalImageHeight = CurrentShape.Height
' if EMF then crop the bottom
CurrentShape.PictureFormat.CropBottom = mCropValue
I have the original size saved before cropping. However, if I'm using a selected image in a PPT file I didn't save, I can't figure out where the original image size is stored in the selected "shape" object (5.49" by 4.13" in the image below). I assumed it would be in the PictureFormat somewhere.
dgp
Set the shape's .ScaleHeight and .ScaleWidth to 1 to return it to the original size.
This may or may not work but it's worth a shot (I can't test because I don't have Powerpoint). After a little bit of research on the PictureFormat interface, it has a few members that might be useful to you. I didn't exactly see anything that specified the picture's original size, but there were these four properties that seemed helpful: CropLeft, CropRight, CropTop, and CropBottom. They return the number of points cropped off each side respectively. A way to get the original size of the object would be to add the Width to CropLeft + CropRight and add the Height to CropTop + CropBottom. Try that and let me know if it works. Documentation is found here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.pictureformat_members(v=office.14).aspx
Try getting the shape's ScaleWidth and ScaleHeight properties. I couldn't find it in the documentation, but awhile ago, I had a student work with me to create an application to format powerpoint presentations that were imported into microsoft word. Here's the code that dealt with the scale size of the shape.

Adding a checkbox symbol using OOo API VB.NET and UNO

I've been searching for a solution for this for quite some time now but with no luck.
I have a PDF that I am generating using VB.NET, the OpenOffice API and UNO. I am generating a text document and I need to be able to insert a checkbox in code.
One possible solution is to change the font to Wingdings and just type 'o' but that solution is neither elegant nor very easy to implement given my environment (using company created code for text document creation and manipulation, have a Write command that will write to the document (strings)).
If it's possible to just add the checkbox to a string of text then that would be perfect.
Have you tried to use an image of a checkbox (one checked and the other unchecked) and then inject that into your PDF, like this:
Caveat - I have worked with iTextSharp so the following code is relevant to iTextSharp, but the concept should translate to whatever PDF generating library/framework you are using
Method #1 - Put checkbox image into table cell
Dim imgCheckBoxChecked As Image = Image.GetInstance(HttpContext.Current.Server.MapPath("checkbox_checked.gif"))
Dim imgCheckBoxUnChecked As Image = Image.GetInstance(HttpContext.Current.Server.MapPath("checkbox_unchecked.gif"))
Dim table As Table
Dim cell As Cell
cell = New Cell(New Paragraph("", font))
'' Add checked or unchecked here
cell.AddElement(imgCheckBoxChecked)
cell.AddElement(imgCheckBoxUnChecked)
table.AddCell(cell)
Method #2 - Put checkbox image into the document
'' Add checked or unchecked here
pdfDoc.Add(imgCheckBoxChecked)
pdfDoc.Add(imgCheckBoxUnChecked)
Note: You will obviously need to find or create the .gif images for checked and unchecked.

Convert text to image in Microsoft Word

I have a large book written in Microsoft Word and want to create a macro that will find all text using a predefined style and convert that text to an inline image. This text will be in Arabic and generally no longer than 4-5 lines. Is this possible?
UPDATE: Here's an example to show what I'm referring to:
I want to replace that entire line in Arabic with an image (as if I cropped this attached image to only include the Arabic and then replaced the line in Arabic with the image).
The reason I want a macro or script to do this is because there are hundreds of such lines and updating them one by one is cumbersome plus that will make modifications difficult later on.
UPDATE2: I found an interesting option here: http://windowssecrets.com/forums/showthread.php/31344-Convert-Text-to-an-Image-of-Text-in-VBA-(Office-2000-Sr1a)
It looks like you can cut a piece of text and then "Paste Special" as an image. So if there's a way to automate that that might work.
This is not an answer although I hope it will grow into a community answer. At the moment it is an exploration of what is required to solve the problem.
I know from the discussion when this question was posted on Super User that Abdullah wishes to publish his book on Kindle. So the question is really about how to get a document in English and Arabic ready for publication as an e-Book.
The Kindle does not support Arabic. The number of languages it does support is slowly increasing but there is no evidence I can find that Amazon has plans to add Arabic in the foreseeable future.
The format behind an Amazon e-Book is a cut down version of HTML. If a Word document containing Arabic letters is exported to HTML, the Arabic letters are included as character entities; for example: “ﭐ &#amp;64337; ﭒ ﭓ”. Importing the original Word or the HTML version to Kindle, results in the leading bits being discarded so these characters are displayed as P, Q, R and S instead of “ﭐ ﭑ ﭒ ﭓ (Alef Wasla isolated form, Alef Wasla final form, Beeh Wasla isolated form and Beeh Wasla final form).
I have tried Abdullah’s idea of saving some Arabic letters in a PNG file and creating an HTML file containing <p> … </p> <img src= “Arabic.png” > <p> … </p>. The appearance of this file on my Kindle 2 is perfectly acceptable so this has the potential to be a solution. The question is: how can the necessary conversions be performed?
We need to extract each Arabic string from either the Word document or its HTML equivalent and import it into a program that can convert them to PNG files.
The only way that I know of automating this would be to copy each string to a slide within PowerPoint. With PowerPoint’s SaveAs option it is possible to save each slide as a separate PNG file. The slides are named: SLIDE1.PNG, SLIDE2.PNG, SLIDE3.PNG and so on in sequence which would allow a macro to relate the results to the original strings. It would then be possible to replace the Arabic strings in the HTML file with the image elements. None of this would be too difficult to automate but there is a problem with the slides all being the size of the PowerPoint page. The page could be made smallish but what we need is for each slide to be cropped to just bigger than that slide’s text. I cannot think of any way of automating this cropping.
Does anyone have a better approach than converting each Arabic phrase to a PNG file?
I have been looking for PNG editors with some sort of command line interface but can find nothing that would be easier than using PowerPoint. Does anyone know of an alternative to PowerPoint?
Does anyone have any suggestions for automating the cropping of each image? When a string is placed in a PowerPoint slide it is possible to set its width to, say, 6.5cm (which looks good on my Kindle) and get the height determined by PowerPoint. This could be saved for later use if anyone knows how to use it.
Implementing solution
Pending any suggestions for improving the approach described above, the following outlines how I would implement it.
I would not attempt to process the Word document. I would save it as a Web Page, Filtered HTML file, which is a required step on the way to creating a Kindle eBook, and process that.
Within the HTML file created from my test document, the Arabic phrase comes out as:
<p class="MsoNormal"></p>
<p class="MsoNormal" align="center" style="text-align:center"><span dir="RTL"
style="font-size:24.0pt;font-family:Arial">
&#64336;&#64337;&#64338;&#64339;&#64340;&#64341;
&#64342;&#64343;&#65153;&#65154;&#65276;&#65275;
&#65274;&#65273;&#65246;&#65226;&#65227;&#65228;
</span><span style="font-size:24.0pt"></span></p>
<p class="MsoNormal"></p>
<p class="MsoNormal"></p>
I assume Abdullah's document will result in something similar. Note 1: the above is a random collection of Arabic letters. Note 2: they are held left-to-right in reading sequence even though, when displayed or printed, they are read right-to-left.
The whole of this block will have to be replaced with something like:
<br><imc src="xxxx.png"><br>
where the file xxxx.png holds an image of the Arabic text.
The file names, such as xxxx.png, could be systematic (A001.png, A002.png, ...) but I would have thought that transliterating the first ten or twenty characters of the phrase from the Arabic to English alphabets and using the result, with a numeric suffix, as the file name would be more convenient.
I would hold the records necessary to manage the process in an Excel worksheet. I would place the VBA code in the same workbook.
The steps in the conversion process that I envisage are:
VBA macro to extract Arabic strings from latest HTML file and add new strings to the Excel worksheet. (More about the Excel worksheet later.)
VBA macro to create PowerPoint file, with one slide per new string, and use SaveAs in PNG format to create one PNG file per slide before discarding the PowerPoint file.
Human to crop each PNG file. (There appears to be no way of automating the cropping so this task will be minimised by use of data in the Excel worksheet.)
VBA macro to rename each slide from SLIDEnnn.PNG to its permanent name and to record the permanent name in the Excel worksheet.
VBA macro to update the latest HTML file by replacing the block containing the Arabic phrase with the appropriate HTML IMG element.
The Excel worksheet needs two columns: Arabic phrase and PNG file name. If there is any risk of the worksheet being sorted between steps 2 and 4, we may need a sequence number as well.
Macro 1 will extract an Arabic phrase from the HTML file, look down the list in the worksheet for this phrase and add the phrase at the bottom if it is not already present.
Macro 2 will look for phrases in the worksheet that do not have a PNG file name. These new phrases are the ones to be written to the PowerPoint presentation. That is, a phrase only goes into this process once.
Task 3, cropping each PNG file, will be a pain. All I can say is that it will only be once per phrase.
Macro 4 will assume that the SLIDE001.PNG, SLIDE002.PNG, … are in the sequence of phrases without PNG files in the worksheet. If this might not be true (because the worksheet has been sorted) we will either need a sequence number or to retain the PowerPoint file. The macro will assign a unique name to each new phrase, record this name in the worksheet and rename the PNG file.
Macro 5 creates a new copy of the latest HTML file using the contents of the worksheet to determine which phrase to replace with which PNG file.
This process is not ideal but it will achieve the desired result and has no obvious complications. Any suggestions for improving it?
Before you begin these instructions, press record in the Microsoft Word macro editor, so you can see what the VBA code is.
I'm wondering if this will be easier if you convert the docx file to .rtf (rich text format) and replace that line with an image? Go to File > Save As.. > name it "old.rtf", then replace the line with an image and Save As.. again and name it "new.rtf" and then download Beyond Compare or your favorite diff program to see what happened. It should be easy to do this pro-grammatically if you choose to. I think working in text would be easier than Microsoft's binary format unless you can find a good library to modify their doc or docx formats.
Sub CopySelPasteAsPicture()
' Take a picture of a selection and paste it at the
' document end
With Selection
.CopyAsPicture
End With
ActiveDocument.Content.Select
With Selection
.Collapse Direction:=wdCollapseEnd
.TypeParagraph
.TypeParagraph
.PasteSpecial DataType:=wdPasteMetafilePicture
End With
End Sub