Exemplo n.º 1
0
        public void ProcessCertificateFiles(List <FileInfo> files2process, List <FileInfo> pdf2process)
        {
            // first: processing pdf files
            OnTaskProgressed(new TaskProgressedEventArgs(0, (uint)files2process.Count, 0,
                                                         new StringBuilder("Verarbeite ").Append(pdf2process.Count).Append(" Dateien...").ToString()));
            // collection of all sync tasks
            var tasksPDF = new List <Task>();

            pdf2process.ForEach(pdf =>
            {
                tasksPDF.Add(Task.Run(() =>
                {
                    PDFMetaData pdfProperties = new PDFMetaData(Properties.Settings.Default.certificateDomain, pdf.Directory.Name);

                    PDFactory.LabelonAllPages(pdf, pdfProperties);
                    // trigger event
                    OnTaskProgressed(new TaskProgressedEventArgs(1));
                }));
            });
            Task.WaitAll(tasksPDF.ToArray());

            // second: rename file
            OnTaskProgressed(new TaskProgressedEventArgs(0, (uint)files2process.Count, 0,
                                                         new StringBuilder("Benenne ").Append(files2process.Count).Append(" Dateien um...").ToString()));
            string delimiterSign = Properties.Settings.Default.certificateFileNameSeparator;
            // collection of all sync tasks
            var tasksRename = new List <Task>();

            files2process.ForEach((f) =>
            {
                tasksRename.Add(Task.Run(() =>
                {
                    StringBuilder newFileName = new StringBuilder(f.DirectoryName)
                                                .Append(@"\")
                                                .Append(f.Directory.Name)
                                                .Append(delimiterSign)
                                                .Append(f.Name);

                    // rename file by moving is (like linux :-)
                    f.MoveTo(newFileName.ToString());
                    // trigger event
                    OnTaskProgressed(new TaskProgressedEventArgs(1));
                }));
            });
            Task.WaitAll(tasksRename.ToArray());
            OnTaskProgressed(new TaskProgressedEventArgs(0, 0, 0, ""));
        }
Exemplo n.º 2
0
        public static void LabelonAllPages(FileInfo file, PDFMetaData prop)
        {
            Console.WriteLine("PDF processing {0}", file.FullName);
            //const string LABELCERTFOLDER = "certificate-folder";

            try
            {
                PdfDocument document = PdfReader.Open(file.FullName);

                // write metadata

/*
 *              Parallel.ForEach(prop.CustomProperties, (customProp) =>
 *              {
 *                  if (document.Info.Elements.ContainsKey(@"/" + customProp.Key))
 *                  {
 *                      document.Info.Elements.SetValue(@"/" + customProp.Key, new PdfString(customProp.Value));
 *                  }
 *                  else
 *                  {
 *                      document.Info.Elements.Add(@"/" + customProp.Key, new PdfString(customProp.Value));
 *                  }
 *              });
 */

                foreach (var customProp in prop.CustomProperties)
                {
                    if (document.Info.Elements.ContainsKey(@"/" + customProp.Key))
                    {
                        document.Info.Elements.SetValue(@"/" + customProp.Key, new PdfString(customProp.Value));
                    }
                    else
                    {
                        document.Info.Elements.Add(@"/" + customProp.Key, new PdfString(customProp.Value));
                    }
                }


                string inPrint  = prop.Domain + @" Certificate Number: " + prop.CertNum;
                var    PDFpages = document.Pages;

/*
 *              Parallel.ForEach<PdfPage>(PDFpages,(p) =>
 *              {
 *                  XGraphics gfx = XGraphics.FromPdfPage(p);
 *                  XFont font = new XFont("Arial", 6, XFontStyle.Regular);
 *                  XSize stringBlock = gfx.MeasureString(inPrint, font);
 *                  XRect rect = new XRect(0, 0, stringBlock.Width + 2, stringBlock.Height + 2);
 *                  XBrush brush = XBrushes.Black;
 *                  gfx.DrawRectangle(brush, rect);
 *                  gfx.DrawString(inPrint, font, XBrushes.White, new XRect(1, 1, stringBlock.Width, stringBlock.Height), XStringFormats.Center);
 *
 *              });
 */
                foreach (PdfPage p in document.Pages)
                {
                    XGraphics gfx         = XGraphics.FromPdfPage(p);
                    XFont     font        = new XFont("Arial", 6, XFontStyle.Regular);
                    XSize     stringBlock = gfx.MeasureString(inPrint, font);
                    XRect     rect        = new XRect(0, 0, stringBlock.Width + 2, stringBlock.Height + 2);
                    XBrush    brush       = XBrushes.Black;
                    gfx.DrawRectangle(brush, rect);
                    gfx.DrawString(inPrint, font, XBrushes.White, new XRect(1, 1, stringBlock.Width, stringBlock.Height), XStringFormats.Center);
                }

                document.Save(file.FullName);
            }
            catch (PdfSharp.Pdf.IO.PdfReaderException e)
            {
                Console.WriteLine("{1} has {0}", e.Message, file.FullName);
            }
        }