Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's itext 7 equivalent to pdfstamper class in itext 5

Tags:

.net

itext7

I trying to convert from iText5 to iText7. Got the package for iText7 from Nuget.

like image 320
u-xas Avatar asked Jul 12 '17 14:07

u-xas


People also ask

What is PdfStamper in iText?

PdfStamper(PdfReader reader, OutputStream os) Starts the process of adding extra content to an existing PDF document. PdfStamper(PdfReader reader, OutputStream os, char pdfVersion) Starts the process of adding extra content to an existing PDF document.

Is iText same as iTextSharp?

iTextSharp is the name of the iText 5 PDF library for .

Is iText 7 open source?

iText 7 Core is available under open source (AGPL) as well as a commercial license.

What is iText leading?

According to the PDF specification, the distance between the baseline of two lines is called the leading. In iText, the default leading is 1.5 times the size of the font. For instance: the default font size is 12 pt, hence the default leading is 18.


1 Answers

That's explained in chapter 5 of the iText 7 Jump-start tutorial. There is no PdfStamper class anymore. There is only a PdfDocument class that is used for creation of files as well as for manipulation of files.

Your question is very incomplete.

Is your code used to fill out forms? In that case, you need something like this:

PdfDocument pdf = new PdfDocument(
    new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("name").setValue("Abhishek Kumar");
pdf.close();

Or in C#:

PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue("name", out toSet);
toSet.SetValue("Abhishek Kumar");
form.FlattenFields();
pdf.Close();

Is your code used to add extra content to a document? In that case, you need something like this:

PdfDocument pdfDoc =
    new PdfDocument(new PdfReader(src), new PdfWriter(dest));
Document document = new Document(pdfDoc);
Rectangle pageSize;
PdfCanvas canvas;
int n = pdfDoc.getNumberOfPages();
for (int i = 1; i <= n; i++) {
    PdfPage page = pdfDoc.getPage(i);
    pageSize = page.getPageSize();
    canvas = new PdfCanvas(page);
    // add new content
}
pdfDoc.close();

Where it says // add new content, you can add content to the canvas.

Are you using PdfStamper for something else? In that case, you need to improve your question.

like image 198
Bruno Lowagie Avatar answered Oct 05 '22 03:10

Bruno Lowagie