cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with IWDResource and iText

Former Member
0 Kudos

Hi,

I generate a pdf with iText. Now I want to add on my pdf the pagenumber like "Page 1 of 6". My plan was to create the pdf, and with the iText-Class pdfstamper I want to add the pagenumber:


try {
	PdfReader reader = new PdfReader(baos.toByteArray());
	int n = reader.getNumberOfPages();
	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(wdContext.currentContextElement().getPdfResource().toString()));
	PdfContentByte page;
	Rectangle rect;
	BaseFont bf = BaseFont.createFont();
	for (int i = 1; i < n + 1; i++) {
		page = stamper.getOverContent(i);
		rect = reader.getPageSizeWithRotation(i);
		page.beginText();
		page.setFontAndSize(bf, 12);
		page.showTextAligned(Element.ALIGN_CENTER, "Page " + i + " of " + n, rect.getRight(300), rect.getTop(100), 0);
					page.endText();
	}
	stamper.close();
} catch (IOException e) {
	e.printStackTrace();
	messageManager.reportSuccess("IOException: " + e);
} catch (DocumentException e) {
	e.printStackTrace();
	messageManager.reportSuccess("DocumentException: " + e);
}

But there is a problem with the resource. This line is the problem: FileOutputStream(wdContext.currentContextElement().getPdfResource().toString())

He don't find the pdf, because it's created dynamicly and has no path like "c:/itext/test.pdf"

Do you have an idea to solve the problem or is there another option to create the pagenumbers?

Accepted Solutions (1)

Accepted Solutions (1)

kai_mattern2
Explorer
0 Kudos

I see.

But what do you try to do? the reader is the ByteArray PDF you created and you want to modifiy. And then you try to write the modified pdf to disk? Whats the content of getPDFResource? The file path to the file you want to write?

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(wdContext.currentContextElement().getPdfResource().toString()));

Try this one please just to see that your code is working. Replace the FileOutputstream with a ByteArrayOutputStream (aos in the example).

And ad this to your code. A new window should open with your stamped PDF.

byte[] pdfContent = aos.toByteArray();

IWDCachedWebResource pdfResource = WDWebResource.getWebResource(pdfContent, WDWebResourceType.PDF);

IWDWindow window = wdComponentAPI.getWindowManager().createExternalWindow(pdfResource.getURL(), "NewPDF", true);

window.removeWindowFeature(WDWindowFeature.MENU_BAR);

window.removeWindowFeature(WDWindowFeature.TOOL_BAR);

window.removeWindowFeature(WDWindowFeature.STATUS_BAR);

window.removeWindowFeature(WDWindowFeature.ADDRESS_BAR);

window.open();

Edited by: Kai Mattern on Oct 26, 2010 2:15 PM

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

thank you very much for your information.

This line solves the problem:

PdfStamper stamper = new PdfStamper(reader, baos);

But I don't need the window. I already have a popup-view where you can open the pdf. I just had to change the source-path in the context to open the stamped pdf.

Former Member
0 Kudos

But your solution only shows the current page, but not the total pagenumbers. You have someting like "Page 4" but I want something like "Page 4 of 6"

kai_mattern2
Explorer
0 Kudos

Yes very easily without IWDResource.

yust add:

p = new Paragraph("Seite " + document.getPageNumber(), fontBlack);

(you have to add the Paragraph to the document of course, and the font has to exist)

In my documents I do this in the

public void onEndPage(PdfWriter writer, Document document)

event.

Edited by: Kai Mattern on Oct 26, 2010 1:41 PM