1. word中插入文件
Code
protected bool BatchDocAndCsv(string docFile, string csvFile)
{
if (string.IsNullOrEmpty(docFile) || string.IsNullOrEmpty(csvFile))
{
return false;
}
if (File.Exists(docFile) == false || File.Exists(csvFile) == false)
{
return false;
}
WordClass obj = new WordClass();
obj.OpenDocument(docFile);
try
{
object ClassType = "Package";
object FileName = csvFile;
object LinkToFile = true;
object DisplayAsIcon = true;
object IconFileName = "C:\\WINDOWS\\system32\\packager.exe";
object IconIndex = 0;
object IconLabel = csvFile;
object width = 100;
object left = 0;
object top = 0;
object start = 0;
object end = 0;
object range = null;
object findText = "EMBEDDED_CSV";
object Replacement = "";
object Forward = true;
object Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object Format = false;
object MatchCase = false;
object MatchWholeWord = false;
object MatchByte = false;
object MatchAllWordForms = false;
object MatchSoundsLike = false;
object MatchWildcards = true;
obj.WordApplication.Selection.Find.ClearFormatting();
if (obj.WordApplication.Selection.Find.Execute(ref findText, ref MatchCase, ref MatchWholeWord, ref MatchWildcards,
ref MatchSoundsLike, ref MatchAllWordForms, ref Forward, ref Wrap, ref Format,ref Replacement, ref missing, ref missing, ref missing, ref missing, ref missing))
{
Thread.Sleep(1500);
obj.WordApplication.Selection.InlineShapes.AddOLEObject(ref ClassType, ref FileName, ref LinkToFile, ref DisplayAsIcon, ref IconFileName, ref IconIndex, ref IconLabel, ref missing);
}
else
{
obj.Close(false);
obj.Dispose();
return false;
}
obj.Close(true);
obj.Dispose();
return true;
}
catch (Exception ex)
{
obj.Close(false);
obj.Dispose();
return false;
}
}
protected bool BatchDocAndCsv(string docFile, string csvFile)
{
if (string.IsNullOrEmpty(docFile) || string.IsNullOrEmpty(csvFile))
{
return false;
}
if (File.Exists(docFile) == false || File.Exists(csvFile) == false)
{
return false;
}
WordClass obj = new WordClass();
obj.OpenDocument(docFile);
try
{
object ClassType = "Package";
object FileName = csvFile;
object LinkToFile = true;
object DisplayAsIcon = true;
object IconFileName = "C:\\WINDOWS\\system32\\packager.exe";
object IconIndex = 0;
object IconLabel = csvFile;
object width = 100;
object left = 0;
object top = 0;
object start = 0;
object end = 0;
object range = null;
object findText = "EMBEDDED_CSV";
object Replacement = "";
object Forward = true;
object Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object Format = false;
object MatchCase = false;
object MatchWholeWord = false;
object MatchByte = false;
object MatchAllWordForms = false;
object MatchSoundsLike = false;
object MatchWildcards = true;
obj.WordApplication.Selection.Find.ClearFormatting();
if (obj.WordApplication.Selection.Find.Execute(ref findText, ref MatchCase, ref MatchWholeWord, ref MatchWildcards,
ref MatchSoundsLike, ref MatchAllWordForms, ref Forward, ref Wrap, ref Format,ref Replacement, ref missing, ref missing, ref missing, ref missing, ref missing))
{
Thread.Sleep(1500);
obj.WordApplication.Selection.InlineShapes.AddOLEObject(ref ClassType, ref FileName, ref LinkToFile, ref DisplayAsIcon, ref IconFileName, ref IconIndex, ref IconLabel, ref missing);
}
else
{
obj.Close(false);
obj.Dispose();
return false;
}
obj.Close(true);
obj.Dispose();
return true;
}
catch (Exception ex)
{
obj.Close(false);
obj.Dispose();
return false;
}
}
2.pdf中插入文件
需要用到Gnostice PDFtoolkit VCL
Code
protected bool BatchPdfAndCsv(string pdfFile, string csvFile)
{
if (string.IsNullOrEmpty(pdfFile) || string.IsNullOrEmpty(csvFile))
{
return false;
}
if (File.Exists(pdfFile) == false || File.Exists(csvFile) == false)
{
return false;
}
gtPDFDocumentX PDFDoc = new gtPDFDocumentXClass();
PDFDoc.LoadFromFile(pdfFile);
gtPDFFileAttachmentX FilAtt;
// Create the File Attachment Object
FilAtt = PDFDoc.CreatePDFFileAttachment();
// Set the File Attachment Properties.
string AttachFile = csvFile;
FilAtt.FileName = AttachFile;
FilAtt.Author = "";
FilAtt.Subject = "";
FilAtt.Contents = "";
FilAtt.IconColor = 13;
FilAtt.FileAttachmentIcon = TxgtFileAttachmentIcon.faPushPin;
FilAtt.SetBounds(100, 600, 100, 100);
// Insert the File Attachment
PDFDoc.InsertFileAttachment(FilAtt, 1);
PDFDoc.ShowSetupDialog = false;
PDFDoc.OpenAfterSave = true;
PDFDoc.SaveToFile(pdfFile);
return true;
}