itextsharp操作

    最近涉足印刷行业,需要做一个软件,这个软件在线接收了客户上传的pdf文件后(socket通讯,4字节发送文件大小,而后分包,总大小/8K=包数,以及最后一包的大小)........

    完成异步socket通讯功能后,现在需要做pdf的相关操作

  1.pdf转图片

     2.多pdf合并+有规律的合并

     3.图片转jpg

     4.图片转pdf

     5.拼版算法,求最小面积,得出最节约纸张的排版

 

      目前1~4步已经完成,正在做第五步。下面分享第二步的操作的代码

pdf管理类

using System.Collections.Generic;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;



namespace Pdf

{

    /// <summary>

    /// Pdf文件信息

    /// 印刷文件最多两页,且每页的长宽一样的

    /// </summary>

    public class PdfFileInfo

    {

        public string path { get; set; }



        public float width { get; set; }



        public float height { get; set; }



        public float X { get; set; }



        public float Y { get; set; }

    }



    /// <summary>

    /// pdf操作核心类

    /// </summary>

    public class PdfMange

    {

        /// <summary>

        /// 读取多个pdf文件,按照每个pdf文件顺序+页面顺序合并到一个pdf文件中

        /// </summary>

        /// <param name="fileList"></param>

        /// <param name="targetFile"></param>

        public static void ComBineMoreToOne(string[] fileList, string targetFile)

        {

            PdfReader reader;

            Document document = new Document();

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(targetFile, FileMode.Create));

            document.Open();



            PdfContentByte cb = writer.DirectContent;

            PdfImportedPage newPage;



            for (int i = 0; i < fileList.Length; i++)

            {

                reader = new PdfReader(fileList[i]);

                int iPageNum = reader.NumberOfPages;



                for (int j = 1; j <= iPageNum; j++)

                {

                    newPage = writer.GetImportedPage(reader, j);

                    document.NewPage();

                    cb.AddTemplate(newPage, 0, 0);

                }

            }

            document.Close();

        }

        /// <summary>

        /// 读取多个pdf文件,按照每个pdf文件顺序+页面顺序合并到一个pdf文件中的一页上,并且自动适应大小排序

        /// 1英寸=72磅     2.54厘米=1英寸   pdf单位磅

        /// </summary>

        /// <param name="fileList"></param>

        /// <param name="targetFile"></param>

        public static void ComBineMoreToOnePage(IList<PdfFileInfo> fileList, string targetFile)

        {

            float[] urx2 = GetUrxy(fileList);

            float urx = urx2[0], ury = urx2[1];

            int marginLeft = 0, marginRight = 0, marginTop = 0, marginBottom = 0;

            iTextSharp.text.Rectangle pagesize = new iTextSharp.text.Rectangle(urx, ury);

            Document document = new Document(pagesize, marginLeft, marginRight, marginTop, marginBottom);

            PdfReader reader;

            PdfImportedPage newPage;

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(targetFile, FileMode.Create));

            document.Open();

            PdfContentByte cb = writer.DirectContent;

            //=================================关键复制pdf内容代码段Start===============================================

            foreach (PdfFileInfo info in fileList)

            {

                reader = new PdfReader(info.path);

                int PageNum = reader.NumberOfPages;

                for (int j = 1; j <= PageNum; j++)

                {

                    if (j % 2 != 0&&j<3)//印刷行业规定最多只有2页,且只有一页的第二页要空

                    {

                        newPage = writer.GetImportedPage(reader, j);

                        cb.AddTemplate(newPage, info.X, info.Y);

                    }



                }

            }

            document.NewPage();

            foreach (PdfFileInfo info in fileList)

            {

                reader = new PdfReader(info.path);

                int PageNum = reader.NumberOfPages;

                for (int j = 1; j <= PageNum; j++)

                {

                    if (j % 2 == 0 && j < 3)//印刷行业规定最多只有2页,且只有一页的第二页要空

                    {

                        newPage = writer.GetImportedPage(reader, j);

                        cb.AddTemplate(newPage, info.X, info.Y);

                    }



                }

            }

            //=================================关键复制pdf内容代码段end===============================================

            document.Close();

        }



        public static float[] GetUrxy(IList<PdfFileInfo> fileList)

        {

            IList<float> x = new List<float>();

            IList<float> y = new List<float>();

            foreach (PdfFileInfo info in fileList)

            {

                x.Add(info.width + info.X);





                y.Add(info.height + info.Y);

            }



            for (int i = 0; i < x.Count; i++)

            {

                float temp = x[i];

                for (int j = 0; j < x.Count; j++)

                {

                    if (x[j] < x[i])

                    {

                        x[i] = x[j];

                        x[j] = temp;

                    }

                }

            }



            for (int i = 0; i < y.Count; i++)

            {

                float temp = y[i];

                for (int j = 0; j < y.Count; j++)

                {

                    if (y[j] < y[i])

                    {

                        y[i] = y[j];

                        y[j] = temp;

                    }

                }

            }



            float[] result = { x[0], y[0] };



            return result;

        }



        /// <summary>

        /// 计算pdf 毫米转磅

        /// </summary>

        /// <param name="size"></param>

        /// <returns></returns>

        public static float GetPdfSize(float size)

        {

            return (size / 10) / (float)2.54*(float)72;

        }



        //==============================================================================================================================================

    }

}

以及调用方法例子

/// <summary>

        /// 合并多个pdf文件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button4_Click(object sender, EventArgs e)

        {

            List<PdfFileInfo> fileList = new List<PdfFileInfo>();

            PdfFileInfo info = new PdfFileInfo();

            info.path = "c://41.pdf";

            info.X = 0;

            info.Y = 0;

            info.width = PdfMange.GetPdfSize(210);

            info.height = PdfMange.GetPdfSize(297);

            fileList.Add(info);



            PdfFileInfo info2 = new PdfFileInfo();

            info2.path = "c://5.pdf";

            info2.X = PdfMange.GetPdfSize(210); 

            info2.Y = 0;

            info2.width = PdfMange.GetPdfSize(210);

            info2.height = PdfMange.GetPdfSize(297);

            fileList.Add(info2);



            PdfFileInfo info3 = new PdfFileInfo();

            info3.path = "c://442.pdf";

            info3.X = PdfMange.GetPdfSize(210*2); ;

            info3.Y = 0;

            info3.width = PdfMange.GetPdfSize(210);

            info3.height = PdfMange.GetPdfSize(297);

            fileList.Add(info3);







            PdfMange.ComBineMoreToOnePage(fileList, "c://newpdf.pdf");



        }

 效果如下:

itextsharp操作

itextsharp操作

itextsharp操作

 

最终生成

itextsharp操作

你可能感兴趣的:(itext)