Tuesday, January 25, 2011

Add an Image to pdf document using ItextSharp

In this article we will show how to add Image in pdf document using Itextsharp in asp.net. For some report which is in pdf format we need to add image .jpg, .gif, .bmp or any image format.
Itextsharp is an open source free pdf generate library which will generate the pdf and yes you can add the image into it.

you can also refer to my articles on ItextSharp in asp.net here to create table in pdf doc here

Step 1 : Download the ITextsharp library from here.

Step 2 : Add Itextsharp.dll to your project by right click and add reference on the project explorer

Step 3 : Now add the Namespace to your .cs or .vb file


using iTextSharp.text;
using iTextSharp.text.pdf;




Step 4 :

Lets see this code which will be place image into PDF document


// Now image in the pdf file
string imageFilePath = Server.MapPath(".") + "/image/Sunset.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

//Resize image depend upon your need
jpg.ScaleToFit(280f, 260f);

//Give space before image
jpg.SpacingBefore = 30f;

//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_CENTER;

doc.Add(paragraph); // add paragraph to the document

doc.Add(jpg); //add an image to the created pdf document



Now to see full code you can place the below code in any event like on button click

In C# sample code function:

private void CreatePDFFile()
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";

//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
doc.Open();//Open Document to write

//Write some content into pdf file
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");

// Now image in the pdf file
string imageFilePath = Server.MapPath(".") + "/image/Sunset.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

//Resize image depend upon your need
jpg.ScaleToFit(280f, 260f);

//Give space before image
jpg.SpacingBefore = 30f;

//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_CENTER;

doc.Add(paragraph); // add paragraph to the document

doc.Add(jpg); //add an image to the created pdf document
}
catch (DocumentException docEx)
{
//handle pdf document exception if any
}
catch (IOException ioEx)
{
// handle IO exception
}
catch (Exception ex)
{
// ahndle other exception if occurs
}
finally
{
//Close document and writer
doc.Close();

}
}



Same above code vb.net method:

Private Sub CreatePDFFile()
Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Try
Dim pdfFilePath As String = Server.MapPath(".") & "/pdf/myPdf.pdf"

'Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfFilePath, FileMode.Create))
doc.Open()
'Open Document to write
'Write some content into pdf file
Dim paragraph As New Paragraph("This is my first line using Paragraph.")

' Now image in the pdf file
Dim imageFilePath As String = Server.MapPath(".") & "/image/Sunset.jpg"
Dim jpg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageFilePath)

'Resize image depend upon your need
jpg.ScaleToFit(280F, 260F)

'Give space before image
jpg.SpacingBefore = 30F

'Give some space after the image
jpg.SpacingAfter = 1F
jpg.Alignment = Element.ALIGN_CENTER

doc.Add(paragraph)
' add paragraph to the document
'add an image to the created pdf document
doc.Add(jpg)
Catch docEx As DocumentException
'handle pdf document exception if any
Catch ioEx As IOException
' handle IO exception
Catch ex As Exception
' ahndle other exception if occurs
Finally
'Close document and writer

doc.Close()
End Try
End Sub



If you run your application you can see the image in pdf document like this:




Hope this will help all!



Thank You for your valuable time.

Irfan Ahmad alias (Isfan Habib)
(Team Lead)
DZ International
Srinagar, Kashmir
e-mail: irfanhabib06@gmail.com

Monday, January 24, 2011

Visual Studio / SQL Server Unable to open database / Operating systerm Error 5

Operating systerm Error 5 usually means not enough permission.

Make sure account under which SQL server service is runing has modify permission on the folder where you have kept your MDF File.

Not account under you are logged in Windows (does not matter you have permission; it is the SQL server service account that needs permission)

•Right click on the folder D:\Development Workx\Hamza Workingx\hamriqueweb_1_Edited_ALi2\App_Data and click on properties
•Click on security tab
•Click on Add button and add sql service account
•Provide modify privilege and click ok
•Verify both mdf and ldf have modify privilege
•Attach the db

Friday, October 22, 2010

Can I make SQL Server format dates and times for me?

Many people have asked if there is a way to make SQL Server behave the way FORMAT works in VB (and FormatDateTime in VBScript). What they'd like to see is the ability to tell SQL Server to format a date with long date and time, or in MM/DD/YYYY format, instead of having to memorize existing format conversion numbers and/or manipulate the strings themselves. For example, to get today's date in YYYYMMDD format, you currently need to call the following:

SELECT CONVERT(CHAR(8), GETDATE(), 112)

What does the 112 mean? Nothing. It's just an arbitrary number representing this specific format (Kalen Delaney's Inside SQL Server 2000 has a detailed explanation of the more commonly-used conversions).

Now, wouldn't it be nice to be able to say this:

SELECT CONVERT(VARCHAR, GETDATE(), 'YYYYMMDD')

?

Well, now you can, if you're using SQL Server 2000. I designed this scalar user-defined function for specifically this purpose.

CREATE FUNCTION dbo.FormatDateTime
(
@dt DATETIME,
@format VARCHAR(16)
)
RETURNS VARCHAR(64)
AS
BEGIN
DECLARE @dtVC VARCHAR(64)
SELECT @dtVC = CASE @format

WHEN 'LONGDATE' THEN

DATENAME(dw, @dt)
+ ',' + SPACE(1) + DATENAME(m, @dt)
+ SPACE(1) + CAST(DAY(@dt) AS VARCHAR(2))
+ ',' + SPACE(1) + CAST(YEAR(@dt) AS CHAR(4))

WHEN 'LONGDATEANDTIME' THEN

DATENAME(dw, @dt)
+ ',' + SPACE(1) + DATENAME(m, @dt)
+ SPACE(1) + CAST(DAY(@dt) AS VARCHAR(2))
+ ',' + SPACE(1) + CAST(YEAR(@dt) AS CHAR(4))
+ SPACE(1) + RIGHT(CONVERT(CHAR(20),
@dt - CONVERT(DATETIME, CONVERT(CHAR(8),
@dt, 112)), 22), 11)

WHEN 'SHORTDATE' THEN

LEFT(CONVERT(CHAR(19), @dt, 0), 11)

WHEN 'SHORTDATEANDTIME' THEN

REPLACE(REPLACE(CONVERT(CHAR(19), @dt, 0),
'AM', ' AM'), 'PM', ' PM')

WHEN 'UNIXTIMESTAMP' THEN

CAST(DATEDIFF(SECOND, '19700101', @dt)
AS VARCHAR(64))

WHEN 'YYYYMMDD' THEN

CONVERT(CHAR(8), @dt, 112)

WHEN 'YYYY-MM-DD' THEN

CONVERT(CHAR(10), @dt, 23)

WHEN 'YYMMDD' THEN

CONVERT(VARCHAR(8), @dt, 12)

WHEN 'YY-MM-DD' THEN

STUFF(STUFF(CONVERT(VARCHAR(8), @dt, 12),
5, 0, '-'), 3, 0, '-')

WHEN 'MMDDYY' THEN

REPLACE(CONVERT(CHAR(8), @dt, 10), '-', SPACE(0))

WHEN 'MM-DD-YY' THEN

CONVERT(CHAR(8), @dt, 10)

WHEN 'MM/DD/YY' THEN

CONVERT(CHAR(8), @dt, 1)

WHEN 'MM/DD/YYYY' THEN

CONVERT(CHAR(10), @dt, 101)

WHEN 'DDMMYY' THEN

REPLACE(CONVERT(CHAR(8), @dt, 3), '/', SPACE(0))

WHEN 'DD-MM-YY' THEN

REPLACE(CONVERT(CHAR(8), @dt, 3), '/', '-')

WHEN 'DD/MM/YY' THEN

CONVERT(CHAR(8), @dt, 3)

WHEN 'DD/MM/YYYY' THEN

CONVERT(CHAR(10), @dt, 103)

WHEN 'HH:MM:SS 24' THEN

CONVERT(CHAR(8), @dt, 8)

WHEN 'HH:MM 24' THEN

LEFT(CONVERT(VARCHAR(8), @dt, 8), 5)

WHEN 'HH:MM:SS 12' THEN

LTRIM(RIGHT(CONVERT(VARCHAR(20), @dt, 22), 11))

WHEN 'HH:MM 12' THEN

LTRIM(SUBSTRING(CONVERT(
VARCHAR(20), @dt, 22), 10, 5)
+ RIGHT(CONVERT(VARCHAR(20), @dt, 22), 3))

ELSE

'Invalid format specified'

END
RETURN @dtVC
END
GO

(If you're using SQL Server 7.0, you can't create UDFs; so, I suppose you could put this logic into a stored procedure, and put the result into an output parameter.)

Sample usage:

DECLARE @now DATETIME
SET @now = GETDATE()

PRINT dbo.FormatDateTime(@now, 'LONGDATE')
PRINT dbo.FormatDateTime(@now, 'LONGDATEANDTIME')
PRINT dbo.FormatDateTime(@now, 'SHORTDATE')
PRINT dbo.FormatDateTime(@now, 'SHORTDATEANDTIME')
PRINT dbo.FormatDateTime(@now, 'UNIXTIMESTAMP')
PRINT dbo.FormatDateTime(@now, 'YYYYMMDD')
PRINT dbo.FormatDateTime(@now, 'YYYY-MM-DD')
PRINT dbo.FormatDateTime(@now, 'YYMMDD')
PRINT dbo.FormatDateTime(@now, 'YY-MM-DD')
PRINT dbo.FormatDateTime(@now, 'MMDDYY')
PRINT dbo.FormatDateTime(@now, 'MM-DD-YY')
PRINT dbo.FormatDateTime(@now, 'MM/DD/YY')
PRINT dbo.FormatDateTime(@now, 'MM/DD/YYYY')
PRINT dbo.FormatDateTime(@now, 'DDMMYY')
PRINT dbo.FormatDateTime(@now, 'DD-MM-YY')
PRINT dbo.FormatDateTime(@now, 'DD/MM/YY')
PRINT dbo.FormatDateTime(@now, 'DD/MM/YYYY')
PRINT dbo.FormatDateTime(@now, 'HH:MM:SS 24')
PRINT dbo.FormatDateTime(@now, 'HH:MM 24')
PRINT dbo.FormatDateTime(@now, 'HH:MM:SS 12')
PRINT dbo.FormatDateTime(@now, 'HH:MM 12')
PRINT dbo.FormatDateTime(@now, 'goofy')