Пример #1
0
        private void btnInvoice_Click(object sender, EventArgs e)
        {
            textBox4.Clear();
            if (textBox2.Text.Length < 1)
            {
                MessageBox.Show("ソースファイルを選択してください。");
                return;
            }
            else if (textBox3.Text.Length < 1)
            {
                MessageBox.Show("目標フォルダを選択してください。");
                return;
            }
            else
            {
                string   allLog = "";
                DateTime dt     = DateTime.Now;
                try
                {
                    string       sourceFileName = textBox2.Text.Split(',')[0];
                    string       TargetPath = textBox3.Text + @"\\";
                    SetRectangle JudgePDFType = new SetRectangle(100, 0, 50, 300, sourceFileName);
                    int          cx1 = 0, cy1 = 0, cx2 = 0, cy2 = 0, dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
                    if (JudgePDFType.GetPreName(1).Length >= 7)
                    {
                        if (Regex.IsMatch(JudgePDFType.GetPreName(1).Substring(7).Split(' ')[0], @"^[+-]?\d*[.]?\d*$"))
                        {
                            cx1 = 100; cy1 = 0; cx2 = 50; cy2 = 300;
                            dx1 = 50; dy1 = 350; dx2 = 10; dy2 = 20;
                        }
                        else
                        {
                            cx1 = 0; cy1 = 450; cx2 = 400; cy2 = 50;
                            dx1 = 330; dy1 = 540; dx2 = 150; dy2 = 15;
                        }
                    }
                    else
                    {
                        cx1 = 0; cy1 = 450; cx2 = 400; cy2 = 50;
                        dx1 = 330; dy1 = 540; dx2 = 150; dy2 = 15;
                    }

                    SetRectangle invoiceCustRec = new SetRectangle(cx1, cy1, cx2, cy2, sourceFileName);
                    SetRectangle invoiceDateRec = new SetRectangle(dx1, dy1, dx2, dy2, sourceFileName);

                    int times = 1;


                    for (int page = 1; page <= invoiceCustRec.pdfDoc.GetNumberOfPages();)
                    {
                        string strCustRec   = invoiceCustRec.GetPreName(page);
                        string customerCode = strCustRec.Substring(7).Split(' ')[0];
                        string customerName = strCustRec.Substring(strCustRec.IndexOf(' ', 9) + 1).Trim();
                        string date         = invoiceDateRec.GetPreName(page).Trim();

                        //名前設定
                        string targetFolderName = customerCode + "_" + customerName;
                        string fileName         = date + "_" + targetFolderName + ".pdf";

                        PdfReader pdfReader = new PdfReader(sourceFileName);


                        //--------------------------------
                        if (!Directory.Exists(TargetPath + targetFolderName))
                        {
                            Directory.CreateDirectory(TargetPath + targetFolderName);
                        }
                        string filePath = TargetPath + targetFolderName + "\\" + fileName;


                        PdfDocument sourceDocument = new PdfDocument(pdfReader);

                        PdfDocument targetDocument = new PdfDocument(new PdfWriter(filePath));
                        PdfMerger   merger         = new PdfMerger(targetDocument);



                        allLog += "PageNo: " + page + "\n";
                        allLog += fileName + "作成しました。\n";

                        textBox4.AppendText("PageNo: " + page + "\r\n");
                        textBox4.AppendText(fileName + "作成しました。\r\n");



                        do
                        {
                            Console.WriteLine(invoiceCustRec.GetPreName(page));

                            merger.Merge(sourceDocument, page, page);
                            page++;
                            if (page > invoiceCustRec.pdfDoc.GetNumberOfPages())
                            {
                                break;
                            }
                        }while (invoiceCustRec.GetPreName(page - 1) == invoiceCustRec.GetPreName(page));


                        times++;

                        sourceDocument.Close();
                        merger.Close();
                        targetDocument.Close();
                    }
                    allLog += "処理完成しました。" + (times - 1) + "個PDFを処理しました。";


                    MessageBox.Show("処理完成しました。" + (times - 1) + "個PDFを処理しました。");
                }
                catch (Exception ex)
                {
                    allLog += ex.Message + "\n";
                }
                finally
                {
                    File.AppendAllText(".\\LOG\\" + dt.ToString("yyyy-MM-dd H-mm-ss") + "log.txt", "\r\n" + allLog);
                }
                // string result = text.ToString();
            }
        }
Пример #2
0
        public string GetPreName(int page)
        {
            SetRectangle invoiceRec = new SetRectangle(x1, y1, x2, y2, sn);

            return(PdfTextExtractor.GetTextFromPage(invoiceRec.pdfDoc.GetPage(page), invoiceRec.strategy));
        }