How to convert PDF pages to PNG using Ghostscript Wrapper VB.NET? - asp.net

I'm trying convert from digitally signed PDF to PNG using this solution: Simple VB.Net Wrapper for Ghostscript Dll. It doesn't work for me too. I'm using Ghostscript 9.18 32bits.
First, the source code goes to InOutErrCallBack method in this part:
gsapi_init_with_args(InstanceHndl, NumArgs + 1, Args)
When this occurr, the execution freeze. After this occurs, the next calls to RunGS method returns false in this part:
If gsapi_new_instance(InstanceHndl, IntPtr.Zero) <> 0 Then
Return False
Exit Function
End If
The return value of gsapi_new_instance is -100:
Ghostscript API - Return codes
The gswin32.dll is in C:\Windows\System32 and BIN project folder.
My parameter sequence:
Dim outputImgPath As String
outputImgPath = "C:\Download\DocumentosV2\Protocolo\Pronunciamento\" + Guid.NewGuid.ToString("N") + ".png"
Dim args() As String = { _
"-dNOPAUSE", _
"-dBATCH", _
"-dSAFER", _
"-dQUIET", _
"-sDEVICE=png16m", _
String.Format("-r{0}", resolucao), _
"-dTextAlphaBits=2", _
"-dGraphicsAlphaBits=2", _
String.Format("-dFirstPage={0}", pageNumber), _
String.Format("-dLastPage={0}", pageNumber), _
String.Format("-sOutputFile={0}", outputImgPath), _
"-f", _
pdfPath _
}
UPDATE
#kens advise me for some arguments. So, I remove it to test.
My Complete code:
Imports PdfSharp
Imports System
Imports System.Collections
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing
'''
''' http://www.geekscrapbook.com/2009/11/16/c-method-to-add-an-image-to-a-pdf/
'''
Public Class PDF2Image2PDF
Inherits Simp.Net.Infra.SimpComponent
Private SyncRoot As New Object
'Converte cada página do PDF em imagem
Public Function Pdf2Png(ByVal pdfPath As String, ByVal resolucao As Int32) As Image()
Dim outputImgPath As String
Dim pageCount As Integer
Dim objPdfReader As New iTextSharp.text.pdf.PdfReader(pdfPath)
pageCount = objPdfReader.NumberOfPages()
Dim objImages As New ArrayList
For pageNumber As Int32 = 1 To pageCount
outputImgPath = "C:/Download/DocumentosV2/Protocolo/Pronunciamento/" + Guid.NewGuid.ToString("N") + ".png"
Dim objFileStream As FileStream
Dim objMemoryStream As MemoryStream
'Dim args() As String = { _
' "-dNOPAUSE", _
' "-dBATCH", _
' "-dSAFER", _
' "-dQUIET", _
' "-sDEVICE=png16m", _
' String.Format("-r{0}", resolucao), _
' "-dTextAlphaBits=2", _
' "-dGraphicsAlphaBits=2", _
' String.Format("-dFirstPage={0}", pageNumber), _
' String.Format("-dLastPage={0}", pageNumber), _
' String.Format("-sOutputFile={0}", outputImgPath), _
' "-f", _
' pdfPath _
' }
Dim args() As String = { _
"-dNOPAUSE", _
"-dBATCH", _
"-sDEVICE=png16m", _
String.Format("-dFirstPage={0}", pageNumber), _
String.Format("-dLastPage={0}", pageNumber), _
String.Format("-sOutputFile={0}", outputImgPath), _
Replace(pdfPath, "\", "/") _
}
If GhostscriptDllLib.RunGS(args) Then
If File.Exists(outputImgPath) Then
objFileStream = New FileStream(outputImgPath, FileMode.Open)
Dim length As Int32 = objFileStream.Length
Dim bytes(length) As Byte
objFileStream.Read(bytes, 0, length)
objFileStream.Close()
objMemoryStream = New MemoryStream(bytes, False)
objImages.Add(Image.FromStream(objMemoryStream))
Else
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
Else
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
Next
Return CType(objImages.ToArray(GetType(Image)), Image())
End Function
'Converte cada imagem do vetor em uma página do PDF
Public Function Images2PDF(ByVal imagens() As Image) As PdfDocument
Dim pdf As PdfDocument
Dim gfx As XGraphics
Dim ximg As XImage
pdf = New PdfDocument
For Each img As Image In imagens
pdf.AddPage(New PdfPage)
gfx = XGraphics.FromPdfPage(pdf.Pages.Item(0))
ximg = XImage.FromGdiPlusImage(img)
gfx.DrawImage(ximg, 0, 0)
ximg.Dispose()
gfx.Dispose()
Next
Return pdf
End Function
End Class
The Caller code:
Public Sub DownloadPeticionamento(ByVal sender As System.Object, ByVal e As System.web.UI.ImageClickEventArgs)
Dim imagem As ImageButton = DirectCast(sender, ImageButton)
Dim pdfPath As String = imagem.DescriptionUrl
Dim objPdfPeticionamento As New Simp.Net.Negocio.PDF2Image2PDF
Dim objImages() As Image
Dim objPdfDoc As PdfDocument
objImages = objPdfPeticionamento.Pdf2Png(pdfPath, 600)
objPdfDoc = objPdfPeticionamento.Images2PDF(objImages)
objPdfDoc.Save(Me.Page.Response.OutputStream, True)
End Sub
I test this in command line to:
gswin32c -dNOPAUSE -dBATCH -sDEVICE=png16m -sOutputFile=C:/Download/DocumentosV2/Protocolo/Pronunciamento/fb21872746b64f8fb31b3764b5444e2e.png C:/Upload/DocumentosV2/Protocolo/Pronunciamento/3_0_404702190_2016_10081288_230_0_f1f09b4b38ac49a8a3e5576f6041eea3.pdf
Via command line, the pdf is converted. The command line output:
C:\Documents and Settings\leandro.ferreira>gswin32c -dNOPAUSE -dBATCH -sDEVICE=png16m -dFirstPage=1 -dLastPage=1 -sOutputFile=C:/Download/DocumentosV2/Protocolo/Pronunciamento/fb21872746b64f8fb31b3764b5444e2e.png C:/Upload/DocumentosV2/Protocolo/Pronunciamento/3_0_404702190_2016_10081288_230_0_f1f09b4b38ac49a8a3e5576f6041eea3.pdf
GPL Ghostscript 9.16 (2015-03-30)
Copyright (C) 2015 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
Can't find (or can't open) font file %rom%Resource/Font/Arial-BoldMT.
Can't find (or can't open) font file Arial-BoldMT.
Can't find (or can't open) font file %rom%Resource/Font/Arial-BoldMT.
Can't find (or can't open) font file Arial-BoldMT.
Querying operating system for font files...
Didn't find this font on the system!
Substituting font Helvetica-Bold for Arial-BoldMT.
Loading NimbusSan-Bol font from %rom%Resource/Font/NimbusSan-Bol... 4047240 2487522 8937716 7603550 3 done.
Can't find (or can't open) font file %rom%Resource/Font/ArialMT.
Can't find (or can't open) font file ArialMT.
Can't find (or can't open) font file %rom%Resource/Font/ArialMT.
Can't find (or can't open) font file ArialMT.
Didn't find this font on the system!
Substituting font Helvetica for ArialMT.
Loading NimbusSan-Reg font from %rom%Resource/Font/NimbusSan-Reg... 4080352 2580805 9014744 7662839 3 done.
C:\Documents and Settings\leandro.ferreira>

-100 is simply 'fatal error' ie something really bad happened. More information is generally printed to stdout or stderr, of course setting -dQUIET will suppress much of this....
Since you are getting -100 returned its almost certainly the case that your arguments to Ghostscript are incorrect in some way.
The absolute first thing you should do when debugging problems is to try and simplify the problem, so remove any switches that you don't unquestionably require.
So the first thing to do is to remove the -dQUIET, capture all the stdout/stderr output and tell us what it says.
Further to that, note the following:
You don't need '-f' because the only purpose of that switch is to terminate -c which you haven't used.
You haven't given the values of 'resolucao', 'pageNumber', or 'pdfPath' or so there's no way to know if those are valid (or sensible). The path you have specified for outputImgPath is quite long, and includes the backslash character. I would advise using the forward slash '/' instead, as backslash is used to begin an escape and can give results you may not expect.
You've also used -dSAFER, do you understand the implications of specifying this ? If not, remove it and try again.
When you have created the string 'args' print it out. At the very least this will give you something you can try from the command line, and may mean that other people have some chance of reproducing your problem.
Finally; please note that Ghostscript is distributed under the Affero General Public Licence, please be sure to read the licence and ensure that you are conforming to the terms contained therein.

To do this work, I modified the Simple VB.Net Wrapper for Ghostscript Dll CallBack:
Private Function InOutErrCallBack(ByVal handle As IntPtr, _
ByVal Strz As IntPtr, ByVal Bytes As Integer) As Integer
Dim objString As String
objString = Marshal.PtrToStringAnsi(Strz, Bytes)
System.Diagnostics.Debug.WriteLine(objString)
Return Bytes
End Function
This wrotes the Ghostscript output into the Output window's Visual Studio 2003. I removed the extra "needless" arguments to Ghostscript, like -dGraphicsAlphaBits. The asp.net user does not access the Path.GetTempDir, so, I need a different "temp" dir.
'Converte cada página do PDF em imagem
Public Function Pdf2Png(ByVal pdfPath As String, ByVal tempDirPath As String, ByVal resolucao As Int32) As Image()
Dim outputImgPath As String
Dim pageCount As Integer
Dim objPdfReader As New iTextSharp.text.pdf.PdfReader(pdfPath)
pageCount = objPdfReader.NumberOfPages()
Dim objImages As New ArrayList
For pageNumber As Int32 = 1 To pageCount
outputImgPath = tempDirPath + Guid.NewGuid.ToString("N") + ".png"
Dim objFileStream As FileStream
Dim objMemoryStream As MemoryStream
'Dim args() As String = { _
' "-dNOPAUSE", _
' "-dBATCH", _
' "-dSAFER", _
' "-dQUIET", _
' "-sDEVICE=png16m", _
' String.Format("-r{0}", resolucao), _
' "-dTextAlphaBits=2", _
' "-dGraphicsAlphaBits=2", _
' String.Format("-dFirstPage={0}", pageNumber), _
' String.Format("-dLastPage={0}", pageNumber), _
' String.Format("-sOutputFile={0}", outputImgPath), _
' "-f", _
' pdfPath _
' }
Dim args() As String = { _
"-dNOPAUSE", _
"-dBATCH", _
"-sDEVICE=png16m", _
String.Format("-dFirstPage={0}", pageNumber), _
String.Format("-dLastPage={0}", pageNumber), _
String.Format("-sOutputFile={0}", outputImgPath), _
Replace(pdfPath, "\", "/") _
}
If GhostscriptDllLib.RunGS(args) Then
If File.Exists(outputImgPath) Then
objFileStream = New FileStream(outputImgPath, FileMode.Open)
Dim length As Int32 = objFileStream.Length
Dim bytes(length) As Byte
objFileStream.Read(bytes, 0, length)
objFileStream.Close()
objMemoryStream = New MemoryStream(bytes, False)
objImages.Add(Image.FromStream(objMemoryStream))
File.Delete(outputImgPath)
Else
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
Else
Throw New InvalidOperationException("Erro ao converter páginas do PDF em imagens PNG")
End If
Next
Return CType(objImages.ToArray(GetType(Image)), Image())
End Function

Related

VB.NET FTP File Download

I'm trying to set up my program to connect to my FTP and download files directly from my server. This what I have so far. I don't know what I'm doing wrong, or where I'm going wrong, because no matter how I code it either says "End Expected" or "Method can't handle etc due to signatures not being compatible"
I don't know what I'm doing wrong, any help would be greatly appreciated.
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
(ByVal downloadpath As String, ByVal ftpuri As String, ByVal ftpusername As String, ByVal ftppassword As String)
'Create a WebClient.
Dim request As New WebClient()
' Confirm the Network credentials based on the user name and password passed in.
request.Credentials = New Net.NetworkCredential("Username", "Password")
'Read the file data into a Byte array
Dim bytes() As Byte = request.DownloadData("ftp://ftp.yourwebsitename/file.extension")
Try
' Create a FileStream to read the file into
Dim DownloadStream As FileStream = IO.File.Create("C:\Local\Test.zip")
' Stream this data into the file
DownloadStream.Write(bytes, 0, bytes.Length)
' Close the FileStream
DownloadStream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("Process Complete")
End Sub
You probably pasted an existing method inside a Button.Click handler by mistake.
Rebuilding what was probably the original method is almost enough.
Note that this FTP procedure is a quite basic. You can rely on it only when downloading from a known remote resource. Also, as it it, it doesn't allow to show the download progress or even to cancel it.
Maybe take a look at the WebClient.DownloadDataAsync method, which allows to easily implement a progress bar and cancel the download procedure, when needed.
Also, if you're interested, in this SO question, you can find some notes and a sample Form, which can be included in a Project, to test some features of the FtpWebRequest.
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
Button16.Enabled = False
DownloadFile("C:\Local\Test.zip", "ftp://ftp.example.com/file.ext", "[username]", "[password]")
Button16.Enabled = True
End Sub
Private Sub DownloadFile(destinationPath As String, ftpResource As String, ftpUsername As String, ftpPassword As String)
Dim client As New WebClient()
client.Credentials = New NetworkCredential(ftpUsername, ftpPassword)
Try
Dim dataBytes() As Byte = client.DownloadData(ftpResource)
If dataBytes.Length > 0 Then
File.WriteAllBytes(destinationPath, dataBytes)
MessageBox.Show("Download Complete")
Else
MessageBox.Show("Download failed")
End If
Catch ex As WebException
MessageBox.Show(ex.Message)
Catch ex As IoException
MessageBox.Show(ex.Message)
End Try
End Sub
Here is a Console solution. Compile this into a exe file, and run it by double-clicking the executable or get a scheduler (i.e., Windos Task Scheduler) to open and run the file (it runs as soon as it opens).
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Linq
Imports System.Text
Imports System.Net
Imports System.IO
Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(ParamArray ByVal args() As String)
If args.Any Then
' Do code that references args
Dim dt As DateTime = DateTime.Today.AddDays(-1)
Dim date As String = String.Format("{0:yyyyMMdd}", dt)
Dim p As Program = New Program
p.getFTPFile(("raw_CA_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
p.getFTPFile(("raw_EM_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
p.getFTPFile(("raw_GLB_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
p.getFTPFile(("raw_US_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
Else
' Do code that depends on no input arguments.
Dim dt As DateTime = DateTime.Today.AddDays(-1)
Dim date As String = String.Format("{0:yyyyMMdd}", dt)
Dim p As Program = New Program
p.getFTPFile(("raw_CA_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
p.getFTPFile(("raw_EM_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
p.getFTPFile(("raw_GLB_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
p.getFTPFile(("raw_US_" _
+ (date + ".txt")))
' match a certain pattern in the name of the file
End If
End Sub
Private Sub getFTPFile(ByVal FTPFile As String)
FTPSettings.IP = "000.000.100.000"
FTPSettings.UserID = "your_id"
FTPSettings.Password = "your_password"
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
Dim outputStream As FileStream = New FileStream(("C:\Downloads\AFL_Files\" + FTPFile), FileMode.Create)
reqFTP = CType(FtpWebRequest.Create(("ftp://something#ftp.corp.com/your_path/" + FTPFile)),FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = true
reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As FtpWebResponse = CType(reqFTP.GetResponse,FtpWebResponse)
ftpStream = response.GetResponseStream
Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer() As Byte = New Byte((bufferSize) - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While (readCount > 0)
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
ftpStream.Close
outputStream.Close
response.Close
Catch ex As Exception
If (Not (ftpStream) Is Nothing) Then
ftpStream.Close
ftpStream.Dispose
End If
Throw New Exception(ex.Message.ToString)
End Try
End Sub
Public Class FTPSettings
Public Shared Property IP As String
Get
End Get
Set
End Set
End Property
Public Shared Property UserID As String
Get
End Get
Set
End Set
End Property
Public Shared Property Password As String
Get
End Get
Set
End Set
End Property
End Class
End Class
End Namespace

Creating a webservice that accepts XML as a string

I have been trying to create a webservice that needs to allow the client to send thru 3 parameters being Username, Password and XML_In all 3 of type string. This then sends the variables to a Stored procedure which processes the data and returns an XML string which is then returned to the client.
The Stored procedure works 100% but I'm getting an error with the XML being sent thru as a string. From reading up most gave the suggestion of adding <httpRuntime requestValidationMode="2.0"/> and <pages validateRequest="false"> to my web.config which works but that would then apply to my entire site which I dont want at all.
The other suggestion was to place it in the #Page part but that does not apply to a web service as it has no user defined layout. Please see my code below and help. I'm still quite a newbie to .Net thus the reason I'm doing 90% of it in SQL.
The error That gets returned is :
System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (XML_In="<senddata><settings ...").
at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
at System.Web.HttpRequest.ValidateHttpValueCollection(HttpValueCollection collection, RequestValidationSource requestCollection)
at System.Web.HttpRequest.get_Form()
at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
here's the code:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Xml
Imports JumpStart.Framework.Database
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://mydomainname.co.za/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class LeadProcessor
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function Lead_Processing(ByVal UserName As String, ByVal PassWord As String, ByVal XML_In As String) As XmlDocument
Dim poDSL As New DSL
Dim Result As String
Dim XML_Out = New XmlDocument()
Result = poDSL.ExecuteProcedure_ExecuteScalar("DECLARE #Result XML " & _
"EXEC [dbo].[APP_InsertLeadFromXML] " & _
"#Username = N'" & UserName & "', " & _
"#Password = N'" & PassWord & "', " & _
"#ParmListXML = '" & XML_In.ToString & "', " & _
"#XMLResult = #Result OUTPUT " & _
"SELECT #Result")
XML_Out.LoadXml(Result)
Return XML_Out
End Function
I agree with CodeCaster comment (new projects - WCF or WebApi).
But if you can't change it now, consider use a XMLDocument as parameter, or an XElemento, or directly, do a Base64 transform of the text, in order to avoid that errors.
Code for your service' client:
your_proxy.Lead_Processing(sUserName, sPassWord, Convert.ToBase64String(Encoding.UTF8.GetBytes(sXML))
Then, in your service, do this
<WebMethod()> _
Public Function Lead_Processing(ByVal UserName As String, ByVal PassWord As String, ByVal XML_In As String) As XmlDocument
Dim oData As Byte() = Convert.FromBase64String(XML_In)
Dim sDecodedXML As String = System.Text.Encoding.UTF8.GetString(oData)
Dim poDSL As New DSL
Dim Result As String
Dim XML_Out = New XmlDocument()
Result = poDSL.ExecuteProcedure_ExecuteScalar("DECLARE #Result XML " & _
"EXEC [dbo].[APP_InsertLeadFromXML] " & _
"#Username = N'" & UserName & "', " & _
"#Password = N'" & PassWord & "', " & _
"#ParmListXML = '" & sDecodedXML & "', " & _
"#XMLResult = #Result OUTPUT " & _
"SELECT #Result")
XML_Out.LoadXml(Result)
Return XML_Out
End Function
Hope it helps
Edit:
Your service with base64.
We're telling that Asmx it's almost a legacy tech. New projects may use WCF tech (I can't teach you WCF in a few lines).
You can put that code into your asmx service. You can use that code in the way I edited my answer.

PDFs missing EOF section on customer's server

Folks- I'm relatively new to ASP.NET, and have a question that has stumped my peers-- folks much more experienced than myself.
My company created a website that uses iTextSharp to build and stream PDFs. The functionality works perfectly on my company's development and staging/test servers. The customer's functionality isn't working well, however. The customer's server streams a file where the PDF is missing the last block of data representing the EOF section. The PDF seems to build correctly, streams correctly, but when users open the PDF, the following error displays: 'There was an error opening this document. The file is damaged and could not be repaired.'
By comparing the PDFs in a text viewer (comparing the PDFs from my server vice the customer's server), I can see that the EOF section is missing from the customer's PDF. I'll also note that no errors are thrown during PDF creation, if that's helpful. To make matters more difficult, I have no access to the customer's servers, so I won't be able to interact with the systems directly.
The asp.net version is 3.5. Both of our servers (my company and the customer) are: running IIS7.5 on Server 2008R2; using iTextSharp is 5.1.2; and are configured for FIPS compatibility.
I've read dozens and dozens of posts detailing why a PDF isn't created properly, why it may not be streaming, and all things related, but I haven't seen this particular issue before. I guess what I need to know in the short-term is: 1) what can I provide to help diagnose the issue, 2) where is a good place to start looking for areas of concern?
Also, I updated to revision 5.5.3 last night; same results-- it works fine on my servers, but produces broken PDFs on the customer's server.
Code added:
Public Function BuildReport(ByVal tblReport As DataTable, _
ByRef memStream As MemoryStream, _
ByRef strErrMsg As String) As Boolean
Dim booOK As Boolean = True
strErrMsg = String.Empty
' Create document
Try
' Create writer (listens to the document and directs PDF stream)
memStream = New MemoryStream()
Dim msWriter As PdfWriter = PdfWriter.GetInstance(_document, memStream)
msWriter.CloseStream = False
'Create header
Dim ev As New itsEvents
msWriter.PageEvent = ev
' Set document metadata
_document.AddTitle(_strMetaTitle)
_document.AddSubject(_strMetaSubject)
_document.AddCreator(_strMetaApplication)
_document.AddAuthor(_strMetaAuthor)
' Open document, add document content, close document
_document.Open()
AddReportContent(tblReport)
_document.Close()
Catch ex As Exception
booOK = False
strErrMsg = ex.Message
End Try
Return booOK
End Function
Private Sub AddReportContent(ByVal tblReport As DataTable)
' Count report columns
Dim intReportColumns As Integer = 0
For Each col As DataColumn In tblReport.Columns
If ContainedInColumnMask(col.ColumnName) Then
intReportColumns += 1
End If
Next
' Build table
Dim table As PdfPTable
Dim cell As PdfPCell
Dim phrase As Phrase
If intReportColumns >= 1 Then
' Init table
table = New PdfPTable(intReportColumns)
' Add title to table
'phrase = New Phrase(_strMetaTitle, _fontLarge)
'cell = New PdfPCell(phrase)
'cell.Colspan = intReportColumns
'cell.HorizontalAlignment = 1 ' 0=Left, 1=Centre, 2=Right
'table.AddCell(cell)
' Add column headers to table
Dim i As Integer = 0
Dim intColWidth As Integer
Dim intColWidths As Integer() = New Integer(intReportColumns - 1) {}
Dim intColWidthTotal As Integer = 0
Dim strColName As String
For Each col As DataColumn In tblReport.Columns
If ContainedInColumnMask(col.ColumnName) Then
strColName = col.ColumnName
If (col.ExtendedProperties.Item("NOTEXTEXPORT") <> True) Then
If col.ExtendedProperties.Contains("FRIENDLYNAME") Then
strColName = col.ExtendedProperties.Item("FRIENDLYNAME")
End If
End If
phrase = New Phrase(strColName, _fontMedium)
cell = New PdfPCell(phrase)
cell.BorderWidth = 1
cell.BackgroundColor = iTextSharp.text.BaseColor.LIGHT_GRAY
'cell.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY
table.AddCell(cell)
intColWidth = GetColumnWidth(col, strColName, _fontMedium.Size, _fontSmall.Size)
intColWidths(i) = intColWidth
intColWidthTotal += intColWidth
i += 1
End If
Next
table.TotalWidth = intColWidthTotal
table.SetWidths(intColWidths)
' Add rows to table
For Each row As DataRow In tblReport.Rows
For Each col As DataColumn In tblReport.Columns
If ContainedInColumnMask(col.ColumnName) Then
phrase = New Phrase(SetBlankIfNothing(row.Item(col.ColumnName).ToString()), _fontSmall)
cell = New PdfPCell(phrase)
cell.BorderWidth = 0.5
table.AddCell(cell)
End If
Next
Next
Else
' Init table
table = New PdfPTable(1)
' Nothing to add to table
table.AddCell(String.Empty)
End If
' Add table to document
_document.Add(table)
End Sub
Public Sub New(ByVal strMetaTitle As String, _
ByVal strMetaSubject As String, _
ByVal strMetaApplication As String, _
ByVal strMetaAuthor As String, _
Optional ByVal strColumnMask As String = "")
GetStaticInfo()
_strMetaTitle = strMetaTitle
_strMetaSubject = strMetaSubject
_strMetaApplication = strMetaApplication
_strMetaAuthor = strMetaAuthor
_document = New iTextSharp.text.Document(_itsPage, _itsMarginLeft, _itsMarginRight, _itsMarginTop, _itsMarginBottom)
If strColumnMask <> "" And Not strColumnMask Is Nothing Then
_strColumnMask = strColumnMask
End If
End Sub
Public Sub New(ByVal strMetaTitle As String, _
ByVal strMetaSubject As String, _
ByVal strMetaApplication As String, _
ByVal strMetaAuthor As String, _
Optional ByVal strColumnMask As String = "")
GetStaticInfo()
_strMetaTitle = strMetaTitle
_strMetaSubject = strMetaSubject
_strMetaApplication = strMetaApplication
_strMetaAuthor = strMetaAuthor
_document = New iTextSharp.text.Document(_itsPage, _itsMarginLeft, _itsMarginRight, _itsMarginTop, _itsMarginBottom)
If strColumnMask <> "" And Not strColumnMask Is Nothing Then
_strColumnMask = strColumnMask
End If
End Sub

vb.net code that will export / convert multiple selected files in to one pdf file

I am trying to create a code that will convert multiple selected files to one pdf file . Currently the code exports the selected files in to a zip file. But I want to open all the selected files in one single pdf file .
For your assistance I am providing the code that exports all files into one zip file.
In the code below there are two table mentioned. one is document and another is vacancyapplication. In the document table all the files are stored guid is the unique id in the document table.
Imports System
Imports System.Web
Imports System.IO
Imports System.Collections.Generic
Imports Ionic.Zip
Imports System.Linq
Imports NLog
Public Class download_bulk_cv : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim _logger As Logger = LogManager.GetCurrentClassLogger()
Dim vacancy = New Vacancy(context.Request("v"))
context.Response.Clear()
context.Response.ContentType ="application/zip"
context.Response.AddHeader("content-disposition", "attachment; filename=" & vacancy.Title.Replace(" ", "_") & "_" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".zip")
Dim files = New List(Of String)()
For Each docPath As String In From row As DataRow In DB.GetData("select guid, originalfilename from document where id in (select candidatecvid from vacancyapplication where id in (" & context.Request("a").ToString() & "))").Rows Let guid = row.Item("guid").ToString() Select HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(guid, 1) & "\" & Right(guid, 1) & "\" & guid & "." & System.IO.Path.GetExtension(row.Item("originalfilename")).ToLower().Substring(1)
If File.Exists(docPath) Then
files.Add(docPath)
'_logger.Info(docPath)
End If
Next
Using zip As New ZipFile()
zip.AddFiles(files.ToArray(), "CVs") '.AddFile(docPath, "CVs")
zip.AddEntry("info.txt", files.Count.ToString.ToString() & "CVs archived", Encoding.Default)
zip.Save(context.Response.OutputStream)
End Using
context.Response.End()
End Sub
End Class
i have written the following code to merge the pdf documents but its not working
Edited code
Public Class preview_bulk_cv : Implements IHttpHandler
''Implements IDisposable
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim _logger As Logger = LogManager.GetCurrentClassLogger()
Dim vacancy = New Vacancy(context.Request("v"))
Dim files = New List(Of String)()
Dim sourceFiles = New List(Of String)()
Dim directorypath As String = HttpContext.Current.Server.MapPath("~/documents") & "\download\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\"
Dim pdf_document As iTextSharp.text.Document = Nothing
Dim pdf_copier As iTextSharp.text.pdf.PdfCopy = Nothing
context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AddHeader("content-disposition", "attachment; filename=" & vacancy.Title.Replace(" ", "_") & "_" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".pdf")
For Each docPath As String In From row As DataRow In DB.GetData("select guid, originalfilename from document where id in (select candidatecvid from vacancyapplication where id in (" & context.Request("a").ToString() & "))").Rows Let guid = row.Item("guid").ToString() Select HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(guid, 1) & "\" & Right(guid, 1) & "\" & guid & "." & System.IO.Path.GetExtension(row.Item("originalfilename")).ToLower().Substring(1)
Dim epath As String = HttpContext.Current.Server.MapPath("~/documents") & "\download\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".pdf"
Converter.ConvertDocument(docPath, epath)
If File.Exists(epath) Then
sourceFiles.Add(epath)
End If
If File.Exists(docPath) Then
files.Add(docPath)
'_logger.Info(docPath)
End If
Next
Dim all_source_files As String() = sourceFiles.ToArray()
Dim docs As PdfDocument() = New PdfDocument(all_source_files.Length - 1) {}
For i As Integer = 0 To all_source_files.Length - 1
Dim reader As New PdfReader(all_source_files(i))
' Using reader As New iTextSharp.text.pdf.PdfReader(all_source_files(i))
Dim finalpdf As String = HttpContext.Current.Server.MapPath("~/documents") & "\download\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\finalcv.pdf"
If i = 0 Then
pdf_document = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
pdf_copier = New iTextSharp.text.pdf.PdfCopy(pdf_document, New IO.FileStream(finalpdf, IO.FileMode.Create))
pdf_document.Open()
End If
For page_num As Integer = 1 To reader.NumberOfPages
pdf_copier.AddPage(pdf_copier.GetImportedPage(reader, page_num))
Next
' End Using
Next
pdf_copier.Close()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
I am new with vb.net . I appreciate your kind assistance.
This is an example of code to combine an array of PDFs into 1 merged PDF, it requires a reference to the iTextSharp dll I mentioned in my comment. If you can save each file individually to a PDF now, you can use something like System.IO.Directory.GetFiles(your_directory) to get the array of file names and then combine them with something like the code here:
' This requires a reference to the iTextSharp library (http://sourceforge.net/projects/itextsharp/)
Dim pdfs() As String ' all of your PDF files you'd like to merge
Dim output_pdf As String ' the output file
Dim pdf_document As iTextSharp.text.Document = Nothing
Dim pdf_copier As iTextSharp.text.pdf.PdfCopy = Nothing
For i As Integer = 0 To pdfs.Length - 1
Using pdf_reader As New iTextSharp.text.pdf.PdfReader(pdfs(i))
If i = 0 Then
pdf_document = New iTextSharp.text.Document(pdf_reader.GetPageSizeWithRotation(1))
pdf_copier = New iTextSharp.text.pdf.PdfCopy(pdf_document, New IO.FileStream(output_pdf, IO.FileMode.Create))
pdf_document.Open()
End If
For page_num As Integer = 1 To pdf_reader.NumberOfPages
pdf_copier.AddPage(pdf_copier.GetImportedPage(pdf_reader, page_num))
Next
End Using
Next
pdf_copier.Close()
Here is the code for converting any documents to pdf files and merging them into one single pdf file
<%# WebHandler Language="VB" Class="PDFMerge" %>
Imports System
Imports System.Web
Imports System.IO
Imports System.Collections.Generic
Imports Ionic.Zip
Imports System.Linq
Imports NLog
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Text.RegularExpressions
Public Class PDFMerge : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim _logger As Logger = LogManager.GetCurrentClassLogger()
Dim vacancy = New Vacancy(context.Request("v"))
Dim sourceFiles = New List(Of String)()
For Each docPath As String In From row As DataRow In DB.GetData("database query").Rows Select HttpContext.Current.Server.MapPath("~/Downloads") & "\" System.IO.Path.GetExtension(row.Item("originalfilename")).ToLower().Substring(1)
Dim epath As String = HttpContext.Current.Server.MapPath("~/Downloads") & "\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\" & Now.ToString("yyyy-MMM-dd-HHmmss") & ".pdf"
Converter.ConvertDocument(docPath, epath)
If File.Exists(epath) Then
sourceFiles.Add(epath)
End If
Next
Dim OutputFileName As String = HttpContext.Current.Server.MapPath("~/Downloads") & "\" & Now.ToString("yyyy-MMM-dd") & "\" & vacancy.Title.Replace(" ", "_") & "\" & vacancy.Title.Replace(" ", "_") & ".pdf"
PDFMerge.MergeFiles(OutputFileName, sourceFiles.ToArray)
Dim mPDFFile As FileStream = File.OpenRead(OutputFileName)
Dim mPDFFileBuffer(mPDFFile.Length - 1) As Byte
mPDFFile.Read(mPDFFileBuffer, 0, mPDFFileBuffer.Length)
mPDFFile.Close()
System.Diagnostics.Process.Start(OutputFileName)
context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AddHeader("Content-Disposition", "attachment;filename=" & OutputFileName)
context.Response.AddHeader("Content-Length", mPDFFileBuffer.Length)
context.Response.OutputStream.Write(mPDFFileBuffer, 0, mPDFFileBuffer.Length)
mPDFFileBuffer = Nothing
context.Response.Flush()
context.Response.End()
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Shared Sub MergeFiles(destinationFile As String, sourceFiles As String())
Try
Dim f As Integer = 0
Dim reader As New PdfReader(sourceFiles(f)) ' we create a reader for a certain document
Dim n As Integer = reader.NumberOfPages ' we retrieve the total number of pages
'Console.WriteLine("There are " + n + " pages in the original file.");
Dim document As New Document(reader.GetPageSizeWithRotation(1)) ' step 1: creation of a document-object
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(destinationFile, FileMode.Create)) ' step 2: we create a writer that listens to the document
document.Open() ' step 3: we open the document
Dim cb As PdfContentByte = writer.DirectContent
Dim page As PdfImportedPage
Dim rotation As Integer
' step 4: we add content
While f < sourceFiles.Length
Dim i As Integer = 0
While i < n
i += 1
document.SetPageSize(reader.GetPageSizeWithRotation(i))
document.NewPage()
page = writer.GetImportedPage(reader, i)
rotation = reader.GetPageRotation(i)
If rotation = 90 OrElse rotation = 270 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, _
reader.GetPageSizeWithRotation(i).Height)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, _
0)
'Console.WriteLine("Processed page " + i);
End If
End While
f += 1
If f < sourceFiles.Length Then
reader = New PdfReader(sourceFiles(f))
' we retrieve the total number of pages
'Console.WriteLine("There are " + n + " pages in the original file.");
n = reader.NumberOfPages
End If
End While
' step 5: we close the document
document.Close()
Catch e As Exception
Dim strOb As String = e.Message
End Try
End Sub
Public Function CountPageNo(strFileName As String) As Integer
' we create a reader for a certain document
Dim reader As New PdfReader(strFileName)
' we retrieve the total number of pages
Return reader.NumberOfPages
End Function
End Class
To convert the documents use a third party library like Apose Words for .net. Create a separate class as converter and a function ConvertDocument(ByRef docPath As String, ByRef expPath As String) . If you already have all the files in pdf you dont need to convert them...
you only need to merge them
I hope this codes will help many people

Best way to stamp an image with another image to create a watermark in ASP.NET?

Anyone know? Want to be able to on the fly stamp an image with another image as a watermark, also to do large batches. Any type of existing library or a technique you know of would be great.
This will answer your question:
http://www.codeproject.com/KB/GDI-plus/watermark.aspx
Good luck!
I have had good luck with ImageMagick. It has an API for .NET too.
here is my full article: http://forums.asp.net/p/1323176/2634923.aspx
use the SDK Command Prompt and navigate the active folder to the folder containing the below source code... then compile the code using
vbc.exe watermark.vb /t:exe /out:watermark.exe
this will create an exe in the folder.. the exe accepts two parameters:
ex.
watermark.exe "c:\source folder" "c:\destination folder"
this will iterate through the parent folder and all subfolders. all found jpegs will be watermarked with the image you specify in the code and copied to the destination folder. The original image will stay untouched.
// watermark.vb --
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Namespace WatermarkManager
Class Watermark
Shared sourceDirectory As String = "", destinationDirectory As String = ""
Overloads Shared Sub Main(ByVal args() As String)
'See if an argument was passed from the command line
If args.Length = 2 Then
sourceDirectory = args(0)
destinationDirectory = args(1)
' make sure sourceFolder is legit
If Directory.Exists(sourceDirectory) = False
TerminateExe("Invalid source folder. Folder does not exist.")
Exit Sub
End If
' try and create destination folder
Try
Directory.CreateDirectory(destinationDirectory)
Catch
TerminateExe("Error creating destination folder. Invalid path cannot be created.")
Exit Sub
End Try
' start the magic
CreateHierarchy(sourceDirectory,destinationDirectory)
ElseIf args.Length = 1
If args(0) = "/?"
DisplayHelp()
Else
TerminateExe("expected: watermark.exe [source path] [destination path]")
End If
Exit Sub
Else
TerminateExe("expected: watermark.exe [source path] [destination path]")
Exit Sub
End If
TerminateExe()
End Sub
Shared Sub CreateHierarchy(ByVal sourceDirectory As String, ByVal destinationDirectory As String)
Dim tmpSourceDirectory As String = sourceDirectory
' copy directory hierarchy to destination folder
For Each Item As String In Directory.GetDirectories(sourceDirectory)
Directory.CreateDirectory(destinationDirectory + Item.SubString(Item.LastIndexOf("\")))
If hasSubDirectories(Item)
CreateSubDirectories(Item)
End If
Next
' reset destinationDirectory
destinationDirectory = tmpSourceDirectory
' now that folder structure is set up, let's iterate through files
For Each Item As String In Directory.GetDirectories(sourceDirectory)
SearchDirectory(Item)
Next
End Sub
Shared Function hasSubDirectories(ByVal path As String) As Boolean
Dim subdirs() As String = Directory.GetDirectories(path)
If subdirs.Length > 0
Return True
End If
Return False
End Function
Shared Sub CheckFiles(ByVal path As String)
For Each f As String In Directory.GetFiles(path)
If f.SubString(f.Length-3).ToLower = "jpg"
WatermarkImage(f)
End If
Next
End Sub
Shared Sub WatermarkImage(ByVal f As String)
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(f)
Dim graphic As Graphics
Dim indexedImage As New Bitmap(img)
graphic = Graphics.FromImage(indexedImage)
graphic.DrawImage(img, 0, 0, img.Width, img.Height)
img = indexedImage
graphic.SmoothingMode = SmoothingMode.AntiAlias
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim x As Integer, y As Integer
Dim source As New Bitmap("c:\watermark.png")
Dim logo As New Bitmap(source, CInt(img.Width / 3), CInt(img.Width / 3))
source.Dispose()
x = img.Width - logo.Width
y = img.Height - logo.Height
graphic.DrawImage(logo, New Point(x,y))
logo.Dispose()
img.Save(destinationDirectory+f.SubString(f.LastIndexOf("\")), ImageFormat.Jpeg)
indexedImage.Dispose()
img.Dispose()
graphic.Dispose()
Console.WriteLine("successfully watermarked " + f.SubString(f.LastIndexOf("\")+1))
Console.WriteLine("saved to: " + vbCrLf + destinationDirectory + vbCrLf)
End Sub
Shared Sub SearchDirectory(ByVal path As String)
destinationDirectory = destinationDirectory + path.SubString(path.LastIndexOf("\"))
CheckFiles(path)
For Each Item As String In Directory.GetDirectories(path)
destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
CheckFiles(Item)
If hasSubDirectories(Item)
destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
SearchDirectory(Item)
destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
End If
destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
Next
destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
End Sub
Shared Sub CreateSubDirectories(ByVal path As String)
destinationDirectory = destinationDirectory + path.SubString(path.LastIndexOf("\"))
For Each Item As String In Directory.GetDirectories(path)
destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
Directory.CreateDirectory(destinationDirectory)
Console.WriteLine(vbCrlf + "created: " + vbCrlf + destinationDirectory)
If hasSubDirectories(Item)
destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
CreateSubDirectories(Item)
destinationDirectory += Item.SubString(Item.LastIndexOf("\"))
End If
destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
Next
destinationDirectory = destinationDirectory.SubString(0,destinationDirectory.LastIndexOf("\"))
End Sub
Shared Sub TerminateExe(ByVal Optional msg As String = "")
If msg ""
Console.WriteLine(vbCrLf + "AN ERROR HAS OCCURRED //" + vbCrLf + msg)
End If
Console.WriteLine(vbCrLf + "Press [enter] to close...")
'Console.Read()
End Sub
Shared Sub DisplayHelp()
Console.WriteLine("watermark.exe accepts two parameters:" + vbCrLf + " - [source folder]")
Console.WriteLine(" - [destination folder]")
Console.WriteLine("ex." + vbCrLf + "watermark.exe ""c:\web_projects\dclr source"" ""d:\new_dclr\copy1 dest""")
Console.WriteLine(vbCrLf + "Press [enter] to close...")
Console.Read()
End Sub
End Class
End Namespace

Resources