Monday, May 17, 2010

Using C# to convert Tif to Pdf

Below is code that allows you to create a PDF file from one or more Tif files (even those with multiple pages).

This code references iTextSharp:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing.Imaging;


Below the code is setting up the document/pdf and adding a tif file to the pdf:

string sMergedFiles = "C:\\PDFTest\\PdfReport.PDF";

string sTiffFiles = "C:\\PDFTest\\TiffFiles\\";

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sMergedFiles, FileMode.CreateNew));

document.Open();

//here you can loop thru and call AddTiff2Pdf for multiple files

string tiffFileName = Path.Combine(sTiffFiles, "asdf.tif");

AddTiff2Pdf(tiffFileName, ref writer, ref document);

string tiffFileName = Path.Combine(sTiffFiles, "1234.tif");

AddTiff2Pdf(tiffFileName, ref writer, ref document);

document.Close();


Below is the function used to add the tiff to the pdf:

private void AddTiff2Pdf(string tiffFileName, ref PdfWriter writer, ref Document document)
{

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(tiffFileName);

int numberOfPages = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

PdfContentByte cb = writer.DirectContent;

for (int page = 0; page < numberOfPages; page++)
{

bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page);

System.IO.MemoryStream stream = new System.IO.MemoryStream();

bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(stream.ToArray());

stream.Close();

img.ScalePercent(72f / bitmap.HorizontalResolution * 100);

img.SetAbsolutePosition(0, 0);

cb.AddImage(img);

document.NewPage();
}
}

Tuesday, May 11, 2010

CRM 4.0: Hiding Save Buttons

Recently had a client that wanted users to only "Save as Completed" activities. That had some users that thought Save & Close would actually "close" the activity. To alleviate the potential error, we decided to remove all Save buttons (Save, Save & Close, and Save and New) from the menu bar and file menu. Below is the code to do this.


var CRMFORMTYPE_CREATE = 1;
var CRMFORMTYPE_UPDATE = 2;
var CRMFORMTYPE_READONLY = 3;
var CRMFORMTYPE_DISABLED = 4;
var CRMFORMTYPE_QUICKCREATE = 5;
var CRMFORMTYPE_BULKEDIT = 6;

if(crmForm.FormType == CRMFORMTYPE_UPDATE)
{
var li = document.getElementsByTagName('LI');
var i = 0;
while(i < li.length)
{
if (li[i].getAttribute('id') == '_MIcrmFormSave' ||
li[i].getAttribute('id') == '_MIcrmFormSubmitCrmForm59truetruefalse' ||
li[i].getAttribute('id') == '_MBcrmFormSave' ||
li[i].getAttribute('id') == '_MBcrmFormSubmitCrmForm59truetruefalse')
{
li[i].outerHTML='';
}

if (li[i].getAttribute('id') == '_MIcrmFormSaveAndClose')
{
document.getElementById('_MIcrmFormSaveAndClose').style.display = "none";
}
if (li[i].getAttribute('id') == '_MBcrmFormSaveAndClose')
{
document.getElementById('_MBcrmFormSaveAndClose').style.display = "none";
}

//alert(li[i].getAttribute('title') + ' | ' + li[i].getAttribute('id'));
i = i + 1;
}
}