2

How could I find every SOH character, which looks like a box on the PDF, and place a checkbox form field on top of it. This question was close Extract text and text rectangle coordinates from a Pdf file using itextsharp but I can not get this to work. Below is some code of what I am trying to do. It would be best also, if I could not put a form if there is already one there.

StringBuilder text = new StringBuilder();

if (File.Exists(filePath))
{
    using (PdfReader pdfReader = new PdfReader(filePath))
    using (FileStream fileOut = new FileStream(@"C:\Projects\document.pdf", FileMode.Create, FileAccess.Write))
    using (PdfStamper stamp = new PdfStamper(pdfReader, fileOut))
    {

        for (int page = 1; page <= pdfReader.NumberOfPages; page++)
        {
            ITextExtractionStrategy strategy = new PdfHelper.LocationTextExtractionStrategyEx();
            string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
            text.Append(currentText);

            int count = 0;
            foreach (var strat in ((PdfHelper.LocationTextExtractionStrategyEx)(strategy)).TextLocationInfo)
            {
                RadioCheckField checkbox = new RadioCheckField(stamp.Writer, new iTextSharp.text.Rectangle(strat.TopLeft[0], strat.BottomRight[1], (strat.TopLeft[0] + 5), (strat.BottomRight[1] - 5)), ("CheckBoxInserted" + count), "On");
                checkbox.CheckType = RadioCheckField.TYPE_SQUARE;
                stamp.AddAnnotation(checkbox.CheckField, page);
            }

            RadioCheckField checkField = new RadioCheckField(stamp.Writer, new iTextSharp.text.Rectangle(450, 690, 460, 680), "checkboxname", "On");
            checkField.CheckType = RadioCheckField.TYPE_SQUARE;
            stamp.AddAnnotation(checkField.CheckField, 1);
        }
    }
}
return text.ToString();
Community
  • 1
  • 1
d456
  • 1,147
  • 4
  • 13
  • 25
  • 1
    What is a *SOH character*? How do you want to test whether a character *looks like a box on the PDF*? (Image comparison? Unicode character codes?) – mkl Feb 27 '13 at 11:26
  • Do you have any control over the original PDFs or are you working with legacy documents? I ask because this question has been asked before and it turned out someone was trying to perform two passes on a PDF, the first pass put in a placeholder and the second pass replaced it. The much easier solution in that case was to just do it all in one pass. If that's not your case then you'll need to explain why the above code doesn't work for you, specifically what it does or does not do. Does it not find the SOH? Does the checkbox not appear? – Chris Haas Feb 27 '13 at 14:54

0 Answers0