[STAThread]
static void Main(string[] args)
{
var dataobject = System.Windows.Forms.Clipboard.GetDataObject();
String[] formats = dataobject.GetFormats();
foreach (String format in formats)
{
var streamTest = (System.IO.Stream)dataobject.GetData(format);
}
}
Answer: as per @gnud: Use System.Windows.Clipboard instead of System.Windows.Forms.Clipboard.
So, this code worked for me to get the contents of the clipboard in the Art::Text clipformat
, modify it, and paste it back in with a different text and "field type". The custom field type shows up in the PPTX file after saving.
I have no idea if this is "safe" or compatible with anything at all - you should probably read the documentation of the drawingml format to figure that out.
var input = System.Windows.Clipboard.GetData("Art::Text ClipFormat") as MemoryStream;
// Take a copy of the data into a writable stream
var strm = new MemoryStream();
input.CopyTo(strm);
strm.Position = 0;
using (var p = new ZipArchive(strm, ZipArchiveMode.Update, leaveOpen: true))
{
var drawingEntryName = "clipboard/drawings/drawing1.xml";
var drawing = p.GetEntry(drawingEntryName);
XDocument doc;
using (var drawingStream = drawing.Open())
{
doc = XDocument.Load(drawingStream);
}
var NS_DrawingMl = XNamespace.Get(@"http://schemas.openxmlformats.org/drawingml/2006/main");
var fieldXmlNode = doc.Descendants(NS_DrawingMl + "fld").Single();
fieldXmlNode.Attribute("type").SetValue("mycustomtype2");
// Replace the ID to avoid two fields with same ID in document. Might cause trouble? I don't know.
fieldXmlNode.Attribute("id").SetValue(Guid.NewGuid().ToString("B").ToUpper());
var tNode = fieldXmlNode.Descendants(NS_DrawingMl + "t").Single();
tNode.Value = "21.05.2021";
using (var drawingSaveStream = drawing.Open())
{
doc.Save(drawingSaveStream);
}
}
strm.Position = 0;
var clipboardData = new DataObject("Art::Text ClipFormat", strm);
System.Windows.Clipboard.Clear();
System.Windows.Clipboard.SetDataObject(clipboardData);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments