问题描述
我发现了类似的问题,但不完全相同。
在用户界面上,有一个文本字段和两个签名字段(生成图像文件的第 3 方组件)。
如果文本不是很长,它会传递两个版本的单词。但是如果文本很长并且有一些输入,它在 Office 2019 和 Office 365 上不起作用。在 Office 2016 上,它始终有效。
为了更好地解释,
我打开文档:
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
...
app = new Microsoft.Office.Interop.Word.Application();
doc = app.Documents.Open(tempPath);
app.Visible = false;
doc.Bookmarks["comment"].Select();
app.Selection.TypeText(orderComment); //Order comment is typed by the user
...
//this code saves the signature as a png image and it works in any case. The image exists in the folder before calling the rest of the code.
string clientSignaturePath = System.Configuration.ConfigurationManager.AppSettings["TempPath"] + Guid.NewGuid().ToString().Substring(0,6) + ".png";
using (FileStream fs = new FileStream(clientSignaturePath,FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(model.ClientSignature);
bw.Write(data);
bw.Close();
}
fs.Close();
}
//If the orderComment is too long,it gives this error in this method when I call the line rng.Paste(); on Office 2019 and 365 but not on 2016.
error : this method or property is not available because the clipboard is empty or invalid
UserMethods.Insertimage(doc,clientSignaturePath,"client",79,175);
在类 UserMethods 中:
public static void Insertimage(Microsoft.Office.Interop.Word.Document doc,string imagePath,string type,float? imageHeight = null,float? imageWidth = null)
{
Range rng = null;
if (type == "tech")
rng = doc.Tables[7].Cell(1,1).Range;
else if (type == "client")
rng = doc.Tables[7].Cell(1,2).Range;
else
rng = doc.Tables[7].Cell(1,3).Range;
Microsoft.Office.Interop.Word.Inlineshape autoScaledInlineshape = rng.Inlineshapes.AddPicture(imagePath);
float scaledWidth = imageWidth ?? autoScaledInlineshape.Width;
float scaledHeight = imageHeight ?? autoScaledInlineshape.Height;
autoScaledInlineshape.Delete();
// Create a new Shape and fill it with the picture
Microsoft.Office.Interop.Word.Shape newShape = doc.Shapes.AddShape(1,scaledWidth,scaledHeight);
newShape.Fill.UserPicture(imagePath);
// Convert the Shape to an Inlineshape and optional disable Border
Microsoft.Office.Interop.Word.Inlineshape finalInlineshape = newShape.ConvertToInlineshape();
//finalInlineshape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
// Cut the range of the Inlineshape to clipboard
finalInlineshape.Range.Cut();
// And paste it to the target Range
rng.Paste();
}
服务器的(Windows Server 2016)官方版本在大文本的情况下不起作用:
提前致谢。
解决方法
Cut 方法可能会导致与剪贴板访问相关的安全问题
试试
rng.FormattedText = finalInlineShape.Range.FormattedText;
finalInlineShape.Delete();
和评论;
//finalInlineShape.Range.Cut();