public void VisitTableCell(TableCell tableCell) { _pdfCell = _pdfRow.Cells[_currentCellIndex++]; }
public void MakeGiftCert(int itemID) { try { GiftCertificateInfo item; //load the item GiftCertificateController controller = new GiftCertificateController(); item = controller.GetGiftCert(itemId); string CertAmountWords = ""; string MailToAddressField = ""; string CertAmountNumber = ""; if (item != null) { if (item.PaypalPaymentState.ToString().Length == 0) { Response.Redirect(Globals.NavigateURL("Access Denied"), true); } TextInfo textInfo = new CultureInfo("en-US", false).TextInfo; CertAmountWords = textInfo.ToTitleCase(NumberToWords(Int32.Parse(item.CertAmount.ToString())).ToString()).ToString() + " Dollars" + Environment.NewLine; CertAmountNumber = "$" + String.Format("{0:f2}", item.CertAmount).ToString(); MailToAddressField = item.MailTo.ToString() + Environment.NewLine + item.MailToAddress.ToString() + Environment.NewLine + item.MailToCity.ToString() + ", " + item.MailToState.ToString() + " " + item.MailToZip.ToString(); _CertNotes = item.Notes.ToString(); } string myPortalName = this.PortalSettings.PortalName.ToString(); Document document = new Document(); document.Info.Author = "Joseph Aucoin"; document.Info.Keywords = "Gift Certificate"; document.Info.Title = myPortalName.ToString() + " Gift Certificate"; // Get the A4 page size MigraDoc.DocumentObjectModel.Unit width, height; PageSetup.GetPageSize(PageFormat.Letter, out width, out height); // Add a section to the document and configure it such that it will be in the centre // of the page Section section = document.AddSection(); section.PageSetup.PageHeight = height; section.PageSetup.PageWidth = width; section.PageSetup.LeftMargin = 20; section.PageSetup.RightMargin = 10; section.PageSetup.TopMargin = 20; // height / 2; //++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++ // ADD LOGO string myLogo = PortalSettings.HomeDirectoryMapPath + _CertLogo.ToString(); // "Images\\Chapins-Logo.png"; MigraDoc.DocumentObjectModel.Shapes.Image image = section.Headers.Primary.AddImage(myLogo.ToString()); image.LockAspectRatio = true; image.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Line; image.RelativeHorizontal = MigraDoc.DocumentObjectModel.Shapes.RelativeHorizontal.Margin; image.Top = MigraDoc.DocumentObjectModel.Shapes.ShapePosition.Top; image.Left = MigraDoc.DocumentObjectModel.Shapes.ShapePosition.Right; image.WrapFormat.Style = MigraDoc.DocumentObjectModel.Shapes.WrapStyle.Through; MigraDoc.DocumentObjectModel.Tables.Table HeaderTable = new MigraDoc.DocumentObjectModel.Tables.Table(); HeaderTable.Borders.Width = 0; // Default to show borders 1 pixel wide Column HeaderTable.LeftPadding = 10; HeaderTable.RightPadding = 10; HeaderTable.Borders.Left.Visible = false; HeaderTable.Borders.Right.Visible = false; // Add 1 columns float myColumnWidth = ((section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin) / 2); // ((width / 2) - 10); MigraDoc.DocumentObjectModel.Tables.Column column0 = HeaderTable.AddColumn(myColumnWidth); MigraDoc.DocumentObjectModel.Tables.Row row1 = HeaderTable.AddRow(); row1.Cells[0].Elements.AddParagraph(_CertReturnAddress.ToString()); row1.Cells[0].Format.Alignment = ParagraphAlignment.Left; section.Add(HeaderTable); /// SPACING MigraDoc.DocumentObjectModel.Paragraph paragraph = section.AddParagraph(); paragraph.Format.LineSpacingRule = MigraDoc.DocumentObjectModel.LineSpacingRule.Exactly; paragraph.Format.LineSpacing = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(30.0); // section.Add(paragraph); //++++++++++++++++++++++++++++++ // ADD ANOTHER TABLE FOR MAIL TO ADDRESS // Create a table so that we can draw the horizontal lines MigraDoc.DocumentObjectModel.Tables.Table tableMailToAddress = new MigraDoc.DocumentObjectModel.Tables.Table(); tableMailToAddress.Borders.Width = 0; // Default to show borders 1 pixel wide Column tableMailToAddress.LeftPadding = 20; tableMailToAddress.RightPadding = 10; // float myColumnWidth100percent = (width - 10); var column = tableMailToAddress.AddColumn((section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin)); double fontHeight = 20; Font font = new Font("Times New Roman", fontHeight); // Add a row with a single cell for the first line MigraDoc.DocumentObjectModel.Tables.Row row = tableMailToAddress.AddRow(); MigraDoc.DocumentObjectModel.Tables.Cell cell = row.Cells[0]; cell = row.Cells[0]; cell.Format.Font.Color = Colors.Black; cell.Format.Alignment = ParagraphAlignment.Left; cell.Format.Font.ApplyFont(font); cell.AddParagraph(MailToAddressField.ToString()); section.Add(tableMailToAddress); // ADD SPACER+++++++++++++++++++++++++++++++++++++++ MigraDoc.DocumentObjectModel.Paragraph paragraph1 = section.AddParagraph(); paragraph1.Format.LineSpacingRule = MigraDoc.DocumentObjectModel.LineSpacingRule.Exactly; paragraph1.Format.LineSpacing = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(20.0); // ADD ANOTHER TABLE FOR CERT // Create a table so that we can draw the horizontal lines MigraDoc.DocumentObjectModel.Tables.Table tableCert = new MigraDoc.DocumentObjectModel.Tables.Table(); tableCert.Borders.Width = 0; // Default to show borders 1 pixel wide Column tableCert.LeftPadding = 10; tableCert.RightPadding = 10; tableCert.Borders.Right.Visible = true; // float myColumnWidth100percent = (width - 10); float sectionWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin; float columnWidth = sectionWidth; var columnCert = tableCert.AddColumn(columnWidth); columnCert.Format.Alignment = ParagraphAlignment.Center; double fontHeightCert = 30; Font fontCert = new Font("Times New Roman", fontHeightCert); // Add a row with a single cell for the first line MigraDoc.DocumentObjectModel.Tables.Row rowCert = tableCert.AddRow(); MigraDoc.DocumentObjectModel.Tables.Cell cellCert = rowCert.Cells[0]; cellCert.Format.Font.Color = Colors.Black; cellCert.Format.Font.ApplyFont(fontCert); cellCert.Borders.Left.Visible = false; cellCert.Borders.Right.Visible = false; cellCert.Borders.Bottom.Visible = false; cellCert.AddParagraph(_CertBannerText.ToString()); // Add a row with a single cell for the second line rowCert = tableCert.AddRow(); cellCert = rowCert.Cells[0]; cellCert.Format.Font.Color = Colors.Black; cellCert.Format.Alignment = ParagraphAlignment.Center; cellCert.Format.Font.ApplyFont(fontCert); cellCert.Borders.Left.Visible = false; cellCert.Borders.Right.Visible = false; cellCert.Borders.Top.Visible = false; cellCert.AddParagraph(CertAmountWords.ToString()); // 3RD LINE // Add a row with a single cell for the third line rowCert = tableCert.AddRow(); cellCert = rowCert.Cells[0]; Font fontCertDetails = new Font("Times New Roman", 20); cellCert.Format.Font.Color = Colors.Black; cellCert.Format.Alignment = ParagraphAlignment.Left; cellCert.Format.Font.ApplyFont(fontCertDetails); cellCert.Borders.Left.Visible = false; cellCert.Borders.Right.Visible = false; cellCert.Borders.Top.Visible = false; cellCert.Format.SpaceBefore = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(10.0); cellCert.Format.LeftIndent = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(15.0); cellCert.AddParagraph("Date:" + item.CreatedDate.ToShortDateString() + Environment.NewLine + "Presented to: " + item.ToName.ToString() + Environment.NewLine + "From: " + item.FromName.ToString() + Environment.NewLine + "Amount: " + CertAmountNumber.ToString()); //////////////////////////////// // 4th LINE // Add a row with a single cell for the forth line rowCert = tableCert.AddRow(); cellCert = rowCert.Cells[0]; Font fontCertSignature = new Font("Arial", 12); cellCert.Format.Font.Color = Colors.Black; cellCert.Format.Alignment = ParagraphAlignment.Left; cellCert.Format.Font.ApplyFont(fontCertSignature); cellCert.Borders.Left.Visible = false; cellCert.Borders.Right.Visible = false; cellCert.Borders.Top.Visible = false; cellCert.Format.SpaceBefore = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(10.0); cellCert.Format.LeftIndent = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(15.0); cellCert.AddParagraph("Authorized Signature: __________________________________________________"); //////////////////////////////// // 5th LINE // Add a row with a single cell for the fifth line rowCert = tableCert.AddRow(); cellCert = rowCert.Cells[0]; Font fontCertInvoiceNumber = new Font("Arial", 10); cellCert.Format.Font.Color = Colors.Black; cellCert.Format.Alignment = ParagraphAlignment.Center; cellCert.Format.Font.ApplyFont(fontCertInvoiceNumber); cellCert.Borders.Left.Visible = false; cellCert.Borders.Right.Visible = false; cellCert.Borders.Top.Visible = false; // cellCert.Format.AddTabStop("16cm", TabAlignment.Right); cellCert.Format.SpaceBefore = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(3.0); //cellCert.Format.LeftIndent = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(15.0); cellCert.AddParagraph("Certificate Number 00" + item.ItemId.ToString()); document.LastSection.Add(tableCert); if (item.Notes.ToString().Trim().Length > 0) { //+++++++++ ADD PARAGRAPH FOR BUYER NOTES MigraDoc.DocumentObjectModel.Paragraph paragraphFooter = section.AddParagraph(); paragraphFooter.Format.LineSpacingRule = MigraDoc.DocumentObjectModel.LineSpacingRule.Exactly; paragraphFooter.Format.LineSpacing = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(8.0); paragraphFooter.Format.SpaceBefore = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(10.0); paragraphFooter.AddText("NOTES:" + Environment.NewLine + _CertNotes.ToString()); } Paragraph footerText = new Paragraph(); footerText.Format.Alignment = ParagraphAlignment.Center; footerText.AddText(_CertFooterText.ToString()); section.Footers.Primary.Add(footerText); // Create a renderer PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); // Associate the MigraDoc document with a renderer pdfRenderer.Document = document; // Layout and render document to PDF pdfRenderer.RenderDocument(); string pdfFilename = PortalSettings.HomeDirectoryMapPath + _PdfFilesFolder.ToString() + "Cert" + itemId.ToString() + ".pdf"; if (File.Exists(pdfFilename)) { File.Delete(pdfFilename); } pdfRenderer.PdfDocument.Save(pdfFilename); bool watermarkIsNeeded = false; if (_CertWatermark.ToString().Trim().Length > 1) { watermarkIsNeeded = true; } if (watermarkIsNeeded) { PdfDocument pdfIn = PdfReader.Open(pdfFilename, PdfDocumentOpenMode.Import); PdfDocument pdfOut = new PdfDocument(); for (int i = 0; i < pdfIn.PageCount; i++) { PdfPage pg = pdfIn.Pages[i]; pg = pdfOut.AddPage(pg); // WATERMARK TEXT string draftFlagStr = _CertWatermark.ToString(); // Get an XGraphics object for drawing beneath the existing content XGraphics gfx = XGraphics.FromPdfPage(pg, XGraphicsPdfPageOptions.Prepend); // Get the size (in point) of the text XFont fontWM = new XFont("Verdana", 62, XFontStyle.Bold); XSize size = gfx.MeasureString(draftFlagStr, fontWM); // Define a rotation transformation at the center of the page gfx.TranslateTransform(pg.Width / 2, pg.Height / 2); gfx.RotateTransform(-Math.Atan(pg.Height / pg.Width) * 180 / Math.PI); gfx.TranslateTransform(-pg.Width / 2, -pg.Height / 2); // Create a string format XStringFormat format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Near; // Create a dimmed red brush XBrush brush = new XSolidBrush(XColor.FromArgb(32, 0, 0, 255)); // Draw the string gfx.DrawString(draftFlagStr, fontWM, brush, new XPoint((pg.Width - size.Width) / 2, (pg.Height - size.Height) / 2), format); } pdfOut.Save(pdfFilename); } // Save and show the document // pdfRenderer.PdfDocument.Save("TestDocument.pdf"); Process.Start("explorer.exe", pdfFilename); HyperLinkPDF.Visible = true; HyperLinkPDF.NavigateUrl = PortalSettings.HomeDirectory + _PdfFilesFolder.ToString() + "Cert" + itemId.ToString() + ".pdf"; } catch (Exception ex) { Exceptions.ProcessModuleLoadException(this, ex); } }