-1

I found this script here on this page (ITextSharp insert text to an existing pdf)

I try a lot, I can find the text "Hello" with CTRL+F, but it is not visible... anyone have an idea ?

You can see that I have tried very often to bring color black in somehow, unfortunately without success :\

Thanks for your help =)

Code:

        using (var reader = new PdfReader(@"E:\input.pdf"))
        {
            using (var fileStream = new FileStream(@"E:\Output.pdf", FileMode.Create, FileAccess.Write))
            {
                var document = new Document(reader.GetPageSizeWithRotation(1));
                var writer = PdfWriter.GetInstance(document, fileStream);

                document.Open();

                for (var i = 1; i <= reader.NumberOfPages; i++)
                {
                    document.NewPage();

                    var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    var importedPage = writer.GetImportedPage(reader, i);

                    var contentByte = writer.DirectContent;
                    contentByte.SetColorFill(BaseColor.BLACK);
                    contentByte.BeginText();

                    contentByte.SetFontAndSize(baseFont, 12);
                    contentByte.SetColorFill(BaseColor.BLACK);
                    contentByte.SetColorStroke(BaseColor.BLACK);

                    

                    var multiLineString = "Hello,\r\nWorld!".Split('\n');

                    foreach (var line in multiLineString) {
                        if (i == 1)
                        {
                            Console.WriteLine(i);
                            contentByte.SetColorFill(BaseColor.BLACK);
                            contentByte.SetColorStroke(BaseColor.BLACK);
                            contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, line, 200, 200, 0);
                        }
                    }
                    contentByte.SetColorFill(BaseColor.BLACK);
                    contentByte.EndText();
                    contentByte.AddTemplate(importedPage, 0, 0);
                }

                document.Close();
                writer.Close();
            }
        }
Murphy
  • 11
  • 1
  • The code from which answer have you used? The accepted answer indeed has the disadvantage of first adding the text and then painting the imported original page. If that page contains some instruction to first paint the whole page in white, that would hide the new content. Instead try one of the `PdfStamper` using answers or at least first draw the imported page and thereafter the new text. – mkl May 17 '22 at 15:28
  • i added the code sorry :D im new here – Murphy May 17 '22 at 16:14
  • Ok, as mentioned in my prior comment, doing `contentByte.AddTemplate(importedPage, 0, 0)` at the end may cover anything you did before. Instead move that line up right below `var contentByte = writer.DirectContent`. – mkl May 17 '22 at 16:31

0 Answers0