我是靠谱客的博主 慈祥橘子,最近开发中收集的这篇文章主要介绍C# Microsoft.Office.Interop.Word 将多个word合成一个并插入图片 转换成pdf,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
最近领导提了个需求 需要把多个word和多个图片 放在一起并且转换成pdf格式
我这里的思路是先把多个word放在一个新的word里
再把图片循环添加到这个新的word文档里
保存之后转换成PDF格式
之前用的spire Doc的这个插件实现的 但是因为这个插件生成后会有水印 虽然它有免费版本的 但是转换pdf格式只会转换3页 超出部分显示不了 所以研究了一下微软的Microsoft.Office.Interop.Word 完美解决
现在还有一个没解决的问题就是怎么设置这个添加图片的位置 如何把两个图片放在同一行中 如果有知道的大佬希望可以得到您的帮助(●’◡’●) 爱你 比心
(需要引用 Microsoft.Office.Interop.Word)
附上代码
/// <summary>
/// 将多个word拼成一个 插入图片后转成PDF
/// </summary>
/// <param name="ImageFileNames">图片路径</param>
/// <param name="OldDocFileName">word路径</param>
/// <param name="NewDocFileName">插入文档之后新的word文档路径</param>
/// <param name="PdfFileName">转换后pdf的路径</param>
/// <returns></returns>
public bool ConverterToPDF(List<string> ImageFileNames, List<string> OldDocFileName, string NewDocFileName, string PdfFileName)
{
Microsoft.Office.Interop.Word.Application App = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document Doc = new Microsoft.Office.Interop.Word.Document();
object lobjMissing = System.Reflection.Missing.Value;
object lobjSaveChanges = null;
try
{
#region 拼接word
object oPageBreak = WdBreakType.wdLineBreak;//接下行合并(LineBreak)
//object oPageBreak = WdBreakType.wdPageBreak;//接下页合并(PageBreak)
object docFileName = null;
object NewdocFileName = null;
OldDocFileName = OldDocFileName.Reverse<string>().ToList();
for (int i = 0; i < OldDocFileName.Count; i++)
{
NewdocFileName = (object)NewDocFileName;
docFileName = OldDocFileName[i];
if (i == 0)
{
Doc = App.Documents.Open(ref docFileName, ref lobjMissing, ref lobjMissing, ref lobjMissing,
ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing,
ref lobjMissing, false, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);
Doc.Activate();
}
else if (i == OldDocFileName.Count - 1)
{
App.Selection.Range.InsertFile(OldDocFileName[i], ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);
App.Selection.Range.InsertBreak(ref oPageBreak);
Doc.SaveAs(ref NewdocFileName, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);
//Doc.Close(ref NewdocFileName, ref lobjMissing, ref lobjMissing);
App.ActiveDocument.Close(ref lobjMissing, ref lobjMissing, ref lobjMissing);
}
else
{
App.Selection.Range.InsertFile(OldDocFileName[i], ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);
App.Selection.Range.InsertBreak(ref oPageBreak);
//Doc.SaveAs(ref NewdocFileName, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);
}
}
#endregion
#region Spire.Doc 添加图片到word(新建段落插入到最后一段)
//ConvertToPDFSpireDOC(ImageFileNames, NewDocFileName);
#endregion
#region Microsoft.Office.Interop.Word 添加图片到文档
打开文档
Doc = App.Documents.Open(NewDocFileName, ref lobjMissing, ref lobjMissing, ref lobjMissing,
ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing,
ref lobjMissing, false, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);
App = Doc.Application;
Microsoft.Office.Interop.Word.Selection selection = App.Selection;
Microsoft.Office.Interop.Word.Sections sel = Doc.Sections;// App.Selection;//段落
//newapp.Selection.EndKey(ref unite, ref nothing);//将光标移至文末
Microsoft.Office.Interop.Word.Section section = sel.Add();
//int sum = 1;
foreach (var item in ImageFileNames.Reverse<string>())
{
//插入图片
//App.ActiveDocument.Shapes.AddPicture(item, ref lobjMissing, ref lobjMissing, 0, ref lobjMissing, ref lobjMissing, ref lobjMissing);
//if (sum == ImageFileNames.Count)
//{
// //selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
// //selection.InlineShapes.AddPicture(item, ref lobjMissing, ref lobjMissing, ref lobjMissing);
// Microsoft.Office.Interop.Word.InlineShape Inlineshape = section.Range.InlineShapes.AddPicture(item, ref lobjMissing, ref lobjMissing, ref lobjMissing);
// Inlineshape.Select();
// Microsoft.Office.Interop.Word.Shape cShape = Inlineshape.ConvertToShape();
// cShape.Top = 900;
//}
//else
//{
Microsoft.Office.Interop.Word.InlineShape Inlineshape = section.Range.InlineShapes.AddPicture(item, ref lobjMissing, ref lobjMissing, ref lobjMissing);
// sum++;
//}
}
Doc.SaveAs(NewDocFileName, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);//插入图片之后保存文档
#endregion
#region Microsoft.Office.Interop.Word 将word保存为pdf
Doc = App.Documents.Open(NewDocFileName, ref lobjMissing, ref lobjMissing,
ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing,
ref lobjMissing, false, ref lobjMissing, ref lobjMissing, ref lobjMissing, ref lobjMissing);//打开新的文档
object lobjFileFormat = WdSaveFormat.wdFormatPDF; //保存格式为PDF
Doc.SaveAs(PdfFileName, lobjFileFormat);//转换成pdf格式
lobjSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
Doc.Close(ref lobjSaveChanges, ref lobjMissing, ref lobjMissing);
Doc = null;
App.Quit(ref lobjSaveChanges, ref lobjMissing, ref lobjMissing);
App = null;
#endregion
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
finally
{
if (Doc != null)
{
Doc.Close(ref lobjSaveChanges, ref lobjMissing, ref lobjMissing);
Marshal.ReleaseComObject(Doc);
Doc = null;
}
if (App != null)
{
App.Quit(ref lobjSaveChanges, ref lobjMissing, ref lobjMissing);
Marshal.ReleaseComObject(App);
App = null;
}
//主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行!
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
最后
以上就是慈祥橘子为你收集整理的C# Microsoft.Office.Interop.Word 将多个word合成一个并插入图片 转换成pdf的全部内容,希望文章能够帮你解决C# Microsoft.Office.Interop.Word 将多个word合成一个并插入图片 转换成pdf所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复