152 lines
7.3 KiB
VB.net
152 lines
7.3 KiB
VB.net
Imports System.Data.OleDb
|
|
|
|
Imports DevExpress.Office.Services
|
|
'Imports DevExpress.Web
|
|
Imports DevExpress.XtraPrinting
|
|
Imports DevExpress.XtraRichEdit
|
|
Imports DevExpress.XtraRichEdit.Native
|
|
Imports DevExpress.XtraRichEdit.Model
|
|
Imports DevExpress.XtraReports.UI
|
|
Imports System.Data
|
|
|
|
Namespace Utilidades
|
|
Public Class Docx
|
|
'Public Shared Function CombinaDocx(tabla As DataTable, Plantilla() As Byte, FormatoPDF As Boolean, Optional NumRegBloque As Integer = 1000) As Byte()
|
|
' Try
|
|
|
|
' If FormatoPDF Then
|
|
' ' Throw New Exception("Exportación a pdf aún no soportada")
|
|
' Dim iNumBloques As Integer = (Int((tabla.Rows.Count - 1) / NumRegBloque)) - 1
|
|
' If tabla.Rows.Count Mod NumRegBloque > 1 Then iNumBloques += 1
|
|
' Dim sp(iNumBloques) As String
|
|
' Dim sd(iNumBloques) As String
|
|
' Dim p As New IO.MemoryStream(Plantilla)
|
|
' Dim iRegIni As Integer
|
|
' Dim iRegFin As Integer
|
|
' For i = 0 To iNumBloques
|
|
' sd(i) = tsUtilidades.Utilidades.ObtieneFicheroAleatorio("docx")
|
|
' sp(i) = tsUtilidades.Utilidades.ObtieneFicheroAleatorio("pdf")
|
|
' Dim fs As New IO.FileStream(sd(i), IO.FileMode.CreateNew, IO.FileAccess.Write)
|
|
' iRegIni = i * NumRegBloque
|
|
' iRegFin = Math.Min((i * NumRegBloque) + NumRegBloque - 1, tabla.Rows.Count - 1)
|
|
' Debug.WriteLine(Now.ToString & " Bloque " & i.ToString & " " & iRegIni & "-" & iRegFin)
|
|
' p = New IO.MemoryStream(Plantilla)
|
|
' Utilidades.Docx.Combinar(p, tabla, fs, iRegIni, iRegFin, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
|
' fs.Close()
|
|
' GC.Collect()
|
|
' GC.WaitForPendingFinalizers()
|
|
' Debug.WriteLine(Now.ToString & " Bloque " & i.ToString & " " & iRegIni & "-" & iRegFin & " a pdf")
|
|
' Utilidades.Docx.ExportarApdf(sd(i), sp(i))
|
|
' Debug.WriteLine(Now.ToString & " Fin Bloque " & i.ToString & " " & iRegIni & "-" & iRegFin & " a pdf")
|
|
' GC.Collect()
|
|
' GC.WaitForPendingFinalizers()
|
|
' ' fs.Close()
|
|
' Next
|
|
' Dim msPdfUnidos As New IO.MemoryStream
|
|
' msPdfUnidos = tsUtilsPDF.pdf.UnePdfsITS(sp)
|
|
' Return msPdfUnidos.ToArray
|
|
' Else
|
|
' Dim sdocx As String
|
|
' Do
|
|
' sdocx = tsUtilidades.Utilidades.ObtieneFicheroAleatorio("docx")
|
|
' Loop Until Not IO.File.Exists(sdocx)
|
|
|
|
' Dim fs As New IO.FileStream(sdocx, IO.FileMode.CreateNew, IO.FileAccess.Write)
|
|
|
|
' Utilidades.Docx.Combinar(New IO.MemoryStream(Plantilla), tabla, fs, 0, tabla.Rows.Count, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
|
' fs.Close()
|
|
' Return IO.File.ReadAllBytes(sdocx) ' tsUtilidades.Ficheros.FicheroAArrayBytes(sdocx)
|
|
' End If
|
|
' Catch ex As Exception
|
|
' Throw New Exception(ex.Message, ex)
|
|
' End Try
|
|
'End Function
|
|
|
|
Public Shared Sub Combinar(Plantilla As IO.Stream, Datos As DataTable, Destino As IO.Stream, PrimerRegistro As Integer, UltimoRegistro As Integer, Formato As DevExpress.XtraRichEdit.DocumentFormat)
|
|
Try
|
|
Dim docServer As New RichEditDocumentServer
|
|
docServer.LoadDocument(Plantilla, Formato)
|
|
Dim options = docServer.CreateMailMergeOptions()
|
|
options.FirstRecordIndex = PrimerRegistro
|
|
options.LastRecordIndex = UltimoRegistro
|
|
options.MergeMode = API.Native.MergeMode.NewSection
|
|
docServer.Options.MailMerge.DataSource = Datos
|
|
docServer.Options.MailMerge.ViewMergedData = True
|
|
docServer.Options.Export.Html.EmbedImages = True
|
|
docServer.Options.MailMerge.ActiveRecord = 0
|
|
docServer.MailMerge(options, Destino, Formato)
|
|
docServer.Dispose()
|
|
GC.Collect()
|
|
GC.WaitForPendingFinalizers()
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Sub
|
|
Public Shared Sub ExportarApdf(FicheroOrigen As String, FicheroDestino As String)
|
|
Try
|
|
Dim docServer As New RichEditDocumentServer
|
|
docServer.LoadDocument(FicheroOrigen, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
|
docServer.Options.Export.Html.EmbedImages = True
|
|
Dim fs As New IO.FileStream(FicheroDestino, IO.FileMode.Create, IO.FileAccess.Write)
|
|
docServer.ExportToPdf(fs)
|
|
fs.Close()
|
|
docServer.Dispose()
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Public Shared Sub ExportarApdf(Documento As IO.Stream, Destino As IO.Stream)
|
|
Try
|
|
Dim docServer As New RichEditDocumentServer
|
|
docServer.LoadDocument(Documento, DevExpress.XtraRichEdit.DocumentFormat.OpenXml)
|
|
docServer.Options.Export.Html.EmbedImages = True
|
|
|
|
Dim po As New PdfExportOptions
|
|
po.Compressed = True
|
|
'po.PageRange = "1-10"
|
|
|
|
docServer.ExportToPdf(Destino, po)
|
|
docServer.Dispose()
|
|
GC.Collect()
|
|
GC.WaitForPendingFinalizers()
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Sub
|
|
|
|
End Class
|
|
Public Class Pdf
|
|
Public Shared Sub DocumentoApdf(DocumentoOrigen As String, TipoDocumentoOrigen As DevExpress.XtraRichEdit.DocumentFormat, PdfDestino As String)
|
|
Try
|
|
Dim docServer As New RichEditDocumentServer
|
|
Dim fsOrigen As New IO.FileStream(DocumentoOrigen, IO.FileMode.Open, IO.FileAccess.Read)
|
|
Dim fsDestino As New IO.FileStream(PdfDestino, IO.FileMode.Create, IO.FileAccess.ReadWrite)
|
|
DocumentoApdf(fsOrigen, TipoDocumentoOrigen, fsDestino)
|
|
fsDestino.Close()
|
|
fsOrigen.Close()
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Sub
|
|
Public Shared Sub DocumentoApdf(DocumentoOrigen As IO.Stream, TipoDocumentoOrigen As DevExpress.XtraRichEdit.DocumentFormat, PdfDestino As IO.Stream)
|
|
Try
|
|
Dim docServer As New RichEditDocumentServer
|
|
docServer.LoadDocument(DocumentoOrigen, TipoDocumentoOrigen)
|
|
docServer.Options.Export.Html.EmbedImages = True
|
|
|
|
Dim po As New PdfExportOptions
|
|
po.Compressed = True
|
|
|
|
docServer.ExportToPdf(PdfDestino, po)
|
|
docServer.Dispose()
|
|
GC.Collect()
|
|
GC.WaitForPendingFinalizers()
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Sub
|
|
|
|
|
|
End Class
|
|
End Namespace |