예제 #1
0
        private static void PDFContentSearch(string[] files, Dictionary <string, string> dictionary, string source, string target, string projectPath, string backup)
        {
            foreach (String file in files)
            {
                Dictionary <String, String> dic = new Dictionary <String, string>();
                String newFilePath      = file.Replace(projectPath, "");
                String newDirectoryName = System.IO.Path.GetDirectoryName(newFilePath);
                if (!Directory.Exists(backup + newDirectoryName) && !newDirectoryName.Equals("\\"))
                {
                    Directory.CreateDirectory(backup + newDirectoryName);
                }
                String targetPath = backup + newFilePath;
                try
                {
                    System.IO.File.Copy(file, targetPath, true);
                    iText.Kernel.Pdf.PdfReader pdfReader = new PdfReader(file);
                    PdfWriter   pdfWriter   = new PdfWriter(targetPath);
                    PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter);
                    PdfAcroForm pdfAcroForm = PdfAcroForm.GetAcroForm(pdfDocument, false);
                    IDictionary <String, PdfFormField> field = pdfAcroForm.GetFormFields();
                    foreach (String key in pdfAcroForm.GetFormFields().Keys)
                    {
                        if (key.Contains("DOB") || key.Contains("Date"))
                        {
                            if (!key.EndsWith(target))
                            {
                                dic.Add(key, key + target);
                            }
                        }
                    }
                    foreach (String field1 in dic.Keys)
                    {
                        pdfAcroForm.RenameField(field1, dic[field1]);
                    }

                    pdfDocument.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occurred :" + ex);
                    continue;
                }
            }
        }
예제 #2
0
        public void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form   = PdfAcroForm.GetAcroForm(pdfDoc, true);

            form.RenameField("personal.loginname", "login");

            pdfDoc.Close();

            pdfDoc = new PdfDocument(new PdfReader(dest));
            form   = PdfAcroForm.GetAcroForm(pdfDoc, true);
            IDictionary <String, PdfFormField> fields = form.GetFormFields();

            // See the renamed field in the console
            foreach (String name in fields.Keys)
            {
                Console.WriteLine(name);
            }

            pdfDoc.Close();
        }
        public virtual void CreatePdf(String dest)
        {
            PdfDocument       pdfDocument = new PdfDocument(new PdfWriter(dest));
            PdfPageFormCopier formCopier  = new PdfPageFormCopier();

            using (StreamReader sr = File.OpenText(DATA))
            {
                String line;
                bool   headerLine = true;
                int    i          = 1;
                while ((line = sr.ReadLine()) != null)
                {
                    if (headerLine)
                    {
                        headerLine = false;
                        continue;
                    }

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    PdfDocument           sourcePdfDocument = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos));
                    //Rename fields
                    i++;
                    PdfAcroForm form = PdfAcroForm.GetAcroForm(sourcePdfDocument, true);
                    form.RenameField("name", "name_" + i);
                    form.RenameField("abbr", "abbr_" + i);
                    form.RenameField("capital", "capital_" + i);
                    form.RenameField("city", "city_" + i);
                    form.RenameField("population", "population_" + i);
                    form.RenameField("surface", "surface_" + i);
                    form.RenameField("timezone1", "timezone1_" + i);
                    form.RenameField("timezone2", "timezone2_" + i);
                    form.RenameField("dst", "dst_" + i);
                    //Fill out fields
                    StringTokenizer tokenizer = new StringTokenizer(line, ";");
                    IDictionary <String, PdfFormField> fields = form.GetFormFields();
                    PdfFormField toSet;
                    fields.TryGetValue("name_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("abbr_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("capital_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("city_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("population_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("surface_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("timezone1_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("timezone2_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    fields.TryGetValue("dst_" + i, out toSet);
                    toSet.SetValue(tokenizer.NextToken());
                    sourcePdfDocument.Close();
                    sourcePdfDocument = new PdfDocument(new PdfReader(new MemoryStream(baos.ToArray())));
                    //Copy pages
                    sourcePdfDocument.CopyPagesTo(1, sourcePdfDocument.GetNumberOfPages(), pdfDocument, formCopier);
                    sourcePdfDocument.Close();
                }
            }

            pdfDocument.Close();
        }