26 Nisan 2011 Salı

ITextSharp Create Barcode Image

Itextsharp'da barkod image yaratmanız için yazmış olduğum method...
Reference lardan System.Drawing'i eklemeyi unutmayın...


       public static Image CreateBarcodeImage(string barcodeText)
        {
            Barcode39 code39 = new Barcode39();
            code39.BarHeight = 5;
            code39.Code = barcodeText;
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
            MemoryStream ms = new MemoryStream();
            bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Image img = iTextSharp.text.Image.GetInstance(ms.ToArray());
            return img;
        }

HL7 To XML Converter

Günümüz teknolojisinde ASTM(American Society For Testing And Materials) yani enerji endüstrisinde kullanılan birçok standart yöntemlerden sorumlu olan kuruluş. yerini HL7 teknolojisi aldı ve gayri ihtiyari bunun içinde hl7 kodlama standartlarını xml e dönüştürme methodları yazılmak durumunda kalındı. Size Method örneklerinden bulduğum ve sizinle paylaşmak istediğim bizzat test edip işe yaradığını gördüğüm methodu paylaşmak istedim... İyi kodlamalar...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

public static class HL7ToXmlConverter
    {
        private static XmlDocument _xmlDoc;

        public static string ConvertToXml(string sHL7)
        {
            // Go and create the base XML
            _xmlDoc = CreateXmlDoc();

            // HL7 message segments are terminated by carriage returns,
            // so to get an array of the message segments, split on carriage return
            string[] sHL7Lines = sHL7.Split('\r');

            // Now we want to replace any other unprintable control
            // characters with whitespace otherwise they'll break the XML
            for (int i = 0; i < sHL7Lines.Length; i++)
            {
                sHL7Lines[i] = Regex.Replace(sHL7Lines[i], @"[^ -~]", "");
            }

            /// Go through each segment in the message
            /// and first get the fields, separated by pipe (|),
            /// then for each of those, get the field components,
            /// separated by carat (^), and check for
            /// repetition (~) and also check each component
            /// for subcomponents, and repetition within them too.
            for (int i = 0; i < sHL7Lines.Length; i++)
            {
                // Don't care about empty lines
                if (sHL7Lines[i] != string.Empty)
                {
                    // Get the line and get the line's segments
                    string sHL7Line = sHL7Lines[i];
                    string[] sFields = HL7ToXmlConverter.GetMessgeFields(sHL7Line);

                    // Create a new element in the XML for the line
                    XmlElement el = _xmlDoc.CreateElement(sFields[0]);
                    _xmlDoc.DocumentElement.AppendChild(el);

                    // For each field in the line of HL7
                    for (int a = 0; a < sFields.Length; a++)
                    {
                        // Create a new element
                        XmlElement fieldEl = _xmlDoc.CreateElement(sFields[0] +
                                             "." + a.ToString());
                        /// Part of the HL7 specification is that part
                        /// of the message header defines which characters
                        /// are going to be used to delimit the message
                        /// and since we want to capture the field that
                        /// contains those characters we need
                        /// to just capture them and stick them in an element.
                        if (sFields[a] != @"^~\&")
                        {
                            /// Get the components within this field, separated by carats (^)
                            /// If there are more than one, go through and create an element for
                            /// each, then check for subcomponents, and repetition in both.
                            string[] sComponents = HL7ToXmlConverter.GetComponents(sFields[a]);
                            if (sComponents.Length > 1)
                            {
                                for (int b = 0; b < sComponents.Length; b++)
                                {
                                    XmlElement componentEl = _xmlDoc.CreateElement(sFields[0] +
                                               "." + a.ToString() +
                                               "." + b.ToString());

                                    string[] subComponents = GetSubComponents(sComponents[b]);
                                    if (subComponents.Length > 1)
                                    // There were subcomponents
                                    {
                                        for (int c = 0; c < subComponents.Length; c++)
                                        {
                                              // Check for repetition
                                            string[] subComponentRepetitions =
                                                     GetRepetitions(subComponents[c]);
                                            if (subComponentRepetitions.Length > 1)
                                            {
                                                for (int d = 0;
                                                     d < subComponentRepetitions.Length;
                                                     d++)
                                                {
                                                    XmlElement subComponentRepEl =
                                                      _xmlDoc.CreateElement(sFields[0] +
                                                      "." + a.ToString() +
                                                      "." + b.ToString() +
                                                      "." + c.ToString() +
                                                      "." + d.ToString());
                                                    subComponentRepEl.InnerText =
                                                         subComponentRepetitions[d];
                                                    componentEl.AppendChild(subComponentRepEl);
                                                }
                                            }
                                            else
                                            {
                                                XmlElement subComponentEl =
                                                  _xmlDoc.CreateElement(sFields[0] +
                                                  "." + a.ToString() + "." +
                                                  b.ToString() + "." + c.ToString());
                                                subComponentEl.InnerText = subComponents[c];
                                                componentEl.AppendChild(subComponentEl);
                                            }
                                        }
                                        fieldEl.AppendChild(componentEl);
                                    }
                                    else // There were no subcomponents
                                    {
                                        string[] sRepetitions =
                                           HL7ToXmlConverter.GetRepetitions(sComponents[b]);
                                        if (sRepetitions.Length > 1)
                                        {
                                            XmlElement repetitionEl = null;
                                            for (int c = 0; c < sRepetitions.Length; c++)
                                            {
                                                repetitionEl =
                                                  _xmlDoc.CreateElement(sFields[0] + "." +
                                                  a.ToString() + "." + b.ToString() +
                                                  "." + c.ToString());
                                                repetitionEl.InnerText = sRepetitions[c];
                                                componentEl.AppendChild(repetitionEl);
                                            }
                                            fieldEl.AppendChild(componentEl);
                                            el.AppendChild(fieldEl);
                                        }
                                        else
                                        {
                                            componentEl.InnerText = sComponents[b];
                                            fieldEl.AppendChild(componentEl);
                                            el.AppendChild(fieldEl);
                                        }
                                    }
                                }
                                el.AppendChild(fieldEl);
                             }
                            else
                            {
                                fieldEl.InnerText = sFields[a];
                                el.AppendChild(fieldEl);
                            }
                        }
                        else
                        {
                            fieldEl.InnerText = sFields[a];
                            el.AppendChild(fieldEl);
                        }
                    }
                }
            }

            return _xmlDoc.OuterXml;
        }

        /// <summary>
        /// Split a line into its component parts based on pipe.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string[] GetMessgeFields(string s)
        {
            return s.Split('|');
        }
        /// <summary>
        /// Get the components of a string by splitting based on carat.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string[] GetComponents(string s)
        {
            return s.Split('^');
        }

        /// <summary>
        /// Get the subcomponents of a string by splitting on ampersand.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string[] GetSubComponents(string s)
        {
            return s.Split('&');
        }

        /// <summary>
        /// Get the repetitions within a string based on tilde.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string[] GetRepetitions(string s)
        {
            return s.Split('~');
        }

        /// <summary>
        /// Create the basic XML document that represents the HL7 message
        /// </summary>
        /// <returns></returns>
        private static XmlDocument CreateXmlDoc()
        {
            XmlDocument output = new XmlDocument();
            XmlElement rootNode = output.CreateElement("HL7Message");
            output.AppendChild(rootNode);
            return output;
        }

    }

Kullanımı:
string myHL7string = @"MSH|^~\&|||||20080925161613||ADT^A05||P|2.6";
txtHL7.Text = myHL7string;
txtConvertHL7.Text = HL7ToXmlConverter.ConvertToXml(myHL7string);

14 Nisan 2011 Perşembe

ITextSharp CropPages

Itextsharp da yazdığınız pdf sayfasını belirlediğiniz koordinat noktalarından kesmek için yazmış olduğum bir method...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;

        public void CropPages(String source, String destination,float llx, float lly, float urx, float ury)
        {
            PdfReader reader = new PdfReader(source);
            int n = reader.NumberOfPages;
            PdfDictionary pageDict;
            PdfRectangle rect = new PdfRectangle(llx, lly, urx, ury);
            for (int i = 1; i <=n; i++)
            {
                pageDict = reader.GetPageN(i);
                pageDict.Put(PdfName.CROPBOX,rect);
            }
            PdfStamper stamper = new PdfStamper(reader, new FileStream(destination, FileMode.Create));
            stamper.Close();
        }

8 Nisan 2011 Cuma

ITextSharp Compress,Decompress PDF

İşinize yarar umuduyla kullanacağımız pdf leri compress ve decompress etmeye yarayan iki tane method...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;

        public void compressPdf(String Source, String Destination)
        {
            PdfReader reader = new PdfReader(Source);

            PdfStamper stamper = new PdfStamper(reader, new FileStream(Destination, FileMode.Create), PdfWriter.VERSION_1_7);

            stamper.Writer.CompressionLevel = 9;

            int total = reader.NumberOfPages + 1;

            for (int i = 1; i < total; i++)
            {
                reader.SetPageContent(i, reader.GetPageContent(i));
            }
            stamper.SetFullCompression();
            stamper.Close();
        }

        public void decompressPdf(String source, String Destination)
        {
            PdfReader reader = new PdfReader(source);
            PdfStamper stamper = new PdfStamper(reader, new FileStream(Destination, FileMode.Create));
            Document.Compress = false;
            int total = reader.NumberOfPages + 1;
            for (int i = 1; i < total; i++)
            {
                reader.SetPageContent(i, reader.GetPageContent(i));
            }
            stamper.Close();
            Document.Compress = true;
        }

ITextSharp Create Table

ItextSharp'da Tablo oluşturmak için yazmış olduğum bir extension method...

        public PdfPTable CreateTable(PdfPTable table, PdfPCell cell, int row, int column)
        {
            table = new PdfPTable(row + column);


            for (int i = 0; i < column; i++)
            {
                for (int j = 0; j < row; j++)
                {
                    cell = new PdfPCell(new Phrase((i + 1).ToString() + " " + (j + 1).ToString()));
                    table.AddCell(new Phrase(i + j));
                }
            }
            table.SpacingAfter = 10f;

            return table;
        }