如何在word和pdf中插入文件

本文介绍如何在Microsoft Word文档中使用VBA代码插入CSV文件,并在PDF文档中使用Gnostice PDFtoolkit VCL组件插入CSV附件。具体包括设置文件属性、插入位置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. word中插入文件

ContractedBlock.gifExpandedBlockStart.gifCode
protected bool BatchDocAndCsv(string docFile, string csvFile)
ExpandedBlockStart.gifContractedBlock.gif    
{
    
        
if (string.IsNullOrEmpty(docFile) || string.IsNullOrEmpty(csvFile))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return false;
        }


        
if (File.Exists(docFile) == false || File.Exists(csvFile) == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return false;
        }


        WordClass obj 
= new WordClass();

        obj.OpenDocument(docFile);

        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                obj.Close(
false);
                obj.Dispose();
                
return false;
            }


            obj.Close(
true);
            obj.Dispose();

            
return true;
        }

        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            obj.Close(
false);
            obj.Dispose();
            
return false;
        }


    }

 

2.pdf中插入文件

需要用到Gnostice PDFtoolkit VCL

ContractedBlock.gifCode

ExpandedBlockStart.gif
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(
100600100100);

        
// Insert the File Attachment

        PDFDoc.InsertFileAttachment(FilAtt, 
1);

        PDFDoc.ShowSetupDialog 
= false;

        PDFDoc.OpenAfterSave 
= true;

        PDFDoc.SaveToFile(pdfFile);

        

        
return true;
    }

转载于:https://www.cnblogs.com/billmo/archive/2009/01/04/1368443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值