ASP.NET & Word (Word Application) - Add/Insert Picture (AddPicture) |
ASP.NET & Word (Word.Application) - Add/Insert Picture (AddPicture) ตัวอย่างนี้จะเป็นการเขียน ASP.NET กับ Word แทรกรูปภาพลงในเอกสาร (Insert Picture)
ASP.NET Config Word (Word Application)
Framework 1.1,2.0,3.0,4.0
Word Document for C# อ่านและดัดแปลงได้จากบทความนี้
Sample
Imports Microsoft.Office.Interop.Word
Public Class AspNetWordAddPicture
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Const wdColorDarkRed = &H80
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphLeft = 0
Const wdParagraph = 4
Const wdHorizontalPositionMargin = 0
Const wdTableLeft = -999998
Const wdCollapseEnd = 0
Dim Wrd As New Microsoft.Office.Interop.Word.Application
Dim WrdDoc As Microsoft.Office.Interop.Word.Document
Dim MyRange1, MyRange2, MyRange3 As Microsoft.Office.Interop.Word.Range
Dim DocName As String = "MyDoc/MyWord.doc"
Wrd.Application.Visible = False
WrdDoc = Wrd.Documents.Add()
MyRange1 = WrdDoc.Paragraphs.Add.Range
With MyRange1
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Name = "Verdana"
.Font.Size = "30"
.Font.Bold = True
.InsertBefore(vbCrLf & vbCrLf & "www.ThaiCreate.Com" & vbCrLf & "Version 2009")
End With
MyRange1.InlineShapes.AddPicture(Server.MapPath("thaicreate-2009.gif"))
'WrdDoc.InlineShapes.AddPicture Server.MapPath("thaicreate-2009.gif")
'WrdDoc.Shapes.AddPicture Server.MapPath("thaicreate-2009.gif"),0,0,MyRange1
'WrdDoc.Shapes.AddPicture Server.MapPath("thaicreate-2009.gif"),40,6,Wrd.Selection.Range
MyRange2 = WrdDoc.Paragraphs.Add.Range
With MyRange2
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Name = "Verdana"
.Font.Size = "15"
.Font.Bold = True
.InsertBefore(vbCrLf & vbCrLf & "PHP,ASP and ASP.NET Tutorial")
End With
MyRange2.InlineShapes.AddPicture(Server.MapPath("doc.gif"))
MyRange3 = WrdDoc.Paragraphs.Add.Range
With MyRange3
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Name = "Verdana"
.Font.Size = "10"
.Font.Bold = True
.Font.Color = wdColorDarkRed
.Underline = True
.InsertBefore(vbCrLf & vbCrLf & vbCrLf & vbCrLf & "All Rights Reserved")
End With
WrdDoc.SaveAs(Server.MapPath(DocName))
Wrd.Application.Quit()
Wrd = Nothing
End Sub
End Class
Screenshot
ดาวน์โหลด Source Code ทั้งหมดได้ในส่วนของดาวน์โหลด
|