Пример #1
0
        private static string[] ExtractFiles(string sourceOddFileName, string sourceEvenFileName)
        {
            var oddFile      = new PDFFile(sourceOddFileName);
            var oddPageCount = oddFile.GetPageCount();

            var evenFile      = new PDFFile(sourceEvenFileName);
            var evenPageCount = evenFile.GetPageCount();

            var filenames = new string[oddPageCount + evenPageCount];

            // Extract each page to an individual PDF file
            for (var i = 1; i <= oddPageCount; i++)
            {
                var pageNum = (i * 2) - 1;
                filenames[pageNum - 1] = $"temp-page-{pageNum}.pdf";
                oddFile.ExtractPages(i, i, filenames[pageNum - 1]);
            }
            for (var i = 1; i <= evenPageCount; i++)
            {
                var pageNum = (i * 2);
                filenames[pageNum - 1] = $"temp-page-{pageNum}.pdf";
                evenFile.ExtractPages(i, i, filenames[pageNum - 1]);
            }

            return(filenames);
        }
        private bool Split(string srcFileName)
        {
            // Open a document from disk

            if (!File.Exists(srcFileName))
            {
                SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Load error: {0} no exist", srcFileName), TraceEventType.Error));
                return(false);
            }

            string      dstFileName;
            PDFDocument pdfDoc;

            try
            {
                pdfDoc = new PDFDocument(srcFileName);
                SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Load success: {0} has {1} pages", srcFileName, pdfDoc.Pages.Count), TraceEventType.Information));
            }
            catch (Exception ex)
            {
                SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Load pdf error: {0}", ex.Message), TraceEventType.Error));

                dstFileName = Path.Combine(_directoryData.FolderError, Path.GetFileName(srcFileName));

                FileCopy(srcFileName, dstFileName);

                return(false);
            }

            int allPagesCount = pdfDoc.Pages.Count;

            pdfDoc.Resolution = _barcodeOptions.ImageResolution;

            Dictionary <int, BarcodeData[]> findedData = new Dictionary <int, BarcodeData[]>();
            Dictionary <int, BarcodeData[]> errorData  = new Dictionary <int, BarcodeData[]>();

            RasterCodecs _rasterCodecs = new RasterCodecs();

            _rasterCodecs.Options.Pdf.InitialPath = AppDomain.CurrentDomain.BaseDirectory;

            for (int i = 1; i <= pdfDoc.Pages.Count; i++)
            {
                try
                {
                    BarcodeData[] detectBarcodes = ReadBarcode(pdfDoc.GetPageImage(_rasterCodecs, i));
                    BarcodeData[] barcodes       = new BarcodeData[] { };

                    if (detectBarcodes.Count() > 0)
                    {
                        // check barcode value contain "-";

                        for (int k = 0; k < detectBarcodes.Count(); k++)
                        {
                            if (detectBarcodes[k].Value.IndexOf("-") > 0)
                            {
                                barcodes = barcodes.Concat(new BarcodeData[] { detectBarcodes[k] }).ToArray();
                            }
                        }

                        if (barcodes.Length == 1)
                        {
                            findedData.Add(i, barcodes);
                        }
                        else if (barcodes.Length > 1)
                        {
                            errorData.Add(i, barcodes);                             // this pdf is error file.
                        }
                    }
                }
                catch (Exception ex)
                {
                    SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Detect barcodes error in pdf: {0}, page: {1}, {2}", srcFileName, i, ex.Message), TraceEventType.Error));
                }
            }

            pdfDoc.Dispose();

            if (errorData.Count > 0)
            {
                dstFileName = Path.Combine(_directoryData.FolderError, Path.GetFileName(srcFileName));

                string errPages = "";

                foreach (var fData in findedData)
                {
                    errPages += fData.Key + ",";
                }
                SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Detect two or more barcodes in pdf: {0}, page(s): {1}", srcFileName, errPages), TraceEventType.Error));
            }
            else if (findedData.Count <= 0)
            {
                dstFileName = Path.Combine(_directoryData.FolderError, Path.GetFileName(srcFileName));
                SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("No detect barcodes: {0}", srcFileName), TraceEventType.Error));
            }
            else
            {
                bool errorFlag = false;

                if (findedData.Count > 0)
                {
                    findedData.Add(allPagesCount + 1, null);
                }

                PDFFile pdffile = new PDFFile(srcFileName);

                int         pageStart = -1, pageEnd;
                BarcodeData prevBarcodeData = null;

                foreach (var fData in findedData)
                {
                    if (pageStart == -1)
                    {
                        pageStart       = fData.Key;
                        prevBarcodeData = fData.Value[0];
                        continue;
                    }

                    string splitName = "";

                    pageEnd = fData.Key - 1;

                    try
                    {
                        splitName = GetNewPdfFileName(srcFileName, prevBarcodeData, pageStart, pageEnd);

                        pdffile.ExtractPages(pageStart, pageEnd, splitName);
                        SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Extract pdf success: {0}, {1} ~ {2} pages", splitName, pageStart, pageEnd), TraceEventType.Debug));
                    }
                    catch (Exception ex)
                    {
                        SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("PDF split error, {0} : {1} ~ {2} pages, {3}", splitName, pageStart, pageEnd, ex.Message), TraceEventType.Error));
                        errorFlag = true;
                        break;
                    }

                    if (fData.Value == null)
                    {
                        break;
                    }

                    pageStart       = fData.Key;
                    prevBarcodeData = fData.Value[0];
                }

                if (!errorFlag)
                {
                    dstFileName = Path.Combine(_directoryData.FolderSuccess, Path.GetFileName(srcFileName));
                    SendMsgEvent(this, new PDFSplitterEventArgs(_directoryData.ID, 2, string.Format("Split success: {0}", srcFileName), TraceEventType.Information));
                }
                else
                {
                    dstFileName = Path.Combine(_directoryData.FolderError, Path.GetFileName(srcFileName));
                }
            }

            bool ret = FileCopy(srcFileName, dstFileName);

            File.Delete(srcFileName);
            return(ret);
        }
Пример #3
0
        private void OptionsNextPage()
        {
            OperationItem operationItem = GetCurrentOperation();
            string        viewFileName;

            if (operationItem.DestinationMustExist)
            {
                viewFileName = _destinationDocument.FileName;
            }
            else
            {
                viewFileName = _destinationFileName != null ? _destinationFileName : _sourceDocument.FileName;
            }

            if (operationItem.DestinationMustExist)
            {
                _optionsConvertOptionsControl.UpdateDocument(_destinationDocument);
            }
            else
            {
                _optionsConvertOptionsControl.UpdateDocument(_sourceDocument);
            }

            try
            {
                using (WaitCursor wait = new WaitCursor())
                {
                    switch (operationItem.Operation)
                    {
                    case Operation.UpdateProperties:
                        _sourceDocument.SetDocumentProperties(_destinationFileName);
                        break;

                    case Operation.ConvertToPDFA:
                        _sourceDocument.ConvertToPDFA(_destinationFileName);
                        break;

                    case Operation.Linearize:
                        _sourceDocument.Linearize(_destinationFileName);
                        break;

                    case Operation.Convert:
                        _sourceDocument.Convert(_firstPageNumber, _lastPageNumber, _destinationFileName);
                        break;

                    case Operation.Merge:
                        string[] sourceFiles = new string[_sourceFilesListBox.Items.Count];
                        for (int i = 0; i < _sourceFilesListBox.Items.Count; i++)
                        {
                            sourceFiles[i] = _sourceFilesListBox.Items[i].ToString();
                        }

                        _sourceDocument.MergeWith(sourceFiles, _destinationFileName);
                        break;

                    case Operation.DeletePages:
                        _sourceDocument.DeletePages(_firstPageNumber, _lastPageNumber, _destinationFileName);
                        break;

                    case Operation.ExtractPages:
                        _sourceDocument.ExtractPages(_firstPageNumber, _lastPageNumber, _destinationFileName);
                        break;

                    case Operation.InsertPages:
                        _destinationDocument.InsertPagesFrom(_insertPageNumber, _sourceDocument, _firstPageNumber, _lastPageNumber);
                        break;

                    case Operation.Distill:
                        _sourceDocument.Distill(_distillerOptions, _destinationFileName);
                        break;

                    case Operation.InitialView:
                        _sourceDocument.SetInitialView(_destinationFileName);
                        break;

                    case Operation.Optimizer:
                        _sourceDocument.Optimize(_destinationFileName);
                        break;

                    case Operation.DigitalSignature:
                        if (!HasDigitalSignatureSupport(this))
                        {
                            return;
                        }

                        _sourceDocument.SignDocument(_destinationFileNameTextBox.Text, _signatureFileNameTextBox.Text, _filePasswordTextBox.Text);
                        break;

                    default:
                        break;
                    }
                }

                System.Diagnostics.Process.Start(viewFileName);
            }
            catch (Exception ex)
            {
                Messager.ShowError(this, ex);
            }
        }