C#-添加、获取、删除附件_第1页
C#-添加、获取、删除附件_第2页
C#-添加、获取、删除附件_第3页
C#-添加、获取、删除附件_第4页
C#-添加、获取、删除附件_第5页
已阅读5页,还剩5页未读, 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#-添加、获得、删除PDF附件

nts;namespaceAddAttachmentPDF{classProgram

{staticvoidMain(string[]args)

(〃创建一个PdfDocument类对象,加载测试文

档}测试

VB.NlmpolmpoNamePdfpdf

〃初Pdf〃将pdf〃保

pdfSys}}试结果:

NETortsSpire.PortsSpire.PespaccAddAtClassProgr

PrivateDimpdfDocumentpdf.LoadFromFil初始化

PdfAttaAttachmenta将文档添加到原.Attachments保存并打开文

档.SavoToFiletcm.DiagnostPdfPdf.Attachmenttachmcnt_PDFramc

SharedSubmpdfAsPdfDf.LoadFromFilf=new

PdfDle(sample,pachment类实例attachment二原PDF文档的附

s.Add(attach档(Attachmenttics.ProcessntsFMain(ByVal

Document=Nie(sample.pDocumentO;pdf);例,加载须要二new

PdfAtt附件集合中hirent);tl.pdf);s.Start(AttargsAsStrNew

PdfDocumpdf)附加的文档achmenl(Newachmenll.pdfringO)ment()

w.pdf);f);DimattachmentAsPdfAttachment=New

PdfAttachment(New.pdf)

pdf.Attachments.Add(attachment)

pdf.SaveToFi1e(Attachment1.pdf)

System.Diagnostics.Process.Start(Attachment!,pdf)

EndSubEndClassEndNamespace1.2以文档注释形式

添加附件C#usingSpire.Pdf;usingSpire.Pdf.Annotations;

usingSpire.Pdf.Graphics;usingSystem;usingSystem.Drawing;

usingSystem.10;namespaceAddAttachmcnt2{class

Program{staticvoidMain(string[]args)

(〃创建一个PdfDocument类对象,加载测试文档

PdfDocumentdoc=newPdfDocument(sample,pdf);//

给添加一个新页面到文档PdfPageBasepage=

doc.Pages.Add();〃添加文本到页面,并设置文本格

式(字体、题号、字体粗细、颜色、文本位置等)

PdfTrueTypeFontfontl=newPdfTrueTypeFont(newFont(Arial,

16f,System.Drawing.FontStyle.Bold));

page.Canvas.DrawString(Attachments:,fontl,

PdfBrushes.CornflowerBlue,newPoint(50,50));

〃将文档作为注释添加到页面PdfTrueTypeFont

font2=newPdfTrueTypeFont(newFont(Arial,12f,

System.Drawing.FontStyle.Bold));PointF

location=newPointF(52,80);〃设置注释标签,

标签内容为作为附件的义档Stringlabel=

sample,docx;byte[]data=

File.ReadAl1Bytes(sample.docx);SizeFsize=

font2.MeasureString(label);font

sam}测试

VB.NlmpolmpolmpolmpolmpolmpoName〃设Rec

pagbout2.Height);Pdfmple.docx,ann

annannann(pa〃保doc

Sys}}试结果:

NETortsSpire.PortsSpire.PortsSpire.PortsSystemorts

System,ortsSystem,espaceAddAt设置注释位置、tangleF

boune.Canvas.Drands=newReAttachmentAndata);

otationl.Colotationl.Flaotationl.Icootationl.TexgeasPdfNew

保存并打开文档.SaveToFiletem.DiagnostPdf

Pdf.AnnotatioPdf.GraphicsDrawing10ttachment2、大小、颜色

nds=newReawString(1abectangleF(bonnotationanlor

Color,ags=PdfAnnon=PdfAttaxt=samplewPagc).Annot档

(Attachmenttics.Processons色、标签类型以ectangleF(lobel,

font2,ounds.Rightnnotationl=Purple;

notationFlagachmentlcon.e.docx;tations.Add(t2.pdf);

s.Start(Att以及显示文本等cation,sizePdfBrushes.M+3,bounds,

newPdfAttags.NoZoom;Graph;annotationl)achment2.pdfe);

MediumPurple.Top,font2.achmentAnnot);f);e,bounds);Height

/2,tation(boundds,ClassProgramPrivate

SharedSubMain(ByVaiargsAsStringO)DimdocAs

PdfDocument=NewPdfDocument(sample.pdf)Dim

pageAsPdfPageBase=doc.Pages.Add()DimfontlAs

PdfTrueTypeFont=NewPdfTrueTypeFont(NewFont(Arial,16F,

System.Drawing.FontStyle.Bolcl))

page.Canvas.DrawString(Attachments:,fontl,

PdfBrushes.CornflowerBlue,NewPoint(50,50))

Dimfont2AsPdfTrueTypeFont=NewPdfTrueTypeFont(New

Font(Arial,12F,System.Drawing.FontStyle.Bold))

DimlocationAsPointF=NewPointF(52,80)Dim

labelAsString=sample,docxDimdataAsByte()

=File.ReadAllBytes(sample,docx)DimsizeAs

SizeF=font2.MeasureString(label)DimboundsAs

RectangleF=NewRectangleF(location,size)

page.Canvas.DrawString(label,font2,PdfBrushes.McdiumPurplc,

bounds)bounds=NewRectangleF(bounds.Right+3,

bounds.Top,font2.Height/2,font2.Height)Dim

annotation!AsPdfAttachmentAnnotation=New

PdfAttachmentAnnotation(bounds,sample,docx,data)

annotationl.Color=Color.Purple

annotation!.Flags=PdfAnnotationFlags.NoZooin

annotationl.Icon=PdfAttachmentlcon.Graph

annotationl.Text=sample,docx(TryCast(page,

PdfNewPage)).Annotations.Add(annotation!)

doc.SaveToFile(Attachment2.pdf)

System.Diagnostics.Process.Start(Attachment2.pdf)

EndSubEndClassEndNamespace2.获得PDF附件2.1

获得文件附件C#usingSpire.Pdf;usingSpire.Pdf.Attachments;

usingSystem;usingSystem.10;namespaceGetAttachment_PDF

classProgram{staticvoidMain(string[]

args){〃创建PDF文档,加载测试文件

PdfDocumentpdf=newPdfDocument();

pdf.LoadFromFi1e(Attachment1.pdf);〃获得文档中

的第一个文件附件PdfAttachmentattachment=

pdf.Attachments[0];〃获得该附件的信息

Console.WriteLine(Name:{0},attachment.FileName);

Console.WriteLine(MimcType:(0),attachment.MimeType);

Console.WriteLine(Description:{0},attachment.Description);

Console.WriteLine(CreationDate:{0},

attachment.CreationDate);

Console.WriteLine(ModificationDate:{0},

attachment.ModificationDate);〃将附件的数据写

入到新义档

File.WriteAllBytes(attachment.FileName,attachment.Data);

Console.ReadKey();}})测试结果:

VB.NlmpolmpolmpolmpoName

NETortsSpire.PortsSpire.PortsSystemortsSystem,espace

GetAtClassProgrPrivateDimpdf

DimConConConConCon

FilConEndSubEndClassPdfPdf.AttachmenlO

ttachmentPDFrameSharedSubmpdfAsPdfDf.LoadFromFilm

attachment

nsole.WriteLinsole.WriteLinsole.WriteLinsole.WriteLinsole.W

riteLile.WriteAHBynsole.ReadKeybntsFMain(ByVaiDocument=

Nle(AttachmeAsPdfAttacine(Name:

{ine(MimeTypine(Descripine(Creatioine(Modificytes(attachmy()

argsAsStrNewPdfDocument1.pdf)chment=pdf{0?,attachpe:{0},

atption:{0},onDcte:{0}cationDate:mcnt.FileNamringO)

ment()f.Attachmenthment.FileNattachment.Mi,attachment),

attachme:{0},attame,attachmets(0.)ame)imeType)

t.Descriptioent.Creationachment.Modient.Data)on)nDate)

ificationDate)EndNamespace2.2获得注释附件C#using

Spire.Pdf;usingSpire.Pdf.Annotations;using

System.Collections.Generic;usingSystem.10;namespace

GetAttachment2{classProgram{staticvoid

Main(string[]args){〃加载PDF文档

PdfDocumentpdf=newPdfDocument();

pdf.LoadFromFile(Attachment2.pdf);〃实例化一

个list并将文档内全部页面的Attachmentannotations添加到该

listListPdfAttachmentAnnotationWidgetattaches

=newListPdfAttachmentAnnotationWidgct();

foreach(PdfPageBasepageinpdf.Pages)

{foreach(PdfAnnotationannotationin

page.AnnotationsWidgct)

{attaches.Add(annotationas

PdfAttachmentAnnotationWidget);}

}〃遍历list,将附件数据写入到新文档

for(inti=0;iattaches.Count;i++)

{File.WriteAlIBytes(attaches[i].FileName,

attaches[i].Data);}}}}测试文

档:

VB.NImpoImpoImpoImpoNamePdfA

NETortsSpire.PortsSpire.PortsSystem,ortsSystem,espace

GetAtClassProgrPrivateDimpdf

DimAttachmcntAnFor

NexForNexEndSubPdf

Pdf.AnnotatioCol1ectionsTOttachment2rameSharedSubmpdf

AsPdfDf.LoadFromFiImattachesAsnnotationWidgrEachpageA

ForEachanattacheNextxtriAsIntegeFile.WriteAxtb

ons.GenericMain(ByVaiDocument=Nie(AttachmesList(Ofget)()

AsPdfPageBannotationAses.Add(TryCaer=0To

atAlIBytes(attargsAsStrNewPdfDocument2.pdf)PdfAttachmase

Inpdf.Ps

PdfAnnotatast(annotatittaches.Countaches(i).FiringO)ment()

mentAnnotatiPagestionInpageion,PdfAttant-1ileName,

attionWidget)e.AnnotationachmentAnnottaches(i).Da=New

LisnsWidgettationWidgetata)st(Of))EndClassEnd

Namespace3.删除PDF附件3.1删除文件附件C#using

Spire.Pdf;namespaceDeleteAttachment_PDF{class

Program{staticvoidMain(string[]args)

{〃加载PDF文档PdfDocumentpdf=

newPdfDocument();

pdf.LoadFromFile(Attachment1.pdf);〃删除文档

的全部文件附件for(inti=0;i

pdf.Attachments.Count;i++)

{pdf.Attachments.RemoveAt(i);

〃保存并打开义档pdf.SaveToFile(Remove,pdf);

System.Diagnostics.Process.Start(Remove,pdf);}

}}VB.NETImportsSpire.PdfNamespace

DeleteAttachmentPDFClassProgramPrivate

SharedSubMain(ByVaiargsAsString())DimpdfAs

PdfDocument=NewPdfDocument()

pdf.LoadFromFile(Attachment!,pdf)ForiAs

Integer=0Topdf.Attachments.Count-1

pdf.Attachments.RemoveAt(i)Next

pdf.SaveToFile(Remove,pdf)

System.Diagnostics.Process.Start(Remove,pdf)End

SubEndClassEndNamespace3.2删除注释附件C#using

Spire.Pdf;usingSpire.Pdf.Annotations;namespace

DeleteAttachment2{classProgram{static

voidMain(string[]args){〃加载PDF文

档PdfDocumentpdf=newPdfDocument();

pdf.LoadFromFile(Attachment2.pdf);〃删除文档

的仝部注释附件foreach(PdfPageBasepagein

pdf.Pages){for(inti=0;i

page.AnnotationsWidget.Count;i++)

{PdfAnnotationannotation=

page.AnnotationsWidget[i]asPdfAttachmentAnnotationWidget;

page.Annotat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论