Tuesday, June 29, 2010

iTextSharp - read an existing pdf, resize it and then draw it on a new one


PdfReader reader = new PdfReader("In.PDF");
Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(doc, 
new FileStream("Out.PDF",
FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, 1); //page #1
float Scale = 0.67f;
cb.AddTemplate(page, Scale, 0, 0, Scale, 0, 0);
doc.Close();

Friday, June 11, 2010

Add an image at a given position without fail on any page of a pdf file.

Looking for a way to add an image at a given position on a pdf ( Provided you use iTextSharp for .Net library). Here is the way to go.


Following code is an extract from here.


string pdfTemplate = @"c:\Temp\PDF\fw4.pdf";
string newFile = @"c:\Temp\PDF\completed_fw4.pdf";


PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
string chartLoc = string.Empty;
chartLoc = @"C:\Temp\PDF\IMG_3746.jpg";//pplLogoSmall.jpg";


iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance(chartLoc);
iTextSharp.text.pdf.PdfContentByte overContent;


iTextSharp.text.Rectangle rect;
try
{
Single X, Y;int pageCount = 0;
rect = pdfReader.GetPageSizeWithRotation(1);
if (chartImg.Width > rect.Width || chartImg.Height > rect.Height)
{
chartImg.ScaleToFit(rect.Width, rect.Height);
X = (rect.Width - chartImg.ScaledWidth) / 2;
Y = (rect.Height - chartImg.ScaledHeight) / 2;
}
else
{
X = (rect.Width - chartImg.Width) / 2;
Y = (rect.Height - chartImg.Height) / 2;
}
chartImg.SetAbsolutePosition(X, Y);
pageCount = pdfReader.NumberOfPages;
for (int i = 1; i < pageCount; i++)
{
overContent = pdfStamper.GetOverContent(i);
overContent.AddImage(chartImg);
}
pdfStamper.Close();
pdfReader.Close();
}
catch (Exception ex)
{
throw ex;
}

Happy Coding!

Friday, May 21, 2010

Inject Some Life into Your Applications—Getting to Know the Unity Application Block

Unity is just a container that stores registrations and mappings between types and can instantiate the appropriate concrete types on demand. Or, to be more precise, it is a lightweight, extensible dependency injection container with support for nested containers and facilities for constructor, property, and method call injection. Unity is available as a stand-alone download and is also included with Enterprise Library 4.0. You can use Unity with Enterprise Library to create both Enterprise Library objects and your own custom business objects, or you can use it without installing Enterprise Library.


Above was the small introduction to the UNITY DI Library provided in the article i went through. Hope you'll make use of this piece of information for dependency injection.


Happy Coding!

Wednesday, March 31, 2010

Asynchronous Tracking - Google Analytics


Asynchronous tracking is an improved way to track website visitors with Google Analytics. Unlike a traditional installation, asynchronous tracking optimizes how browsers load ga.js so its impact on user experience is minimized. It also allows you to put your Analytics snippet higher in the page without delaying subsequent content from rendering.
Although asynchronous tracking uses a different Analytics snippet and a different syntax for tracking calls, it supports the exact same tracking customizations as the traditional snippet. In fact, the asynchronous tracking syntax is also more flexible than the traditional snippet.

Friday, March 12, 2010

NHibernate + IIS6 / IIS7 Hosting Configrations in web.config

We came across a situation where our Web Application updates  the SQL2k5 DB when hosted in IIS6, But does not update when it is hosted in IIS7. After some research we realized the problem was with the modules  and how IIS6 and IIS7 handles the modules mentioned within web.config. Here I share with you how we got the application up and running with some configuration changes.



If you are hosting in IIS6 you should uncomment following,


If you are hosting in IIS7 you should uncomment the following,
Cheers!

Wednesday, March 10, 2010

Copying DB on the Same Server with No Hassel


USE master
GO
-- the original database (use 'SET @DB = NULL' to disable backup)
DECLARE @DB varchar(200)
SET @DB = 'PcTopp'
-- the backup filename
DECLARE @BackupFile varchar(2000)
SET @BackupFile = 'c:\pctopp\sqlserver\backup.dat'
-- the new database name
DECLARE @TestDB varchar(200)
SET @TestDB = 'TestDB'
-- the new database files without .mdf/.ldf
DECLARE @RestoreFile varchar(2000)
SET @RestoreFile = 'c:\pctopp\sqlserver\backup'

-- ****************************************************************
--                    no change below this line
-- ****************************************************************

DECLARE @query varchar(2000)
DECLARE @DataFile varchar(2000)
SET @DataFile = @RestoreFile + '.mdf'
DECLARE @LogFile varchar(2000)
SET @LogFile = @RestoreFile + '.ldf'
IF @DB IS NOT NULL
BEGIN
    SET @query = 'BACKUP DATABASE ' + @DB + ' TO DISK = ' + QUOTENAME(@BackupFile, '''')
    EXEC (@query)
END
-- RESTORE FILELISTONLY FROM DISK = 'C:\temp\backup.dat'
-- RESTORE HEADERONLY FROM DISK = 'C:\temp\backup.dat'
-- RESTORE LABELONLY FROM DISK = 'C:\temp\backup.dat'
-- RESTORE VERIFYONLY FROM DISK = 'C:\temp\backup.dat'
IF EXISTS(SELECT * FROM sysdatabases WHERE name = @TestDB)
BEGIN
    SET @query = 'DROP DATABASE ' + @TestDB
    EXEC (@query)
END
RESTORE HEADERONLY FROM DISK = @BackupFile
DECLARE @File int
SET @File = @@ROWCOUNT
DECLARE @Data varchar(500)
DECLARE @Log varchar(500)
SET @query = 'RESTORE FILELISTONLY FROM DISK = ' + QUOTENAME(@BackupFile , '''')
CREATE TABLE #restoretemp
(
 LogicalName varchar(500),
 PhysicalName varchar(500),
 type varchar(10),
 FilegroupName varchar(200),
 size int,
 maxsize bigint
)
INSERT #restoretemp EXEC (@query)
SELECT @Data = LogicalName FROM #restoretemp WHERE type = 'D'
SELECT @Log = LogicalName FROM #restoretemp WHERE type = 'L'
PRINT @Data
PRINT @Log
TRUNCATE TABLE #restoretemp
DROP TABLE #restoretemp
IF @File > 0
BEGIN
    SET @query = 'RESTORE DATABASE ' + @TestDB + ' FROM DISK = ' + QUOTENAME(@BackupFile, '''') +
        
' WITH MOVE ' + QUOTENAME(@Data, '''') + ' TO ' + QUOTENAME(@DataFile, '''') + ', MOVE ' +
        QUOTENAME(@Log, '''') + ' TO ' + QUOTENAME(@LogFile, '''') + ', FILE = ' + CONVERT(varchar, @File)
    EXEC (@query)
END
GO

==========================================
You are Done !

Credit Goes To :  mschwar  the original poster for the content

Wednesday, February 24, 2010

Installing MSSQL 2000 instance on top of MSSQL 2005

I experienced the issue of installing a SQL 2000 instance on top of an existing installation of SQL 2005. Having had the same frustration of not being able to see the 2000 instance remotely I searched and found this article. " must install SQL 2000 SP4 before 2005" on an article gave me a bit of a panic, but is a hint as to the solution.

SQL 2005 requires that you select "Allow remote connections to access this computer" on the Connections tab of server properties. It also requires that the "SQL Server Browser" service is running.

I checked these and found that installing the SQL 2000 had disabled the "SQL Server Browser" service. Restarting this service allowed remote connection to both the sql Server 2000 and 2005 instances.

Thursday, February 18, 2010

IE8 Standards mode and IE7 Compatibility mode


 Unfortunately, not all web sites confirm to IE8 standards today. If you have concerns about your web site rendering correctly in IE8 standards mode, then there is some help for you out there:
You can address the issues on a page-by-page basis using the following HTTP meta-tag:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

This tag, when seen by IE8 will render the page in IE7 compatibility mode regardless of whether or not you’ve set IE to run in IE8 Standards mode or not.  This is a great solution for that one page in the site that doesn’t quite render correctly in Standards mode.

Note: Including this in the Asp.net Master Page(s) will cater for IE7 compatibility in all the Content Pages that use the Master Page.

for more info : http://www.ditii.com/2008/08/28/ie8-standards-mode-and-ie7-compatibility-mode/



Wednesday, February 10, 2010

AjaxControlToolkit and SmartNavigation

After spending almost 4 hours investigating the following problem with an Ajax extension object:




‘Error: Sys.ArgumentTypeException: Object of type ‘AjaxControlToolkit.PopupBehavior’ cannot be converted to type ‘AjaxControlToolkit.PopupBehavior’.
I finally realized the page was using the smartNavigation directive. This directive has become obsolete with .NET 2.0, you can now use Page.SetFocus() and Page.MaintainScrollPositionOnPostBack() instead to obtain similar result. However, during the upgrade process from 1.1, the wizard didn’t flag this directive as a problem, so I was not aware of its presence until I came across the problem above.
Anyway, I was not surprised that upon removing this directive, the page was no longer throwing the javascript exception.
Lesson learned.

source : http://rafsystems.com/2007/05/22/ajaxcontroltoolkit-and-smartnavigation/