static void Main(string[] args) { //var stream = new MemoryStream(file); var img = Image.FromFile("C:\\1.jpg"); var pdf = ImageToPdfConvetrer.ImageToPdf(img); pdf.Save("D:\\2.PDF"); }
public static void SavePDF(string _name) { glaves50 = new List <string>(); /* * var files = Directory.GetFiles($"{_name}", "*.pdf").OrderBy(d => new FileInfo(d).CreationTime); * foreach (var file in files) * { * PDFs.Add(PdfDocument.FromFile(file)); * } */ int i = 0, k = 0; string path = $"Сводка/{_name}"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } glaves50.AddRange(Directory.GetFiles($"{_name}", "*.pdf").OrderBy(d => new FileInfo(d).CreationTime)); var PDFs = new List <PdfDocument>(); foreach (var file in glaves50) { Console.WriteLine(file); PDFs.Add(PdfDocument.FromFile(file)); if (i >= 50) { PdfDocument PDFer = PdfDocument.Merge(PDFs); PDFer.SaveAs($"{path}/{_name}_{k}.pdf"); k++; i = 0; PDFs.Clear(); } i++; } if (PDFs.Count > 0) { PdfDocument PDFer = PdfDocument.Merge(PDFs); PDFer.SaveAs($"{path}/{_name}_{k}.pdf"); } PDFs.Clear(); var list = new List <string>(); list.AddRange(Directory.GetFiles($"{Directory.GetCurrentDirectory()}/{path}", "*.pdf").OrderBy(d => new FileInfo(d).CreationTime)); foreach (var f in list) { PDFs.Add(PdfDocument.FromFile(f)); } PdfDocument PDF = PdfDocument.Merge(PDFs); var image = Image.FromFile("book.jpg"); var cover = ImageToPdfConvetrer.ImageToPdf(image); PDF.PrependPdf(cover); PDF.SaveAs($"{_name}.pdf"); }
public void Print() { // the url need to be changed before using // store the attendee whose status is going List <SeminarAttendee> goingAttendees = this.seminarReference.Attendees.Where(a => a.Status == "Going").ToList(); // store the list of pdf, ready for merging List <PdfDocument> PDFs = new List <PdfDocument>(); // set up for handling align int counter = 0; Image background = getTemplate(); if (goingAttendees.Count == 0) { MessageBox.Show("There are no attendees marked as 'Going' for this Seminar.\nNo nametags will be created.", "No Attendees Going", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // draw the background of first page using (Graphics g = Graphics.FromImage(nameTag)) { g.DrawImage(background, 0, 0, 723, 962); } int attendeesIteratedThrough = 0; foreach (var attendee in goingAttendees) { //drawing the first page of name tags if (goingAttendees.IndexOf(attendee) % NameTagsPerPage != 0 || goingAttendees.IndexOf(attendee) == 0) { using (Graphics graphics = Graphics.FromImage(nameTag)) { using (Font arialFont = new Font("Arial", 20)) { // drawing the first column of name tags if (IsOdd(goingAttendees.IndexOf(attendee))) { graphics.DrawString(attendee.Name + " " + attendee.PhoneNumber, arialFont, Brushes.Black, new RectangleF(100, 55 + counter, Width, Height)); } else // drawing the second column of name tags { graphics.DrawString(attendee.Name + " " + attendee.PhoneNumber, arialFont, Brushes.Black, new RectangleF(400, 55 + counter, Width, Height)); counter += 135; } } } } else { // adding the pdf into pdf list, ready for merging //PDFs.Add(PdfDocument.FromFile(this.title)); counter = 0; using (Graphics graphics = Graphics.FromImage(nameTag)) { graphics.Clear(Color.White); graphics.DrawImage(background, 0, 0, 723, 962); using (Font arialFont = new Font("Arial", 20)) { if (IsOdd(goingAttendees.IndexOf(attendee))) { graphics.DrawString(attendee.Name + " " + attendee.PhoneNumber, arialFont, Brushes.Black, new RectangleF(100, 55 + counter, Width, Height)); } else // drawing the second column of name tags { graphics.DrawString(attendee.Name + " " + attendee.PhoneNumber, arialFont, Brushes.Black, new RectangleF(400, 55 + counter, Width, Height)); counter += 135; } } } } attendeesIteratedThrough++; if (attendeesIteratedThrough == goingAttendees.Count) { var reached = true; } if (attendeesIteratedThrough % NameTagsPerPage == 0 || attendeesIteratedThrough == goingAttendees.Count) { PDFs.Add(ImageToPdfConvetrer.ImageToPdf(nameTag).CopyPage(0)); } } PdfDocument result = PdfDocument.Merge(PDFs); SaveFileDialog printFile = new SaveFileDialog(); printFile.FileName = seminarReference.Title; printFile.Filter = "Pdf Files (*.pdf) | *.pdf | All Files(*.*) | *.*"; printFile.Title = "Save as"; DialogResult dr = printFile.ShowDialog(); if (dr == DialogResult.OK) { try { result.SaveAs(printFile.FileName); MessageBox.Show("Your nametag template is ready and has been saved to \n\n" + printFile.FileName, "Nametags created successfully", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Failed to Save", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }