Example #1
0
        public virtual void CreateHiddenByFieldNameTest()
        {
            String      fileName = "createHiddenByFieldNameTest.pdf";
            PdfDocument document = CreateDocument(new PdfWriter(destinationFolder + fileName), false);

            document.GetPage(1).SetAdditionalAction(PdfName.O, PdfAction.CreateHide("name", true));
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + fileName, sourceFolder
                                                                             + "cmp_" + fileName, destinationFolder, "diff_"));
        }
Example #2
0
        public virtual void CreateHiddenAnnotationTest()
        {
            String        fileName   = "createHiddenAnnotationTest.pdf";
            PdfDocument   document   = CreateDocument(new PdfWriter(destinationFolder + fileName), false);
            PdfAnnotation annotation = new PdfLineAnnotation(new Rectangle(10, 10, 200, 200), new float[] { 50, 750, 50
                                                                                                            , 750 });

            document.GetPage(1).SetAdditionalAction(PdfName.O, PdfAction.CreateHide(annotation, true));
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + fileName, sourceFolder
                                                                             + "cmp_" + fileName, destinationFolder, "diff_"));
        }
Example #3
0
        public Task WriteAnnotatedPdfAsync(string pdfDocumentPath, IEnumerable <IAnnotation> annotations, string filePath)
        {
            using (var reader = new PdfReader(pdfDocumentPath))
                using (var outFile = File.Open(filePath, FileMode.Create))
                    using (var writer = new PdfWriter(outFile))
                        using (var doc = new PdfDocument(reader, writer))
                        {
                            var acroForm = PdfAcroForm.GetAcroForm(doc, true);
                            foreach (var ann in annotations)
                            {
                                var targets = ann.SelectedTargets;
                                if (targets == null || targets.Count < 1)
                                {
                                    targets = ann.Subject.Appearances ?? throw new ArgumentException($"The subject word {ann.Subject.Text} for a given annotation has no appearances.");
                                }
                                foreach (var trg in targets)
                                {
                                    var page = doc.GetPage(trg.Parent.Index + 1);

                                    var font       = PdfFontFactory.CreateRegisteredFont(iText.IO.Font.Constants.StandardFonts.HELVETICA);
                                    var coord      = trg.GetPdfCoords();
                                    var buttonRect = new Rectangle(coord.Llx, coord.Lly, coord.width, coord.height);
                                    var txRect     = GetAnnotationRect(buttonRect, font, ann, trg);

                                    var textFieldName = Guid.NewGuid().ToString("n");
                                    var textField     = PdfFormField.CreateText(doc, txRect, textFieldName);
                                    textField.SetValue(ann.Content, font, FontSize);
                                    textField.SetColor(ColorConstants.DARK_GRAY);
                                    textField.SetBackgroundColor(ColorConstants.LIGHT_GRAY);
                                    textField.SetReadOnly(true);
                                    textField.SetMultiline(true);
                                    textField.SetVisibility(PdfFormField.HIDDEN);
                                    textField.SetBorderColor(ColorConstants.LIGHT_GRAY);
                                    textField.SetFieldFlags(4097);

                                    acroForm.AddField(textField, page);

                                    var enter = PdfAction.CreateHide(textFieldName, false);
                                    var exit  = PdfAction.CreateHide(textFieldName, true);

                                    var btn = PdfFormField.CreatePushButton(doc, buttonRect, Guid.NewGuid().ToString("n"),
                                                                            string.Empty);
                                    btn.SetBackgroundColor(null);
                                    btn.SetBorderWidth(0);
                                    btn.SetAdditionalAction(PdfName.E, enter);
                                    btn.SetAdditionalAction(PdfName.X, exit);
                                    acroForm.AddField(btn, page);

                                    var underline = PdfTextMarkupAnnotation.CreateUnderline(new Rectangle(buttonRect.GetX(), buttonRect.GetY() - 2, buttonRect.GetWidth(), 3),
                                                                                            new float[]
                                    {
                                        buttonRect.GetX() + buttonRect.GetWidth(), buttonRect.GetY() + buttonRect.GetHeight(),
                                        buttonRect.GetX(), buttonRect.GetY() + buttonRect.GetHeight(),
                                        buttonRect.GetX() + buttonRect.GetWidth(), buttonRect.GetY() - 2,
                                        buttonRect.GetX(), buttonRect.GetY() - 2,
                                    });
                                    underline.SetColor(ColorConstants.YELLOW);
                                    page.AddAnnotation(underline);
                                }
                            }
                        }
            return(Task.CompletedTask);
        }