Exemple #1
0
        /// <summary>
        /// 利用itext7生成文字签名
        /// </summary>
        public void ConvertPdf1()
        {
            string sourcePath = $"C:\\test\\source.pdf";
            string targetPath = $"C:\\test\\target.pdf";
            string fontPath   = $"C:\\Windows\\Fonts\\simkai.ttf";

            string signPath1 = @"C:\Users\Administrator\Desktop\a.png";
            string signPath2 = @"C:\Users\Administrator\Desktop\b.png";
            string signPath3 = @"C:\Users\Administrator\Desktop\c.png";
            string signPath4 = @"C:\Users\Administrator\Desktop\d.png";


            //输入PDF
            using (iText.Kernel.Pdf.PdfReader reader = new iText.Kernel.Pdf.PdfReader(sourcePath))
            {
                //输出PDF
                using (iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(targetPath))
                {
                    //获取PDF对象
                    using (iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(reader, writer))
                    {
                        //获取Document对象
                        using (iText.Layout.Document document = new iText.Layout.Document(pdfDocument))
                        {
                            //从物理文件加载图片
                            iText.Layout.Element.Image image1 = new iText.Layout.Element.Image(iText.IO.Image.ImageDataFactory.Create(signPath1));
                            iText.Layout.Element.Image image2 = new iText.Layout.Element.Image(iText.IO.Image.ImageDataFactory.Create(signPath2));
                            iText.Layout.Element.Image image3 = new iText.Layout.Element.Image(iText.IO.Image.ImageDataFactory.Create(signPath3));
                            iText.Layout.Element.Image image4 = new iText.Layout.Element.Image(iText.IO.Image.ImageDataFactory.Create(signPath4));

                            //将图片绘制到PDF的绝对坐标上,同时缩放图片
                            //坐标与绘制文字的坐标几乎一致,稍微向左,向上一些
                            //缩放的宽度与后面的宽度一致,示例中是200
                            //缩放的高度计算两个签名之间的高度差,例如93-73=20
                            //注意示例采用的签名图片的尺寸是:400px * 150px,应当采取和它差不多的尺寸效果最佳
                            document.Add(image1.ScaleToFit(200, 20).SetFixedPosition(1, 3089, 93, 200));
                            document.Add(image2.ScaleToFit(200, 20).SetFixedPosition(1, 3089, 73, 200));
                            document.Add(image3.ScaleToFit(200, 20).SetFixedPosition(1, 3089, 53, 200));
                            document.Add(image4.ScaleToFit(200, 20).SetFixedPosition(1, 3089, 33, 200));

                            //加载字体
                            iText.Kernel.Font.PdfFont font = iText.Kernel.Font.PdfFontFactory.CreateFont(fontPath, iText.IO.Font.PdfEncodings.IDENTITY_H, true);

                            //添加文本
                            document.Add(new iText.Layout.Element.Paragraph("签名1").SetFont(font).SetFontSize(12).SetFixedPosition(1, 3090, 90, 200));
                            document.Add(new iText.Layout.Element.Paragraph("签名2").SetFont(font).SetFontSize(12).SetFixedPosition(1, 3090, 70, 200));
                            document.Add(new iText.Layout.Element.Paragraph("签名3").SetFont(font).SetFontSize(12).SetFixedPosition(1, 3090, 50, 200));
                            document.Add(new iText.Layout.Element.Paragraph("签名4").SetFont(font).SetFontSize(12).SetFixedPosition(1, 3090, 30, 200));
                        }
                    }
                }
            }
        }
        public static void createDocument(List <ImageSelection> listImages, Appearance appearance, PageSize pageSize, string exportFile)
        {
            checkSameDocuments(ref exportFile);

            using (PdfWriter writer = new PdfWriter(exportFile))
            {
                using (PdfDocument pdf = new PdfDocument(writer))
                {
                    Document pdoc = new Document(pdf, pageSize);

                    pdoc.SetMargins(14.1665f, 14.1665f, 14.1665f, 14.1665f);



                    Table table = tableStyle(appearance, pageSize, pdoc);



                    for (int k = 0, i = 0, j = 0; k < listImages.Count; k++, j++)
                    {
                        if (j == appearance.getDimensions().Length)
                        {
                            j = 0; i++;
                        }
                        if (i == appearance.getHeightRatio())
                        {
                            pdoc.Add(table);
                            table = tableStyle(appearance, pageSize, pdoc);

                            pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                            j = i = 0;
                        }

                        Image img = new Image(iText.IO.Image.ImageDataFactory.Create(listImages.ElementAt(k).getPath()));

                        imgStyle(ref img, listImages.ElementAt(k));


                        table.GetCell(i, j).Add(img);
                    }
                    pdoc.Add(table);


                    pdoc.Close();
                }
            }
        }
Exemple #3
0
        public static void createDocument(List <ImageSelection> listImages, Appearance appearance, PageSize pageSize, string exportFile, Format format)
        {
            checkSameDocuments(ref exportFile);

            using (PdfWriter writer = new PdfWriter(exportFile))
            {
                using (PdfDocument pdf = new PdfDocument(writer))
                {
                    Document pdoc = new Document(pdf, pageSize);
                    pdoc.SetMargins(8.5f, 8.5f, 8.5f, 8.5f);
                    Appearance coverAppearance = new Appearance(appearance.getAppearanceType(), format, 1);

                    Table table = tableStyle(coverAppearance, pageSize, pdoc);
                    table = tableStyle(coverAppearance, pageSize, pdoc);
                    Image imgc = null;
                    try
                    {
                        imgc = new Image(iText.IO.Image.ImageDataFactory.Create(listImages.ElementAt(0).getPath()));
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                        MessageBox.Show("There's no image selected !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        pdoc = null;
                        //pdf = null;
                        pdf.AddNewPage();
                        pdf.Close();
                        writer.Close();
                        File.Delete(exportFile);

                        return;
                    }

                    imgStyle(ref imgc, listImages.ElementAt(0));

                    table.GetCell(0, 0).Add(imgc);
                    pdoc.Add(table);



                    table = tableStyle(appearance, pageSize, pdoc);
                    pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));

                    for (int k = 1, i = 0, j = 0; k < listImages.Count - 1; k++, j++)
                    {
                        if (j == appearance.getDimensions().Length)
                        {
                            j = 0; i++;
                        }
                        if (i == appearance.getHeightRatio())
                        {
                            pdoc.Add(table);
                            table = tableStyle(appearance, pageSize, pdoc);

                            pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                            j = i = 0;
                        }

                        Image img = new Image(iText.IO.Image.ImageDataFactory.Create(listImages.ElementAt(k).getPath()));

                        imgStyle(ref img, listImages.ElementAt(k));
                        Console.WriteLine(listImages.ElementAt(k).format);


                        table.GetCell(i, j).Add(img);
                    }
                    pdoc.Add(table);

                    pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));


                    table = tableStyle(coverAppearance, pageSize, pdoc);
                    imgc  = new Image(iText.IO.Image.ImageDataFactory.Create(listImages.ElementAt(listImages.Count - 1).getPath()));
                    imgStyle(ref imgc, listImages.ElementAt(listImages.Count - 1));
                    table.GetCell(0, 0).Add(imgc);
                    pdoc.Add(table);


                    pdoc.Close();
                }
            }
        }
Exemple #4
0
        public static void createDocument(List <ImageSelection> listImages, Appearance appearance, PageSize pageSize, string exportFile, Format format, bool value)
        {
            checkSameDocuments(ref exportFile);

            Console.WriteLine();
            using (PdfWriter writer = new PdfWriter(exportFile))
            {
                using (PdfDocument pdf = new PdfDocument(writer))
                {
                    Document pdoc = new Document(pdf, pageSize);
                    pdoc.SetMargins(0f, 0f, 0f, 0f);
                    Appearance coverAppearance = new Appearance(appearance.getAppearanceType(), format, 1);
                    Appearance currentAppearance;



                    List <ImageSelection> currentImgsList = new List <ImageSelection>();
                    List <Image>          imgs            = new List <Image>();
                    List <Image>          currentImgs     = new List <Image>();

                    for (int i = 0; i < listImages.Count; i++)
                    {
                        imgs.Add(new Image(iText.IO.Image.ImageDataFactory.Create(listImages.ElementAt(i).getPath())));
                        imgs.ElementAt(i).SetHorizontalAlignment(HorizontalAlignment.CENTER);
                        imgs.ElementAt(i).SetAutoScale(true);


                        imgStyle(imgs.ElementAt(i), listImages.ElementAt(i));
                    }
                    setOrientation = true;


                    /// First page
                    Table table = null;
                    try
                    {
                        imgs.ElementAt(0).SetAutoScale(true);

                        table = tableStyle(coverAppearance, pageSize, pdoc);


                        table.GetCell(0, 0).SetMargins(0f, 0f, 0f, 0f);
                        table.GetCell(0, 0).SetPaddings(0f, 0f, 0f, 0f);


                        table.SetMargins(0f, 0f, 0f, 0f);
                        table.SetPaddings(0f, 0f, 0f, 0f);
                        //table.GetCell(0, 0).SetBorder(BorderStyle.FixedSingle);
                        table.GetCell(0, 0).SetVerticalAlignment(VerticalAlignment.MIDDLE);
                        table.GetCell(0, 0).Add(imgs.ElementAt(0));

                        pdoc.Add(table);

                        if (imgs.Count > 2)
                        {
                            pdoc.SetMargins(8.5f, 8.5f, 8.5f, 8.5f);
                        }
                        pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                        MessageBox.Show("There's no image selected !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        pdoc = null;
                        //pdf = null;
                        pdf.AddNewPage();
                        pdf.Close();
                        writer.Close();
                        File.Delete(exportFile);

                        return;
                    }



                    for (int k = 1, p = 0; k < listImages.Count - 1; k++)
                    {
                        currentImgsList.Add(listImages.ElementAt(k));
                        currentImgs.Add(imgs.ElementAt(k));
                        if (k % appearance.getImgNumber() == 0)
                        {
                            currentAppearance = new Appearance(appearance.getAppearanceType(), format, appearance.getImgNumber(), getLandscape(currentImgsList), getPortrait(currentImgsList));
                            Console.WriteLine(currentAppearance.specialCond);
                            switch (currentAppearance.specialCond)
                            {
                            case "21x15_4P0L": table = Custom.customTable1(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x15_3P1L": table = Custom.customTable2(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x15_2P2L": table = Custom.customTable3(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x15_1P3L": table = Custom.customTable3(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x15_0P4L": table = Custom.customTable3(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;


                            case "20x20_1P2L": table = Custom.customTable4(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_2P1L": table = Custom.customTable5(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_0P3L": table = Custom.customTable6(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_3P0L": table = Custom.customTable7(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_4_1P3L": table = Custom.customTable8(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_4_2P2L": table = Custom.customTable9(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_4_0P4L": table = Custom.customTable10(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_4_3P1L": table = Custom.customTable11(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "20x20_4_4P0L": table = Custom.customTable12(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;


                            case "21x30_4_1P3L": table = Custom.customTable13(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_4_2P2L": table = Custom.customTable14(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_4_3P1L": table = Custom.customTable15(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_4_0P4L": table = Custom.customTable16(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_4_4P0L": table = Custom.customTable17(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_5P1L": table = Custom.customTable18(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_1P5L": table = Custom.customTable19(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_2P4L": table = Custom.customTable20(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_3P3L": table = Custom.customTable21(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_4P2L": table = Custom.customTable22(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_0P6L": table = Custom.customTable23(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;

                            case "21x30_6_6P0L": table = Custom.customTable24(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs); break;
                            }
                            Console.WriteLine("Portraits: " + getPortrait(currentImgsList) + "Landscapes: " + getLandscape(currentImgsList));
                            currentImgsList.Clear();
                            currentImgs.Clear();



                            p++;
                            if (((listImages.Count - 2) / appearance.getImgNumber() == p) && ((listImages.Count - 2) % appearance.getImgNumber() == 0))
                            {
                                pdoc.SetMargins(0f, 0f, 0f, 0f);
                            }

                            pdoc.Add(table);
                            pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                        }
                    }
                    if (currentImgsList.Count > 0)
                    {
                        currentAppearance = new Appearance("reminder", appearance.getImgNumber());
                        table             = Custom.customTable3(currentAppearance, pageSize, pdoc, currentImgsList, currentImgs);
                        currentImgsList.Clear();
                        currentImgs.Clear();

                        pdoc.Add(table);

                        pdoc.SetMargins(0f, 0f, 0f, 0f);
                        pdoc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                    }


                    imgs.ElementAt(imgs.Count - 1).SetAutoScale(true);


                    table = tableStyle(coverAppearance, pageSize, pdoc);


                    table.GetCell(0, 0).SetMargins(0f, 0f, 0f, 0f);
                    table.GetCell(0, 0).SetPaddings(0f, 0f, 0f, 0f);


                    table.SetMargins(0f, 0f, 0f, 0f);
                    table.SetPaddings(0f, 0f, 0f, 0f);
                    //table.GetCell(0, 0).SetBorder(BorderStyle.FixedSingle);
                    table.GetCell(0, 0).SetVerticalAlignment(VerticalAlignment.MIDDLE);
                    table.GetCell(0, 0).Add(imgs.ElementAt(imgs.Count - 1));

                    pdoc.Add(table);


                    pdoc.Close();
                }
            }
        }