private void btnIdentGenerate_Click(object sender, EventArgs e) { DocumentBase.CountryCode nationality; Enum.TryParse <DocumentBase.CountryCode>(cmbIdentNationality.SelectedValue.ToString(), out nationality); DocumentBase.CountryCode issuer; Enum.TryParse <DocumentBase.CountryCode>(cmbIdentIssuer.SelectedValue.ToString(), out issuer); DocumentBase.SexCode sex; Enum.TryParse <DocumentBase.SexCode>(cmbIdentSex.SelectedValue.ToString(), out sex); IdentityDocumentModel tmp = new IdentityDocumentModel( issuer, charRemap(txtIdentSurname.Text.Replace(' ', '<')), charRemap(txtIdentGiven.Text.Replace(' ', '<')), txtIdentDocNum.Text, dtpIdentDOB.Value.ToString("yyMMdd"), dtpIdentDOE.Value.ToString("yyMMdd"), charRemap(txtIdentOptionalOne.Text.Replace(' ', '<')), charRemap(txtIdentOptionalTwo.Text.Replace(' ', '<')), "", nationality, sex, txtIdentDocType.Text ); var MRZ = MrzGeneration.GenerateIdentityCardMRZ(tmp); tmp.MRZ = MRZ; PdfGeneration.GenerateIdentityCard(tmp); rtbMRZ.Text = MRZ[0] + Environment.NewLine + MRZ[1] + Environment.NewLine + MRZ[2]; }
public static string[] GenerateIdentityCardMRZ(IdentityDocumentModel document) { string[] MRZ = new string[3]; string lineOne = string.Empty; string lineTwo = string.Empty; string lineThree = string.Empty; lineOne = document.DocType.PadRight(2, '<'); lineOne += document.CountryOfIssue; lineOne += document.DocumentNum; lineOne += mrzCheckDigitConvert(document.DocumentNum); lineOne += document.OptionalOne; lineOne = lineOne.PadRight(30, '<').Substring(0, 30); lineTwo = document.Dob; lineTwo += mrzCheckDigitConvert(document.Dob); lineTwo += Convert.ToChar(Convert.ChangeType(document.Sex, document.Sex.GetTypeCode())); lineTwo += document.ExpDate; lineTwo += mrzCheckDigitConvert(document.ExpDate); lineTwo += document.Nationality; lineTwo += document.OptionalTwo; lineTwo = lineTwo.PadRight(30, '<').Substring(0, 29); lineTwo += mrzCheckDigitConvert(lineOne.Substring(5, 25) + lineTwo.Substring(0, 7) + lineTwo.Substring(8, 7) + lineTwo.Substring(18, 10)); lineThree = (document.SurName + "<<" + document.GivenNames).PadRight(30, '<').Substring(0, 30);; MRZ[0] = lineOne; MRZ[1] = lineTwo; MRZ[2] = lineThree; return(MRZ); //lineOne + Environment.NewLine + lineTwo + Environment.NewLine + lineThree; }
public static void GenerateIdentityCard(IdentityDocumentModel vm) { if (vm.MRZ.Length != 3) { throw new ArgumentException("MRZ should have 3 rows for indentity cards, not : " + vm.MRZ.Length); } // Create a new PDF document PdfDocument document = new PdfDocument(); document.Info.Title = "Created with PDFsharp"; // Create an empty page PdfPage page = document.AddPage(); // Get an XGraphics object for drawing XGraphics gfx = XGraphics.FromPdfPage(page); // Create a font XFont font = new XFont("OCR-B 10 BT", 12, XFontStyle.Regular); var width = XUnit.FromMillimeter(85.68); var height = XUnit.FromMillimeter(54.02); var heightMargin = XUnit.FromInch(.25); var widthMargin = XUnit.FromInch(.25); gfx.DrawRectangle(XBrushes.Beige, new XRect(widthMargin, heightMargin, width, height)); XImage image = XImage.FromFile(@"Templates\IndentityCard_bg.jpg"); gfx.DrawImage(image, widthMargin, heightMargin, width, height); // Draw the text gfx.DrawString(vm.MRZ[0], font, XBrushes.Black, new XRect(XUnit.FromMillimeter(4) + widthMargin, XUnit.FromMillimeter(36.4) + heightMargin, XUnit.FromMillimeter(80), 0), XStringFormats.Default); gfx.DrawString(vm.MRZ[1], font, XBrushes.Black, new XRect(XUnit.FromMillimeter(4) + widthMargin, XUnit.FromMillimeter(41) + heightMargin, XUnit.FromMillimeter(80), 0), XStringFormats.Default); gfx.DrawString(vm.MRZ[2], font, XBrushes.Black, new XRect(XUnit.FromMillimeter(4) + widthMargin, XUnit.FromMillimeter(44.5) + heightMargin, XUnit.FromMillimeter(80), 0), XStringFormats.Default); gfx.DrawLine(XPens.Black, 0, XUnit.FromMillimeter(60) + heightMargin, XUnit.FromInch(8.5), XUnit.FromMillimeter(60) + heightMargin); StringBuilder tmpText = new StringBuilder(); tmpText.AppendLine("DocType : " + vm.DocType); tmpText.AppendLine("CountryOfIssue : " + vm.CountryOfIssue); tmpText.AppendLine("SurName : " + vm.SurName); tmpText.AppendLine("GivenNames : " + vm.GivenNames); tmpText.AppendLine("DocumentNum : " + vm.DocumentNum); tmpText.AppendLine("Dob : " + vm.Dob); tmpText.AppendLine("ExpDate : " + vm.ExpDate); tmpText.AppendLine("OptionalOne : " + vm.OptionalOne); tmpText.AppendLine("OptionalTwo : " + vm.OptionalTwo); tmpText.AppendLine("Nationality : " + vm.Nationality); tmpText.AppendLine("Sex : " + vm.Sex); tmpText = tmpText.Replace('<', ' '); tmpText.AppendLine("MRZ : "); tmpText.AppendLine(string.Join("\n", vm.MRZ)); string text = tmpText.ToString(); XFont font2 = new XFont("Times New Roman", 10, XFontStyle.Bold); XTextFormatter tf = new XTextFormatter(gfx); XRect rect = new XRect(widthMargin, XUnit.FromMillimeter(62) + heightMargin, 250, XUnit.FromMillimeter(62) + heightMargin + 220); gfx.DrawRectangle(XBrushes.LightGray, rect); //tf.Alignment = ParagraphAlignment.Left; tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft); // Save the document... //string filename = "IndentityCard_"+DateTime.Now.ToOADate().ToString()+".pdf"; string filename = "IdentityCard.pdf"; document.Save(filename); System.Diagnostics.Process.Start(filename); }