将word文档上传到SQL Server,预先插入水印

时间:2012-03-14 16:08:40

标签: vb.net openxml watermark

这是我将word文档上传到SQL Server,预先插入水印的代码。我已经看过它一旦添加水印,但它已停止这样做,现在它需要原始文件,破坏它,但上传文档的功能副本。我正在严重失去这个阴谋。请帮忙!!!

Option Strict Off
Imports System.Data.SqlClient
Imports FreeTextBoxControls
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml
Imports V = DocumentFormat.OpenXml.Vml
Imports Ovml = DocumentFormat.OpenXml.Vml.Office
Imports Wvml = DocumentFormat.OpenXml.Vml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging
Imports Ap = DocumentFormat.OpenXml.ExtendedProperties
Imports Vt = DocumentFormat.OpenXml.VariantTypes
Imports System.Web.Mvc
Imports System.Collections.Generic
Imports System.Text
Imports DocumentFormat.OpenXml.Vml
Imports DocumentFormat.OpenXml.Vml.Office
Imports DocumentFormat.OpenXml.Vml.Wordprocessing
Imports HorizontalAnchorValues = DocumentFormat.OpenXml.Vml.Wordprocessing.HorizontalAnchorValues

Imports Lock = DocumentFormat.OpenXml.Vml.Office.Lock Imports VerticalAnchorValues = DocumentFormat.OpenXml.Vml.Wordprocessing.VerticalAnchorValues

Partial Class blogupdate
Inherits System.Web.UI.Page
Private UriPartDictionary As System.Collections.Generic.IDictionary(Of System.String, OpenXmlPart) = New System.Collections.Generic.Dictionary(Of System.String, OpenXmlPart)()
Private UriNewDataPartDictionary As System.Collections.Generic.IDictionary(Of System.String, DataPart) = New System.Collections.Generic.Dictionary(Of System.String, DataPart)()
Private document As WordprocessingDocument


Private Sub ChangePackage(filepath As String)

    Dim sourceBytes As Byte() = File.ReadAllBytes(filepath)

    Dim inMemoryStream As New MemoryStream()
    inMemoryStream.Write(sourceBytes, 0, CInt(sourceBytes.Length))

    Dim document = WordprocessingDocument.Open(inMemoryStream, True)

    Dim queue As New System.Collections.Generic.Queue(Of OpenXmlPartContainer)()
    queue.Enqueue(document)

    Dim part
    While queue.Count > 0
        For Each part In queue.Dequeue().Parts
            If Not UriPartDictionary.Keys.Contains(part.OpenXmlPart.Uri.ToString()) Then
                UriPartDictionary.Add(part.OpenXmlPart.Uri.ToString(), part.OpenXmlPart)
                queue.Enqueue(part.OpenXmlPart)
            End If
        Next
    End While

    document.MainDocumentPart.ChangeIdOfPart(UriPartDictionary("/word/theme/theme1.xml"), "generatedTmpID1")
    document.MainDocumentPart.ChangeIdOfPart(UriPartDictionary("/word/fontTable.xml"), "generatedTmpID2")
    document.MainDocumentPart.ChangeIdOfPart(UriPartDictionary("/word/theme/theme1.xml"), "rId8")
    document.MainDocumentPart.ChangeIdOfPart(UriPartDictionary("/word/fontTable.xml"), "rId7")

    Dim headerPart1 As HeaderPart = document.MainDocumentPart.AddNewPart(Of HeaderPart)("headers")
    GenerateHeaderPart1Content(headerPart1)
    ChangeMainDocumentPart1(document.MainDocumentPart)

    document.MainDocumentPart.Document.Save()
    document.Close()

    Using fileStream As New FileStream(filepath, System.IO.FileMode.Create)
        inMemoryStream.WriteTo(fileStream)
    End Using

    Using Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("cerberusConnectionString").ConnectionString)

        Dim strDocids As String
        Dim SQL As String = "INSERT INTO [tbl_wikidocs] ([DocumentTitle],[document],[docmimetype],[filesize]) VALUES (@DocumentTitle, @document, @docmimetype,@filesize)"
        Dim SQL2 As String = "INSERT INTO [tbl_WIKIblog] ([Title],[post]) VALUES (@DocumentTitle)"
        Dim myCommand As New SqlCommand(SQL, Conn)

        Dim docmimetypes As String = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

        myCommand.Parameters.AddWithValue("@DocumentTitle", doctitle.Text.Trim())
        myCommand.Parameters.AddWithValue("@Docmimetype", docmimetypes)
        Dim imageBytes(UploadedFile.PostedFile.InputStream.Length) As Byte
        UploadedFile.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
        myCommand.Parameters.AddWithValue("@document", imageBytes)
        myCommand.Parameters.AddWithValue("@filesize", imageBytes.Length)

        Conn.Open()
        myCommand.ExecuteNonQuery()
        Conn.Close()
        SQL = "select max(docids) from [tbl_wikidocs]"
        Dim myCommand2 As New SqlCommand(SQL, Conn)
        Conn.Open()
        strDocids = myCommand2.ExecuteScalar
        Conn.Close()

        inMemoryStream.Close()
        inMemoryStream.Dispose()
        inMemoryStream = Nothing

        Dim nurl As String = "docview.aspx?docids=" + strDocids
        HyperLink1.NavigateUrl = (nurl)
        HyperLink1.Text = ("Your document can be found here..." + nurl)

        Dim FreeTextBox1 As FreeTextBox = CType(DetailsView1.FindControl("FreeTextBox1"), FreeTextBox)

        FreeTextBox1.Text = FreeTextBox1.Text + "<BR><P>Your document can be found <a href='" + nurl + "'>here</a>."

    End Using

End Sub

Private Sub GenerateHeaderPart1Content(headerPart1 As HeaderPart)
    Dim header1 As New Header()
    header1.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006")
    header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office")
    header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")
    header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math")
    header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml")
    header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing")
    header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word")
    header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")
    header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml")

    Dim paragraph1 As New Paragraph() With { _
     .RsidParagraphAddition = "00D178F6", _
     .RsidRunAdditionDefault = "001028BC" _
    }

    Dim paragraphProperties1 As New ParagraphProperties()
    Dim paragraphStyleId1 As New ParagraphStyleId() With { _
     .Val = "Header" _
    }

    paragraphProperties1.Append(paragraphStyleId1)

    Dim run1 As New Run()

    Dim runProperties1 As New RunProperties()
    Dim noProof1 As New NoProof()

    runProperties1.Append(noProof1)

    Dim picture1 As New Picture()

    Dim shapetype1 As New V.Shapetype() With { _
     .Id = "_x0000_t136", _
     .CoordinateSize = "21600,21600", _
     .OptionalNumber = 136, _
     .Adjustment = "10800", _
     .EdgePath = "m@7,l@8,m@5,21600l@6,21600e" _
    }

    Dim formulas1 As New V.Formulas()
    Dim formula1 As New V.Formula() With { _
     .Equation = "sum #0 0 10800" _
    }
    Dim formula2 As New V.Formula() With { _
     .Equation = "prod #0 2 1" _
    }
    Dim formula3 As New V.Formula() With { _
     .Equation = "sum 21600 0 @1" _
    }
    Dim formula4 As New V.Formula() With { _
     .Equation = "sum 0 0 @2" _
    }
    Dim formula5 As New V.Formula() With { _
     .Equation = "sum 21600 0 @3" _
    }
    Dim formula6 As New V.Formula() With { _
     .Equation = "if @0 @3 0" _
    }
    Dim formula7 As New V.Formula() With { _
     .Equation = "if @0 21600 @1" _
    }
    Dim formula8 As New V.Formula() With { _
     .Equation = "if @0 0 @2" _
    }
    Dim formula9 As New V.Formula() With { _
     .Equation = "if @0 @4 21600" _
    }
    Dim formula10 As New V.Formula() With { _
     .Equation = "mid @5 @6" _
    }
    Dim formula11 As New V.Formula() With { _
     .Equation = "mid @8 @5" _
    }
    Dim formula12 As New V.Formula() With { _
     .Equation = "mid @7 @8" _
    }
    Dim formula13 As New V.Formula() With { _
     .Equation = "mid @6 @7" _
    }
    Dim formula14 As New V.Formula() With { _
     .Equation = "sum @6 0 @5" _
    }

    formulas1.Append(formula1)
    formulas1.Append(formula2)
    formulas1.Append(formula3)
    formulas1.Append(formula4)
    formulas1.Append(formula5)
    formulas1.Append(formula6)
    formulas1.Append(formula7)
    formulas1.Append(formula8)
    formulas1.Append(formula9)
    formulas1.Append(formula10)
    formulas1.Append(formula11)
    formulas1.Append(formula12)
    formulas1.Append(formula13)
    formulas1.Append(formula14)
    Dim path1 As New V.Path() With { _
     .AllowTextPath = True, _
     .ConnectionPointType = Ovml.ConnectValues.[Custom], _
     .ConnectionPoints = "@9,0;@10,10800;@11,21600;@12,10800", _
     .ConnectAngles = "270,180,90,0" _
    }
    Dim textPath1 As New V.TextPath() With { _
     .[On] = True, _
     .FitShape = True _
    }

    Dim shapeHandles1 As New V.ShapeHandles()
    Dim shapeHandle1 As New V.ShapeHandle() With { _
     .Position = "#0,bottomRight", _
     .XRange = "6629,14971" _
    }

    shapeHandles1.Append(shapeHandle1)
    Dim lock1 As New Ovml.Lock() With { _
     .Extension = V.ExtensionHandlingBehaviorValues.Edit, _
     .TextLock = True, _
     .ShapeType = True _
    }

    shapetype1.Append(formulas1)
    shapetype1.Append(path1)
    shapetype1.Append(textPath1)
    shapetype1.Append(shapeHandles1)
    shapetype1.Append(lock1)

    Dim shape1 As New V.Shape() With { _
     .Id = "_x0000_s3074", _
     .Style = "position:absolute;margin-left:-1in;margin-top:-36pt;width:9pt;height:2.25pt;z-index:251659264", _
     .Type = "#_x0000_t136" _
    }
    Dim shadow1 As New V.Shadow() With { _
     .Color = "#868686" _
    }
    Dim textPath2 As New V.TextPath() With { _
     .Style = "font-family:""Calibri"";font-size:1pt;v-text-kern:t", _
     .FitPath = True, _
     .Trim = True, _
     .[String] = "UNCONTROLLED" _
    }

    shape1.Append(shadow1)
    shape1.Append(textPath2)

    picture1.Append(shapetype1)
    picture1.Append(shape1)

    run1.Append(runProperties1)
    run1.Append(picture1)

    Dim run2 As New Run() With { _
     .RsidRunAddition = "00E17A7B" _
    }

    Dim runProperties2 As New RunProperties()
    Dim noProof2 As New NoProof()

    runProperties2.Append(noProof2)

    Dim picture2 As New Picture()

    Dim shape2 As New V.Shape() With {.Id = "762362640", _
     .Style = "position:absolute;margin-left:0;margin-      top:0;width:527.75pt;height:131.95pt;rotation:315;z-index:251658240;mso-position-   horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin",.OptionalString = "_x0000_s3073", .FillColor = "silver", .Stroked = False, .Type = "#_x0000_t136"}
    Dim fill1 As New V.Fill() With {.Opacity = ".5" }
    Dim shadow2 As New V.Shadow() With { _
     .Color = "#868686" _
    }
    Dim textPath3 As New V.TextPath() With { _
     .Style = "font-family:""Calibri"";font-size:1pt;v-text-kern:t", _
     .FitPath = True, _
     .Trim = True, _
     .[String] = "UNCONTROLLED" _
    }
    Dim lock2 As New Ovml.Lock() With { _
     .Extension = V.ExtensionHandlingBehaviorValues.Edit, _
     .AspectRatio = True _
    }
    Dim textWrap1 As New Wvml.TextWrap() With { _
     .Side = Wvml.WrapSideValues.Largest, _
     .AnchorX = Wvml.HorizontalAnchorValues.Margin, _
     .AnchorY = Wvml.VerticalAnchorValues.Margin _
    }

    shape2.Append(fill1)
    shape2.Append(shadow2)
    shape2.Append(textPath3)
    shape2.Append(lock2)
    shape2.Append(textWrap1)

    picture2.Append(shape2)

    run2.Append(runProperties2)
    run2.Append(picture2)

    paragraph1.Append(paragraphProperties1)
    paragraph1.Append(run1)
    paragraph1.Append(run2)

    header1.Append(paragraph1)

    headerPart1.Header = header1
End Sub



Private Sub ChangeMainDocumentPart1(mainDocumentPart1 As MainDocumentPart)
    Dim document1 As Document = mainDocumentPart1.Document

    Dim body1 As Body = document1.GetFirstChild(Of Body)()

    Dim paragraph1 As Paragraph = body1.GetFirstChild(Of Paragraph)()
    Dim sectionProperties1 As SectionProperties = body1.GetFirstChild(Of SectionProperties)()
    paragraph1.RsidParagraphMarkRevision = "00EB482D"
    paragraph1.RsidParagraphProperties = "00EB482D"
    sectionProperties1.RsidRPr = "00EB482D"

    Dim pageSize1 As PageSize = sectionProperties1.GetFirstChild(Of PageSize)()

    Dim headerReference1 As New HeaderReference() With { _
     .Type = HeaderFooterValues.[Default], _
     .Id = "rId6" _
    }
    sectionProperties1.InsertBefore(headerReference1, pageSize1)
End Sub

0 个答案:

没有答案