C# Combine multiple pictures into one PDF

        I convert each picture into byte[], then convert all byte arrays into byte[][], and then process byte[][]. If you have pictures in other formats to be processed, you can convert them to the above data format first.

Language: C#

The third-party library used: PdfSharp.dll (you can directly use the PDFsharp package), as shown below

The idea is roughly as follows:

① Declare a PDF file object (PdfDocument variable)

②Traverse all the pictures we want to process

③ Declare a PDF page object (PdfPage variable), declare a drawing board (XGraphics object) belonging to the newly declared PDF page object , and then use the drawing board to draw the picture (byte[]) traversed this time on the PDF page object , and then insert the PDF page object into the PDF file object declared in step ① .

④At this point, after traversing all the images, the PDF file object also contains all the image data we want to process, so just save this PDF file object directly .

Here is the code:

 /// <summary>
        /// 保存PDF图像【适用于小批量图片】
        /// </summary>
        /// <param name="buffer">要处理的所有图片数据</param>
        /// <param name="path">PDF的保存路径</param>
        /// <returns></returns>
        public static void SavePdf(byte[][] buffer, string savePath)
        {
            if (buffer == null || buffer.Length == 0)
            {
                throw new Exception("文件流为空!");
            }             
            Verif(savePath);//此方法我用来验证路径,逻辑依自己的业务来写就好,不需要可直接去掉。

            string folderPath = Path.GetDirectoryName(savePath);
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
                
            PdfDocument doc = new PdfDocument();
            try
            {
                for (int i = 0; i < buffer.Length; i++)
                {
                    using (MemoryStream ms1 = new MemoryStream(buffer[i]))
                    {
                        //var stream = Image.FromStream(ms1);
                        //XImage img = XImage.FromGdiPlusImage(stream);
                        XImage img = XImage.FromStream(ms1);
                        PdfPage page = new PdfPage();
                        XGraphics xgr = XGraphics.FromPdfPage(page);
                        doc.Pages.Add(page);
                        xgr.DrawImage(img, 0, 0, page.Width.Value, page.Height.Value);//切记这里一定要指定宽高
                        
                        img.Dispose();
                    }
                }
                doc.Save(savePath);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                doc.Dispose();
                doc.Close();
            }
        }

Thunder point:

When drawing an image (that is, executing the xgr.DrawImage() function), be sure to specify the width and height, that is, page.Width.Value and page.Height.Value in the above code, otherwise, when processing a horizontal image, there will be a bug that the image is not fully displayed .

Well, that’s all for this article. If you guys have any suggestions, opinions or things you don’t understand, just communicate in the comment area.

Guess you like

Origin blog.csdn.net/weixin_45963929/article/details/131413073