private iTextSharp.text.Image GetImageForPdf(Bitmap image, string sessionName) { iTextSharp.text.Image i = null; switch (PdfSettings.ImageType) { case PdfImageType.Tif: i = iTextSharp.text.Image.GetInstance(ImageProcessor.ConvertToImage(image, "TIFF", PdfSettings.ImageQuality, PdfSettings.Dpi), ImageFormat.Png); break; case PdfImageType.Png: i = iTextSharp.text.Image.GetInstance(ImageProcessor.ConvertToImage(image, "PNG", PdfSettings.ImageQuality, PdfSettings.Dpi), ImageFormat.Png); break; case PdfImageType.Jpg: i = iTextSharp.text.Image.GetInstance(ImageProcessor.ConvertToImage(image, "JPEG", PdfSettings.ImageQuality, PdfSettings.Dpi), ImageFormat.Jpeg); break; case PdfImageType.Bmp: i = iTextSharp.text.Image.GetInstance(ImageProcessor.ConvertToImage(image, "BMP", PdfSettings.ImageQuality, PdfSettings.Dpi), ImageFormat.Bmp); break; } return(i); }
public void AddImage(byte[] img) { iTextSharp.text.Image Image = iTextSharp.text.Image.GetInstance(img); Image.Alignment = 3; Image.ScaleAbsolute(150, 150); doc.Add(Image); }
public MemoryStream GeneratePDF() { var doc = new Document(); var ms = new MemoryStream(); var writer = PdfWriter.GetInstance(doc, ms); doc.Open(); PdfPTable table = new PdfPTable(1); PdfPCell cell = new PdfPCell(); string htmlBeforeImage = "<h1 class=\"text-center\">Silicon Shores</h1>\n" + "<h3 class=\"col-md-4\">One " + ticket_types.ticket_name + " Ticket</h3>"; string htmlAfterImage = "<p>Thank you for your ticket purchase! This ticket is redeemable any time at Silicon Shores Theme Park for a days admission</p>\n" + "<p>Requirements for redemption: " + ticket_types.ticket_restrictions + ".</p>\n" + File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/Views/TicketPdf.cshtml")); var bootstrap = File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/bootstrap.min.css")) + File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath("~/Content/bootstrap-theme.min.css")); ElementList list = XMLWorkerHelper.ParseToElementList(htmlBeforeImage, bootstrap); foreach (var element in list) { cell.AddElement(element); } iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(BarCodeImage(), BaseColor.WHITE); var scale = 115; img.ScalePercent(scale); cell.AddElement(img); list = XMLWorkerHelper.ParseToElementList(htmlAfterImage, bootstrap); foreach (var element in list) { cell.AddElement(element); } table.AddCell(cell); doc.Add(table); writer.CloseStream = false; doc.Close(); ms.Position = 0; return(ms); }
/// <summary> /// If adding an image directly, don't forget to call CreatePage /// </summary> /// <param name="image"></param> /// <param name="sessionName"></param> /// <param name="dimension"></param> private void AddImage(Image image, string sessionName, Rectangle dimension) { if (OnProcessImageForDisplay != null) { image = OnProcessImageForDisplay(image); } Bitmap bmp = ImageProcessor.GetAsBitmap(image, PdfSettings.Dpi); iTextSharp.text.Image i = GetImageForPdf(bmp, sessionName); AddImage(i, dimension); bmp.Dispose(); }
private void saveButton_Click(object sender, EventArgs e) { if (firstNameTextBox.Text != string.Empty && lastNameTextBox.Text != string.Empty && universityTextBox.Text != string.Empty && facultyTextBox.Text != string.Empty && specialtyTextBox.Text != string.Empty && groupTextBox.Text != string.Empty) { iTextSharp.text.Image img = null; var isImageSelected = true; try { img = iTextSharp.text.Image.GetInstance(_imageFileName); } catch (Exception) { MessageBox.Show("Select photo please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); isImageSelected = false; } if (!isImageSelected) { return; } var document = new Document(); PdfWriter.GetInstance(document, new FileStream(Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()), "surveys", $"{firstNameTextBox.Text}{lastNameTextBox.Text}.pdf"), FileMode.Create)); document.Open(); var paragraph = new Paragraph($"{firstNameTextBox.Text} {lastNameTextBox.Text}\n" + $"{dateOfBirthLabel.Text}: {dateOfBirthPicker.Value:dd/MM/yyyy}\n" + $"{universityLabel.Text}: {universityTextBox.Text}\n" + $"{facultyLabel.Text}: {facultyTextBox.Text}\n" + $"{specialtyLabel.Text}: {specialtyTextBox.Text}\n" + $"{groupLabel.Text}: {groupTextBox.Text}"); img.ScaleToFit(350f, 350f); img.Alignment = Element.ALIGN_MIDDLE; paragraph.Alignment = Element.ALIGN_CENTER; document.Add(img); document.Add(paragraph); document.Close(); MessageBox.Show("The survey has been saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Please fill in all fields.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void AddImage(iTextSharp.text.Image image, Rectangle dimension) { if (dimension == null) { if (PdfSettings.PdfPageSize == null) { //Getting Width of the image width adding the page right & left margin float width = image.Width / PdfSettings.Dpi * 72; //Getting Height of the image height adding the page top & bottom margin float height = image.Height / PdfSettings.Dpi * 72; //Creating pdf rectangle with the specified height & width for page size declaration dimension = new Rectangle(width, height); } else { dimension = PdfSettings.PdfPageSize; } } /*you __MUST__ call SetPageSize() __BEFORE__ calling NewPage() * AND __BEFORE__ adding the image to the document */ //Changing the page size of the pdf document based on the rectangle defined float dHeight = dimension.Height / _dpi; float dwidth = dimension.Width / _dpi; Rectangle newDim = new Rectangle(dwidth * 72.0f, dHeight * 72.0f); _doc.SetPageSize(newDim); image.SetAbsolutePosition(0, 0); image.ScaleAbsolute(_doc.PageSize.Width, _doc.PageSize.Height); _doc.NewPage(); _doc.Add(image); }
private void WriteCards(List <Card> cards, PdfPTable table) { foreach (var imageFile in cards) { if (File.Exists(imageFile.Path)) { System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageFile.Path); if (imageFile.WhiteBorder) { image = WhiteBorderConverter.ToWhiteBorder(image); } else { image = WhiteBorderConverter.CreateNonIndexedImage(image); } iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png); float scalePercent = 100.0f * 179.0f / (pic.Width); //pic.ScalePercent(51.0f); pic.ScalePercent(scalePercent); PdfPCell cell = new PdfPCell(pic); //cell.FixedHeight = pic.ScaledHeight; cell.FollowingIndent = 0; cell.Indent = 0; cell.Padding = 0; cell.SetLeading(0, 0); cell.BorderColor = iTextSharp.text.BaseColor.WHITE; table.AddCell(cell); } } }
private void WriteCards(List <Card> cards, PdfPTable table, iTextSharp.text.Document document, KnownColor backColor) { foreach (var imageFile in cards) { if (File.Exists(imageFile.Path)) { System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(imageFile.Path); System.Drawing.Bitmap image = null; if (imageFile.BorderColor != System.Drawing.KnownColor.Black) { image = WhiteBorderConverter.ToWhiteBorder(originalImage, imageFile.BorderColor); } else { image = WhiteBorderConverter.CreateNonIndexedImage(originalImage); } GC.Collect(); GC.WaitForPendingFinalizers(); originalImage.Dispose(); originalImage = null; GC.Collect(); GC.WaitForPendingFinalizers(); if (myCubeFeaturesCheckbox.IsChecked == true) { WhiteBorderConverter.FixCorners(ref image, imageFile.BorderColor); // Do I need this for anything //if (image.Width > image.Height) //{ // image.RotateFlip(RotateFlipType.Rotate90FlipNone); //} if (ShowCardNumbers) { //image = AddCardNumber(ref image, imageFile); AddCardNumber(ref image, imageFile); } } //iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png); //iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Gif); iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Bmp); float scalePercent = 100.0f * 179.0f / (pic.Width); // For cube, also only make sure that the heights are correct if (true == this.myCubeFeaturesCheckbox.IsChecked) { scalePercent = 100.0f * 250.0f / (pic.Height); /* * float newHeight = pic.Height * scalePercent / 100.0f; * * // Limit height to 251 at the most * if (newHeight > 251) * { * scalePercent = 100.0f * 250.0f / (pic.Height); * } * * int p = 0;*/ } //pic.ScalePercent(51.0f); pic.ScalePercent(scalePercent); PdfPCell cell = new PdfPCell(pic); //cell.FixedHeight = pic.ScaledHeight; cell.FollowingIndent = 0; cell.Indent = 0; cell.Padding = 0; cell.SetLeading(0, 0); //cell.BorderColor = BaseColor.WHITE; //cell.BorderColor = new iTextSharp.text.BaseColor(System.Drawing.Color.FromKnownColor(backColor)); //cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.FromKnownColor(backColor)); cell.BorderColor = new iTextSharp.text.BaseColor(WhiteBorderConverter.GetCompatibleColor(backColor)); cell.BackgroundColor = new iTextSharp.text.BaseColor(WhiteBorderConverter.GetCompatibleColor(backColor)); table.AddCell(cell); // Draw a rectangle at the right of the doc //float vert = document.GetTop(0) - table.TotalHeight; //iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(0, vert, 50, vert+30); //rect.BackgroundColor = BaseColor.RED; //document.Add(rect); GC.Collect(); } } }
private void savePDF_btn_Click(object sender, EventArgs e) { if (info_dgv.Rows.Count > 0) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "PDF (*.pdf)|*.pdf"; sfd.FileName = "Output.pdf"; bool fileError = false; if (sfd.ShowDialog() == DialogResult.OK) { if (File.Exists(sfd.FileName)) { try { File.Delete(sfd.FileName); } catch (IOException ex) { fileError = true; MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message); } } if (!fileError) { try { PdfPTable pdfTable = new PdfPTable(info_dgv.Columns.Count); pdfTable.DefaultCell.Padding = 3; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; foreach (DataGridViewColumn column in info_dgv.Columns) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); pdfTable.AddCell(cell); } foreach (DataGridViewRow row in info_dgv.Rows) { string id = row.Cells[0].Value.ToString(); pdfTable.AddCell(id); string Fname = row.Cells[1].Value.ToString(); pdfTable.AddCell(Fname); string Lname = row.Cells[2].Value.ToString(); pdfTable.AddCell(Lname); string Bdate = row.Cells[3].Value.ToString(); pdfTable.AddCell(Bdate); string gender = row.Cells[4].Value.ToString(); pdfTable.AddCell(gender); string phone = row.Cells[5].Value.ToString(); pdfTable.AddCell(phone); string address = row.Cells[6].Value.ToString(); pdfTable.AddCell(address); byte[] imageByte = (byte[])row.Cells[7].Value; iTextSharp.text.Image Image = iTextSharp.text.Image.GetInstance(imageByte); pdfTable.AddCell(Image); } using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create)) { iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 20f, 20f, 10f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } MessageBox.Show("Data Exported Successfully !!!", "Info"); } catch (Exception ex) { MessageBox.Show("Error :" + ex.Message); } } } } else { MessageBox.Show("No Record To Export !!!", "Info"); } }
protected void GetSmallImage(PDFDocument document, PdfPTable table) { ImageHelper.SaveImageToDisk(GetWorkshopImage(), Path.Combine(ImageHelper.GetTemporaryDirectory(), "logo.png")); iTextSharp.text.Image itextSharpImage = document.GetImage(Path.Combine(ImageHelper.GetTemporaryDirectory(), "logo.png"), 205, 53); document.AddSpannedCell(table, itextSharpImage, 4, document.GetRegularFont(false), 40); }