/// <summary> /// /// </summary> /// <param name="imageSourceList"></param> /// <param name="outPath"></param> /// <returns></returns> public async Task ConvertImages2PdfAsync(IEnumerable <string> imageSourceList, string outPath) { await Task.Run(() => { //var pdfWriter = new PdfWriter(outPath, // new WriterProperties().SetStandardEncryption(user, owner, // EncryptionConstants.ALLOW_PRINTING | EncryptionConstants.ALLOW_ASSEMBLY, // EncryptionConstants.ENCRYPTION_AES_256)); var pdfWriter = new PdfWriter(outPath); var pdfDocument = new PdfDocument(pdfWriter); var document = new Document(pdfDocument); foreach (var imageSource in imageSourceList) { var imageData = ImageDataFactory.Create(imageSource); var image = new iText.Layout.Element.Image(imageData); var a4Width = PageSize.A4.GetWidth(); var a4Height = PageSize.A4.GetHeight(); if (image.GetImageHeight() > a4Height - 25) { image.ScaleToFit(a4Width - 25, a4Height - 25); } else if (image.GetImageWidth() > a4Width - 25) { image.ScaleToFit(a4Width - 25, a4Height - 25); } image.SetHorizontalAlignment(HorizontalAlignment.CENTER); document.Add(image); } document.Close(); }); }
/// <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 new void ExportToPdf(string path, double width = 700, double height = 370) { // exports spectrum annotation w/o base seq annotation string tempPdfPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "temp.pdf"); string tempPngPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "annotation.png"); base.ExportToPdf(tempPdfPath, width, height); // scales for desired DPI double dpiScale = MetaDrawSettings.CanvasPdfExportDpi / 96.0; // save base seq as PNG SequenceDrawingCanvas.Measure(new Size((int)SequenceDrawingCanvas.Width, (int)SequenceDrawingCanvas.Height)); SequenceDrawingCanvas.Arrange(new Rect(new Size((int)SequenceDrawingCanvas.Width, (int)SequenceDrawingCanvas.Height))); RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)(dpiScale * SequenceDrawingCanvas.Width), (int)(dpiScale * SequenceDrawingCanvas.Height), MetaDrawSettings.CanvasPdfExportDpi, MetaDrawSettings.CanvasPdfExportDpi, PixelFormats.Pbgra32); renderBitmap.Render(SequenceDrawingCanvas); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); using (FileStream file = File.Create(tempPngPath)) { encoder.Save(file); } // adds base seq annotation to pdf PdfDocument pdfDoc = new PdfDocument(new PdfReader(tempPdfPath), new PdfWriter(path)); iText.Layout.Document document = new iText.Layout.Document(pdfDoc); ImageData imgData = ImageDataFactory.Create(tempPngPath); iText.Layout.Element.Image img = new iText.Layout.Element.Image(imgData); img.SetMarginLeft((float)(-1.0 * SequenceDrawingCanvas.Margin.Left) + 10); img.SetMarginTop(-30); img.ScaleToFit((float)SequenceDrawingCanvas.Width, (float)SequenceDrawingCanvas.Height); document.Add(img); document.Close(); pdfDoc.Close(); // delete temp files File.Delete(tempPdfPath); File.Delete(tempPngPath); }