What's itext 7 equivalent to pdfstamper class in itext 5
Asked Answered
A

2

10

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

Alford answered 12/7, 2017 at 14:27 Comment(1)
Please be more specific about what functionality you're aiming to implement. iText7 was designed to be a lot more modular. So depending on your usecase there might be different classes that could serve your needs.Grocery
E
28

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.

Excitement answered 12/7, 2017 at 14:44 Comment(3)
Thanks, yes I'm trying to fill out form, fetching a template from database, filling it and then saving it in database. Though I'm getting syntax error on line Map<String, PdfFormField> fields = form.getFormFields(); saying could not find namespace 'Map<,>'Alford
I assumed that every C# developer was supposed to know the C# equivalent of a Map in Java. I'll update my answer.Excitement
Thanks for the AcroForm hint, didn't found that one yet in their horrible documentations!Reader
R
1

In itext8 the easiest way to add an image is:

  • Create a ImageData object with given image
  • Add this image into a PdfCanvas of a document

There is an example:

// bis is ByteArratInputStream
// bos ByteArrayOutputStream, also available constructor with string path, this is only to avoid file manipulation
val pdfDoc = new PdfDocument(new PdfReader(bis), new PdfWriter(bos));

val page = pdfDoc.getPage(1);
val canvas = new PdfCanvas(page);

// Create image from a source (string)
val imageData = ImageDataFactory.create("./src/main/resources/images/my_image.jpg");

// Add image into canvas at x:450, y:30 with 'inline' as false 
canvas.addImageAt(imageData , 450, 30, false);

// bos will be written with result

Raddy answered 2/7 at 10:4 Comment(3)
"In itext8 the easiest way to add an image is:" - This is not at all the question here, is it?Goggle
@Goggle Far as I know PdfStamper is old class to add images and content into PdfDocument. Create text is quite simple now but image alternative is different from old usage with PdfStamper. That's my example. Maybe I'm wrong and is not fully related with original questionRaddy
"Far as I know PdfStamper is old class to add images and content into PdfDocument." - Well, PdfStamper more exactly allows manipulating PDFs in numerous ways, and and images is just one of them, not even necessarily the main use case.Goggle

© 2022 - 2024 — McMap. All rights reserved.