ITextSharp edits existing pdf

I want to add text to an existing PDF file using iTextSharp, I found different ways, but in all of them authors and readers are separate PDF files. I want me to be able to open a PDF and then write different things in different positions. I have this code right now, but it creates a new file.

using (FileStream stream1 = File.Open(path, FileMode.OpenOrCreate))
      {
      BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      PdfReader reader = new PdfReader("C:\\26178DATA\\pdf\\holding.pdf");
      var pageSize = reader.GetPageSize(1);
      PdfStamper stamper = new PdfStamper(reader, stream1);
      iTextSharp.text.Font tmpFont = new iTextSharp.text.Font(bf, fontSize);
      PdfContentByte canvas = stamper.GetOverContent(1);
      Phrase ph = new Phrase(words[1], tmpFont);
      ph.Font = tmpFont;
      canvas.SetFontAndSize(bf, fontSize);
      ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, ph, iTextSharp.text.Utilities.MillimetersToPoints(x * 10), pageSize.GetTop(iTextSharp.text.Utilities.MillimetersToPoints(y * 10)), 0);
      stamper.Close();
            }
+5
source share
1 answer

You want to add text to an existing PDF file using iTextSharp, find different ways, but in all of them authors and readers are separate PDF files.

, iText (Sharp) PDF PdfStamper, PDF, iText . , , ; . - , - , .

, , . , PdfReader :

PdfReader reader = new PdfReader(path);
using (FileStream stream1 = File.Open(path, FileMode.OpenOrCreate))
{
    BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    ...

, MemoryStream, FileStream, , , .

+4

All Articles