1. word中插入文件
Code
protectedboolBatchDocAndCsv(stringdocFile,stringcsvFile)
{
if(string.IsNullOrEmpty(docFile)||string.IsNullOrEmpty(csvFile))
{
returnfalse;
}
if(File.Exists(docFile)==false||File.Exists(csvFile)==false)
{
returnfalse;
}
WordClassobj=newWordClass();
obj.OpenDocument(docFile);
try
{
objectClassType="Package";
objectFileName=csvFile;
objectLinkToFile=true;
objectDisplayAsIcon=true;
objectIconFileName="C:\\WINDOWS\\system32\\packager.exe";
objectIconIndex=0;
objectIconLabel=csvFile;
objectwidth=100;
objectleft=0;
objecttop=0;
objectstart=0;
objectend=0;
objectrange=null;
objectfindText="EMBEDDED_CSV";
objectReplacement="";
objectForward=true;
objectWrap=Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
objectFormat=false;
objectMatchCase=false;
objectMatchWholeWord=false;
objectMatchByte=false;
objectMatchAllWordForms=false;
objectMatchSoundsLike=false;
objectMatchWildcards=true;
obj.WordApplication.Selection.Find.ClearFormatting();
if(obj.WordApplication.Selection.Find.Execute(reffindText,refMatchCase,refMatchWholeWord,refMatchWildcards,
refMatchSoundsLike,refMatchAllWordForms,refForward,refWrap,refFormat,refReplacement,refmissing,refmissing,refmissing,refmissing,refmissing))
{
Thread.Sleep(1500);
obj.WordApplication.Selection.InlineShapes.AddOLEObject(refClassType,refFileName,refLinkToFile,refDisplayAsIcon,refIconFileName,refIconIndex,refIconLabel,refmissing);
}
else
{
obj.Close(false);
obj.Dispose();
returnfalse;
}
obj.Close(true);
obj.Dispose();
returntrue;
}
catch(Exceptionex)
{
obj.Close(false);
obj.Dispose();
returnfalse;
}
}
2.pdf中插入文件
需要用到Gnostice PDFtoolkit VCL
Code
protectedboolBatchPdfAndCsv(stringpdfFile,stringcsvFile)
{
if(string.IsNullOrEmpty(pdfFile)||string.IsNullOrEmpty(csvFile))
{
returnfalse;
}
if(File.Exists(pdfFile)==false||File.Exists(csvFile)==false)
{
returnfalse;
}
gtPDFDocumentXPDFDoc=newgtPDFDocumentXClass();
PDFDoc.LoadFromFile(pdfFile);
gtPDFFileAttachmentXFilAtt;
//CreatetheFileAttachmentObject
FilAtt=PDFDoc.CreatePDFFileAttachment();
//SettheFileAttachmentProperties.
stringAttachFile=csvFile;
FilAtt.FileName=AttachFile;
FilAtt.Author="";
FilAtt.Subject="";
FilAtt.Contents="";
FilAtt.IconColor=13;
FilAtt.FileAttachmentIcon=TxgtFileAttachmentIcon.faPushPin;
FilAtt.SetBounds(100,600,100,100);
//InserttheFileAttachment
PDFDoc.InsertFileAttachment(FilAtt,1);
PDFDoc.ShowSetupDialog=false;
PDFDoc.OpenAfterSave=true;
PDFDoc.SaveToFile(pdfFile);
returntrue;
}