Loading

PDF合版文件

PDF合版文件

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace PDF
{
    public partial class PDFConvert
    {
        private const float _QUAD_HEIGHT = 59F / _PT_CM_RATE;//709F;
        private const float _QUAD_WIDTH = 59F / _PT_CM_RATE;//709F;
        private const int _QUAD_ROWS = 18;
        private const float _QUAD_HEIGHT_OFFSET = _QUAD_HEIGHT + 40;

        private string Quad_CalcLossString(float qty)
        {
            int rlt = Convert.ToInt32(qty * 9 + (qty - 1) / 2 - 0.1);
            return rlt.ToString();
        }

        public bool toQuads(string[] paras)
        {
            bool rtn = false;

            // @"20160625114457166782_001_C0001_产品描述_C_02_01.pdf,20160706_001,7.5";'
            for (int f = 0; f < paras.Length; f++)
            {

                string[] row = paras[f].Split(',');
                //out_filename = row[1];

                float qty = Convert.ToSingle(row[2]);
                string out_filename = row[1] + "_C_" + Quad_CalcLossString(qty) + ".PDF";
                toQuad(getQuadFilenamePre(row[0]), out_filename, 0);
                out_filename = row[1] + "_M_1.PDF";
                toQuad(getQuadFilenamePre(row[0]), out_filename, 1);
            }
            rtn = true;
            return rtn;

        }
        void toQuad(string filename, string out_filename, int barcode_rows = 0)
        {

            string filename_p1;
            string filename_p2;
            filename_p1 = filename + "_01.PDF";
            filename_p2 = filename + "_02.PDF";
            #region 生成印刷文件
            /////////////////////////////////////////////////////////////////////////////////////
            // 生成印刷文件        

            // 创建一个文档变量
            //Rectangle nSize = new Rectangle(_QUAD_HEIGHT * 12F + 20F, _QUAD_WIDTH * 18F);
            float urx = 726 / _PT_CM_RATE;
            float ury = 1062 / _PT_CM_RATE;
            Rectangle nSize = new Rectangle(urx, ury);
            Document document = new Document(nSize, 0, 0, 0, 0);
            // 创建该文档
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(out_filename, FileMode.Create));
            // 打开文档
            document.Open();
            document.NewPage();

            PdfContentByte cb = writer.DirectContent;
            //读封面PDF内容 创建一个PdfReader对象
            PdfReader reader_p1 = new PdfReader(filename_p1);
            //int n = reader_p1.NumberOfPages;
            Rectangle psize_p1 = reader_p1.GetPageSize(1);

            float width = psize_p1.Width;
            float height = psize_p1.Height;

            if (!(isEquel(width, _QUAD_WIDTH) && isEquel(height, _QUAD_HEIGHT)))
                throw new Exception(string.Format("文件{0}的格式不符合要求", filename_p1));
            // 添加内容
            PdfImportedPage page1 = writer.GetImportedPage(reader_p1, 1);

            //读封底PDF内容 创建一个PdfReader对象
            PdfReader reader_p2 = new PdfReader(filename_p2);
            //int n = reader_p2.NumberOfPages;
            Rectangle psize_p2 = reader_p2.GetPageSize(1);

            width = psize_p2.Width;
            height = psize_p2.Height;

            if (!(isEquel(width, _QUAD_WIDTH) && isEquel(height, _QUAD_HEIGHT)))
                throw new Exception(string.Format("文件{0}的格式不符合要求", filename_p1));
            PdfImportedPage page2 = writer.GetImportedPage(reader_p2, 1);

            //出血位插入黑边
            string pdf_right = getCurrentPath() + "pdf_right.pdf";
            PdfReader reader_black = new PdfReader(pdf_right);
            PdfImportedPage page_black = writer.GetImportedPage(reader_black, 1);


            string floorfile = getCurrentPath() + "floor_quad.pdf";
            PdfReader reader_floor = new PdfReader(floorfile);
            PdfImportedPage page_floor = writer.GetImportedPage(reader_floor, 1);
            //写入出血位


            for (int y = 0; y <= 18; y++)    //3010.75024
            {
                for (int x = 1; x < 12; x++)  //2058.19653
                {
                    if (x > 6)
                    {
                        float startx = x * _QUAD_WIDTH - 6;
                        float starty = 0;
                        if (y == 0)
                        {
                            starty = 0;
                        }
                        if (y == 18)
                        {
                            starty = ury - _QUAD_HEIGHT;
                        }

                        if (0 < y && y < 18)
                        {
                            starty = y * _QUAD_HEIGHT - 4;
                        }
                        cb.AddTemplate(page2, 0, 1f, 1f, 0, startx + 4, starty);
                        cb.AddTemplate(page2, 0, 1f, 1f, 0, startx + 8, starty);
               
                    }
                    else
                    {
                        float startx = x * _QUAD_WIDTH - 6;
                        float starty = 0;
                        if (y == 0)
                        {
                            starty = 0;
                        }
                        if (y == 18)
                        {
                            starty = ury - _QUAD_HEIGHT;
                        }

                        if (0 < y && y < 18)
                        {
                            starty = y * _QUAD_HEIGHT - 4;
                        }
                        cb.AddTemplate(page1, 0, 1f, 1f, 0, startx + 4, starty);
                        cb.AddTemplate(page1, 0, 1f, 1f, 0, startx + 8, starty);
                    
                    }
                }
            }



            //写入出血位
            for (int z = 0; z < _QUAD_ROWS; z++) //列数
            {
                //写入出血位
                if (z >= barcode_rows)
                {
                    //左边 0,插入1.5的P1
                    cb.AddTemplate(page1, 0, 1f, -1f, 0
                        , _QUAD_WIDTH * 1 //3F / _PT_CM_RATE
                        , _QUAD_HEIGHT * (_QUAD_ROWS - 1 - z));

                    //中间5MM, 正反各2.5, 在1.5的位置重复插入一次
                    cb.AddTemplate(page1, 0, -1f, 1f, 0
                        , _QUAD_HEIGHT * (5) + 4F / _PT_CM_RATE
                        , _QUAD_WIDTH * (_QUAD_ROWS - z));
                    cb.AddTemplate(page1, 0, -1f, 1f, 0
                        , _QUAD_HEIGHT * (5) + 3.5F / _PT_CM_RATE
                        , _QUAD_WIDTH * (_QUAD_ROWS - z));
                    cb.AddTemplate(page1, 0, -1f, 1f, 0
                        , _QUAD_HEIGHT * (5) + 2.5F / _PT_CM_RATE
                        , _QUAD_WIDTH * (_QUAD_ROWS - z));

                    cb.AddTemplate(page2, 0, 1f, -1f, 0
                        , _QUAD_WIDTH * 7 + 4F / _PT_CM_RATE//3F / _PT_CM_RATE
                        , _QUAD_HEIGHT * (_QUAD_ROWS - 1 - z));
                    cb.AddTemplate(page2, 0, 1f, -1f, 0
                        , _QUAD_WIDTH * 7 + 5F / _PT_CM_RATE//3F / _PT_CM_RATE
                        , _QUAD_HEIGHT * (_QUAD_ROWS - 1 - z));
                    cb.AddTemplate(page2, 0, 1f, -1f, 0
                        , _QUAD_WIDTH * 7 + 6F / _PT_CM_RATE//3F / _PT_CM_RATE
                        , _QUAD_HEIGHT * (_QUAD_ROWS - 1 - z));


                    //最右边留1.5插入背面
                    cb.AddTemplate(page2, 0, -1f, 1f, 0
                        , _QUAD_HEIGHT * (11) + 8F / _PT_CM_RATE
                        , _QUAD_WIDTH * (_QUAD_ROWS - z));
                }

                //插入出血位左边 2mm的黑边 ->改到最右边
                cb.AddTemplate(page_black, 1f, 0, 0, 1f
                    , 716F / _PT_CM_RATE
                    , _QUAD_HEIGHT * (z));

            }

            ////写入目标PDF
            for (int x = barcode_rows; x < _QUAD_ROWS; x++) //列数
            {
                for (int y = 0; y < 6; y++) //行数
                {


                    //写入列
                    if (y % 2 == 0)
                    {//左转90度
                        cb.AddTemplate(page1, 0, 1f, -1f, 0
                            , _QUAD_HEIGHT * (y + 1) + 1.5F / _PT_CM_RATE
                            , _QUAD_WIDTH * (_QUAD_ROWS - 1 - x));
                        cb.AddTemplate(page2, 0, 1f, -1f, 0
                            , _QUAD_HEIGHT * (y + 7) + 6.5F / _PT_CM_RATE
                            , _QUAD_WIDTH * (_QUAD_ROWS - 1 - x));
                    }
                    else
                    {//右转90度
                        cb.AddTemplate(page1, 0, -1f, 1f, 0
                            , _QUAD_HEIGHT * (y) + 1.5F / _PT_CM_RATE
                            , _QUAD_WIDTH * (_QUAD_ROWS - x));
                        cb.AddTemplate(page2, 0, -1f, 1f, 0
                            , _QUAD_HEIGHT * (y + 6) + 6.5F / _PT_CM_RATE
                            , _QUAD_WIDTH * (_QUAD_ROWS - x));

                    }
                }
            }
            // Console.WriteLine(x.ToString());

            if (barcode_rows > 0)
            {

                //开始写入条码 
                string sVal = getBarcodeValue(filename);
                string sBarcode = Code128.GetCode128B(sVal);

                for (int x = 0; x < barcode_rows; x++)
                {
                    for (int y = 0; y < 6; y++)
                    {
                        //写入列
                        if (y % 2 == 0)
                        {//左转90度
                            cb.AddTemplate(page1, 0, 1f, -1f, 0
                                , _QUAD_HEIGHT * (y + 1) + 1.5F / _PT_CM_RATE
                                , _QUAD_WIDTH * (_QUAD_ROWS - x - 1));

                            cb.AddTemplate(page_floor, 0, 1f, -1f, 0
                                , _QUAD_HEIGHT * (y + 7) + 6.5F / _PT_CM_RATE
                                , _QUAD_WIDTH * (_QUAD_ROWS - x - 1));

                        }
                        else
                        {//右转90度
                            cb.AddTemplate(page1, 0, -1f, 1f, 0
                                , _QUAD_HEIGHT * (y) + 1.5F / _PT_CM_RATE
                                , _QUAD_WIDTH * (_QUAD_ROWS - x));

                            cb.AddTemplate(page_floor, 0, -1f, 1f, 0
                                , _QUAD_HEIGHT * (y + 6) + 6.5F / _PT_CM_RATE
                                , _QUAD_WIDTH * (_QUAD_ROWS - x));
                        }
                    }
                    for (int y = 6; y < 12; y++)
                    {

                        float offset = (y < 6 ? 1.5F : 6.5F) / _PT_CM_RATE - (y % 2 == 0 ? 15 : -30);
                        BaseFont bf = BaseFont.CreateFont(@"c:\windows\fonts\calibri.ttf"
                            , BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设置字体类型
                        cb.BeginText();
                        cb.SetFontAndSize(bf, 14);//设置字体大小
                        cb.SetCharacterSpacing(0);//设定字间距 
                        cb.SetRGBColorFill(0, 0, 0);//设定文本颜色 
                        cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, sVal
                            , ((y + 0) * _QUAD_WIDTH) + _QUAD_WIDTH / 2 + offset
                            , (_QUAD_ROWS - x - 2) * _QUAD_HEIGHT + _QUAD_HEIGHT_OFFSET
                            , 0);

                        cb.EndText();

                        for (int i = 0; i < 4; i++)
                        {
                            bf = BaseFont.CreateFont(@"c:\windows\fonts\code128.TTF"
                                , BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设置字体类型

                            cb.BeginText();
                            cb.SetFontAndSize(bf, 20);//设置字体大小
                            cb.SetCharacterSpacing(0);//设定字间距 
                            cb.SetRGBColorFill(0, 0, 0);//设定文本颜色 


                            cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, sBarcode
                                , ((y + 0) * _QUAD_WIDTH) + _QUAD_WIDTH / 2 + offset
                                , (_QUAD_ROWS - x - 2) * _QUAD_HEIGHT + _QUAD_HEIGHT_OFFSET + 20 + i * 17
                                , 0);

                            cb.EndText();
                        }


                    }

                }
            }
            



            
            cb.Stroke();
            // 关闭文档
            document.Close();


            #endregion


        }
    }
}

 

posted @ 2016-09-20 15:16  Sam Xiao  阅读(160)  评论(0编辑  收藏  举报