Reputation: 2288
I want to add variable text and thumbnail images on specific position. Text with formatting like fontstyles,rotation etc.
iTextSharp.text.pdf.PdfReader reader = null;
Document document = new Document();
PdfWriter writer;
writer = PdfWriter.GetInstance(document,
new FileStream(temp,
FileMode.Create, FileAccess.Write));
reader = new iTextSharp.text.pdf.PdfReader(file);
size = reader.GetCropBox(1);
PdfContentByte cb = writer.DirectContent;
document.NewPage();
PdfImportedPage page = writer.GetImportedPage(reader,
pageNumber);
cb.AddTemplate(page, 0, 0);
document.Close();
This will copy first page to new document. I want to add text and images on new document. how to do that?
Upvotes: 1
Views: 1485
Reputation: 77528
Many people will stop reading your question after your first sentence:
I want to Edit existing PDF
If you read the intro of chapter 6 of my book, you'll understand that the word Edit is not the right word to use.
I did read on, and that I saw that you were using Document
and PdfWriter
to stamp new content on an existing PDF. If you do the effort of reading chapter 6 of my book, you'll understand that this is wrong. You should use PdfStamper
instead.
This is one of the examples provided online:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
float x = reader.getPageSize(i).getWidth() / 2;
float y = reader.getPageSize(i).getTop(20);
ColumnText.showTextAligned(
stamper.getOverContent(i), Element.ALIGN_CENTER,
header, x, y, 0);
}
stamper.close();
reader.close();
}
You can find these examples on the following URLs:
If you need the examples from chapter 6 ported to C#, please visit this URL: http://tinyurl.com/itextsharpIIA2C06
Update
Does PdfStamper
support:
showTextAligned()
; for text blocks use ColumnText
(caveat: read about the difference between text mode and composite mode)Upvotes: 3