Esempio n. 1
0
        private static void ImagesToPdf()
        {
            var sourceDir = $"{_projectDir}\\Samples\\outputs";
            var target    = $"{_projectDir}\\Samples\\combined.pdf";

            PdfCommon.Initialize();
            var watch = new Stopwatch();

            watch.Start();
            using (var doc = PdfDocument.CreateNew())
            {
                var files     = Directory.GetFiles(sourceDir, "*.jpg");
                var fileCount = files.Count();
                for (int pageIndex = 0; pageIndex < fileCount; pageIndex++)
                {
                    using (var image = Image.FromFile($"{sourceDir}\\output_{pageIndex}.jpg", true))
                    {
                        using (PdfBitmap pdfBitmap = new PdfBitmap(image.Width, image.Height, true))
                        {
                            using (var g = Graphics.FromImage(pdfBitmap.Image))
                            {
                                g.DrawImage(image, 0, 0, image.Width, image.Height);
                            }
                            var imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);
                            //Calculate size of image in PDF points
                            var size = CalculateSize(pdfBitmap.Width, pdfBitmap.Height, image.HorizontalResolution, image.VerticalResolution);
                            //Add empty page to PDF document
                            doc.Pages.InsertPageAt(pageIndex, size.Width, size.Height);
                            //Insert image to newly created page
                            doc.Pages[pageIndex].PageObjects.Add(imageObject);
                            //set image matrix
                            imageObject.Matrix = new FS_MATRIX(size.Width, 0, 0, size.Height, 0, 0);
                            //Generate PDF page content to content stream
                            doc.Pages[pageIndex].GenerateContent();
                        }
                    }
                }
                doc.Save(target, SaveFlags.NoIncremental);
            }
            watch.Stop();
            Console.WriteLine($"Combine images took {watch.Elapsed.TotalSeconds} seconds");
        }
    static public PdfImageObject InsertImageToPage(PdfDocument doc, PdfPage page, System.Drawing.Bitmap bmp, PointF atPoint)
    {
        var bi = bmp.LockBits(
            new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
            System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        //Create PdfBitmap object from .Net bitmap
        var bitmap = new PdfBitmap(
            bmp.Width,
            bmp.Height,
            BitmapFormats.FXDIB_Argb,
            bi.Scan0,
            bi.Stride);
        //Create pdf image object and then set PdfBitmap object into it.
        var image = PdfImageObject.Create(doc);

        image.SetBitmap(bitmap);
        //Scale image object to it's actual width and heihgt
        image.SetMatrix(bmp.Width, 0, 0, bmp.Height, (float)atPoint.X, (float)atPoint.Y);
        page.PageObjects.InsertObject(image);
        bmp.UnlockBits(bi);
        return(image);
    }
Esempio n. 3
0
        public void CreatePdf(string model, string starting_date, string ending_date)
        {
            saveFileDialog.Filter = "PDF|*.pdf";
            saveFileDialog.Title  = "Zapisz raport";
            saveFileDialog.ShowDialog();

            int pageIndex = 0;
            int charIndex = 0;
            int imgYPos   = 450;
            int textYPos  = 770;
            int imgCount  = 0;

            PdfCommon.Initialize();
            using (var doc = PdfDocument.CreateNew())
            {
                PdfFont       calibryBold = PdfFont.CreateFont(doc, "CalibriBold", true, 0, 0, 0, FontCharSet.ANSI_CHARSET, false);
                PdfTextObject textObject;
                if (DataTableEntityList.Select(x => x.part_number).Distinct().Count() == 1)
                {
                    List <string> part_number = DataTableEntityList.Select(x => x.part_number).Distinct().ToList();
                    textObject = PdfTextObject.Create("Model: " + model + "        Numer Czesci: " + part_number[0] + "        Data: " + starting_date + " - " + ending_date, 15, 810, calibryBold, 12);
                }
                else
                {
                    textObject = PdfTextObject.Create("Model: " + model + "           Data: " + starting_date + " - " + ending_date, 15, 810, calibryBold, 12);
                }
                textObject.FillColor = Color.Black;
                doc.Pages.InsertPageAt(0, 8.27f * 72, 11.69f * 72);
                doc.Pages[0].PageObjects.Add(textObject);

                foreach (Bitmap pic in picsForRaport)
                {
                    using (var image = pic)
                    {
                        using (PdfBitmap pdfBitmap = new PdfBitmap(image.Width, image.Height, true))
                        {
                            using (var g = Graphics.FromImage(pdfBitmap.Image))
                            {
                                g.DrawImage(image, 0, 0, image.Width, image.Height);
                            }
                            PdfTextObject characteristicTextObject = PdfTextObject.Create(Translator.TranslateColumnName(btnNamesForRaport[charIndex]), 15, textYPos, calibryBold, 12);
                            textYPos -= 350;
                            characteristicTextObject.FillColor = Color.Black;
                            charIndex++;
                            doc.Pages[pageIndex].PageObjects.Add(characteristicTextObject);

                            var imageObject = PdfImageObject.Create(doc, pdfBitmap, 0, 0);

                            imageObject.Matrix = new FS_MATRIX(500, 0, 0, 300, 50, imgYPos);
                            doc.Pages[pageIndex].PageObjects.Add(imageObject);
                            imgYPos -= 350;

                            doc.Pages[pageIndex].GenerateContent();
                            imgCount++;
                            if (imgCount == 2)
                            {
                                pageIndex++;
                                doc.Pages.InsertPageAt(pageIndex, 8.27f * 72, 11.69f * 72);
                                imgYPos  = 450;
                                textYPos = 770;
                                imgCount = 0;
                            }
                        }
                    }
                }
                if (imgCount == 0)
                {
                    doc.Pages.DeleteAt(doc.Pages.Count - 1);
                }
                if (saveFileDialog.FileName != "")
                {
                    doc.Save(saveFileDialog.FileName, SaveFlags.NoIncremental);
                    saveFileDialog.FileName = "";
                }
                //doc.Save(@"C:\Users\" + Environment.UserName + @"\Documents\sample_document.pdf", SaveFlags.NoIncremental);

                if (picsForRaport.Any())
                {
                    picsForRaport.Clear();
                }
                if (btnNamesForRaport.Any())
                {
                    btnNamesForRaport.Clear();
                }
            }
        }