I'd like to offer a Starbucks card to whomever can help me understand completely this code. It's important to me. Thank you for your time!
I started to read the wonderful book of Mr. Lowagie (iText in Action, Second Edition). It is really well written and informative. Due to the high pressure of my employer, I am pushed to skip ahead to chapter 6. Understanding the n-Up code example is rather important for me right now, and being a new programmer, I will try to comment the code below with my questions.
If you could, when you can, spare some time and help me walk through it by looking at my questions below, it would be great. I appreciate all the helpful comments.
public void manipulatePdf(String src, String dest, int pow)
Q: Here the 'pow' variable indicates, for example, that in a pow of 3, the resulting n-up would be (2x2x2) 8. The resulting pdf would have 8 copies of the 'src' PDF. Is this correct?
A:
throws IOException, DocumentException
{
PdfReader reader = new PdfReader(src);
Rectangle pageSize = reader.getPageSize(1);
Rectangle newSize = (pow % 2) == 0 ?
new Rectangle( pageSize.getWidth(), pageSize.getHeight()) : new Rectangle( pageSize.getHeight(), pageSize.getWidth());
Q: Above I am unsure what is going on. It appears, to me, that if the remainder of the modulus operation is zero then the new rectangle will be created in Portrait orientation and vice-versa. Is this correct? If so, may I ask why?
A:
Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight());
for (int i = 0; i < pow; i++)
{
unitSize = new Rectangle(
unitSize.getHeight() / 2, unitSize.getWidth());
}
Q: Above, in the 'for' loop, we are, I believe, preparing the unitsize for each of the pdf that will be squeezed onto the final pdf surface. I do not understand why the getWidth() is not divided by 2 as well?
A:
int n = (int)Math.pow(2, pow);
int r = (int)Math.pow(2, pow / 2);
int c = n / r;
Q: 'n' is the number of pdfPages that will be n'uped unto the final pdf surface. So far so good, right?
'r' may be the number of rows and 'c' the number of columns?
So, assuming a 'pow' of 3..
n = 8 which means 8 up of the pdf when you'll open up the final pdf?
r = 2 which means there'll be be two rows.
c = 4 which means there'll be four columns.
A:
Document document = new Document(newSize, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(String.format(dest, n)));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page;
Rectangle currentSize;
float offsetX, offsetY, factor;
int total = reader.getNumberOfPages();
for (int i = 0; i < total; )
{
if (i % n == 0)
{
document.newPage();
}
Q: The above 'for' loop adds a new page to the final pdf each time the predetermined nup number has been reached in the current working page. So if we have n = 8, then new page will be called at i = 8, 16, 24, etc. Right?
A:
currentSize = reader.getPageSize(++i);
Q: This line accounts for situations when the source pdf has many pages and not all pages share the same pdf page size. Correct?
A:
factor = Math.min(unitSize.getWidth() / currentSize.getWidth(),
unitSize.getHeight() / currentSize.getHeight());
Q: The min function returns the smallest number. Why is unitSize divided by currentSize? This is where my comprehension breaks down.
A:
offsetX = unitSize.getWidth() * ((i % n) % c)
+(unitSize.getWidth()
- (currentSize.getWidth() * factor))/2f;
Q: Okay, the offsetX and offsetY are used to position the pdf page unto the larger final pdf surface. That I understand. Could someone explain the calculation to a non programmer, in English? This is a case of knowing what each math operation does but not understanding the big picture.
A:
offsetY = newSize.getHeight()
- (unitSize.getHeight() * (((i % n) / c) + 1))
+ (unitSize.getHeight()
- (currentSize.getHeight() * factor))/2f;
page = writer.getImportedPage(reader, i);
cb.addTemplate(page,factor, 0, 0, factor, offsetX, offsetY);
}
document.close();
}
I hope this question can help many others, who are new to iText, and may need some detailed code walkthrough for this code.
I hope, as well, that negative and generally unhelpful comments will not be posted.
Thank you all for you time and help!