如何使用 iTextSharp 将多个图像组合成单个 pdf

我已经实现了代码,但问题是我只能将一个图像组合成一个 pdf,但我想将多个图像组合成一个 pdf。我的代码是:

public void ImagesToPdf(string[] imagepaths, string pdfpath)
        {
            using (var ms = new MemoryStream())
            {
                var srcImage = new Bitmap(imagepaths[0].ToString());
                iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height);
                var document = new iTextSharp.text.Document(pageSize, 0, 0, 0, 0);
                iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
                document.Open();
                var image = iTextSharp.text.Image.GetInstance(imagepaths[0].ToString());
                document.Add(image);
                document.Close();
                File.WriteAllBytes(pdfpath, ms.ToArray());
            }
        }

任何建议都会有很大帮助。谢谢

stack overflow How to combine multiple images into single pdf using iTextSharp
原文答案

答案:

作者头像

我终于想出了如何完成这项任务。

public static byte[] ConvertIntoSinglePDF(List<string> filePaths)
        {
            Document doc = new Document();
            doc.SetPageSize(PageSize.A4);

            var ms = new System.IO.MemoryStream();
            {
                PdfCopy pdf = new PdfCopy(doc, ms);
                doc.Open();

                foreach (string path in filePaths)
                {
                    byte[] data = File.ReadAllBytes(path);
                    doc.NewPage();
                    Document imageDocument = null;
                    PdfWriter imageDocumentWriter = null;
                    switch (Path.GetExtension(path).ToLower().Trim('.'))
                    {
                        case "bmp":
                        case "gif":
                        case "jpg":
                        case "png":
                            imageDocument = new Document();
                            using (var imageMS = new MemoryStream())
                            {
                                imageDocumentWriter = PdfWriter.GetInstance(imageDocument, imageMS);
                                imageDocument.Open();
                                if (imageDocument.NewPage())
                                {
                                    var image = iTextSharp.text.Image.GetInstance(data);
                                    image.Alignment = Element.ALIGN_CENTER;
                                    image.ScaleToFit(doc.PageSize.Width - 10, doc.PageSize.Height - 10);
                                    if (!imageDocument.Add(image))
                                    {
                                        throw new Exception("Unable to add image to page!");
                                    }
                                    imageDocument.Close();
                                    imageDocumentWriter.Close();
                                    PdfReader imageDocumentReader = new PdfReader(imageMS.ToArray());
                                    var page = pdf.GetImportedPage(imageDocumentReader, 1);
                                    pdf.AddPage(page);
                                    imageDocumentReader.Close();
                                }
                            }
                            break;
                        case "pdf":
                            var reader = new PdfReader(data);
                            for (int i = 0; i < reader.NumberOfPages; i++)
                            {
                                pdf.AddPage(pdf.GetImportedPage(reader, i + 1));
                            }
                            pdf.FreeReader(reader);
                            reader.Close();
                            break;
                        default:
                            break;
                    }
                }

                if (doc.IsOpen()) doc.Close();
                return ms.ToArray();
            }
        }

获得字节数组后,只需将所有字节写入文件即可

byte[]  document= ImagesToPDF.ConvertIntoSinglePDF(images);
File.WriteAllBytes("cheque.pdf", document);
作者头像

ASP.NET C# Code Less

string FileNamePdf = PdfFileName.Text;
 Document document = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 10f);

       var output = new FileStream(Path.Combine(ObjData.ParentFolderPath + "SaveLocation", FileNamePdf + ".pdf"), FileMode.Create);
                var writer =  PdfWriter.GetInstance(document, output);

  document.Open();

                foreach (HttpPostedFile file in FilesToConvert.PostedFiles)
                {
                    Image image = iTextSharp.text.Image.GetInstance(file.InputStream);
                    image.ScaleAbsolute(575f, 820.25f);
                    document.Add(image);
                    writer.NewPage();

                }

  document.Close();