3

I have pdf files exported as legal format and want to convert them to letter format (basically shrink them), each file may have 1 to 3 pages, below is the code I tried but I have these problems:

  • the page size is reduced which is good, but I can't use the margin properties to put the page at the correct borders of the container (the page I kind of shrinked but drawn somewhere at the bottom of the resulted pdf file)

  • I couldn't increment the number of pages so the code draws both pages, one on top of the other.

Here's the code

PdfImportedPage page;

PdfReader reader = new PdfReader(@"C:\pdf\legalFormat.pdf");
Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\pdf\letterFormat.PDF", FileMode.Create));
doc.Open();

PdfContentByte cb = writer.DirectContent;
for (int i = 1 ; i < reader.NumberOfPages + 1; i++){
    page = writer.GetImportedPage(reader, i); // i is the number of page
    float Scale = 0.67f;
    cb.AddTemplate(page, Scale, 0, 0, Scale, 0, 0);
}
doc.Close();
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
joe
  • 546
  • 1
  • 6
  • 20
  • Have you considered using a `PdfStamper` and merely manipulating the media boxes of the pages and (if rescaling proves necessary) manipulating the default user space unit, cf. [this answer](http://stackoverflow.com/a/12813721/1729265) and [this answer](http://stackoverflow.com/a/13213503/1729265)? – mkl May 08 '13 at 16:02
  • 1
    I think i figured this out, is it possible to answer my own question so i can post the answer in case someone is in the same situation? – joe May 08 '13 at 16:04
  • Yes, you should answer your own question and accept it. Otherwise, this question will keep on turning up when I search for unanswered iText questions ;-) – Bruno Lowagie May 09 '13 at 07:04

1 Answers1

2

Problem solved:

in a main proc run this for test.

            string original = args[0];

            string inPDF = original;
            string outPDF = Directory.GetDirectoryRoot(original) + "temp.pdf";
            PdfReader pdfr = new PdfReader(inPDF);

            Document doc = new Document(PageSize.LETTER);
            Document.Compress = true;

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(outPDF, FileMode.Create));
            doc.Open();

            PdfContentByte cb = writer.DirectContent;

            PdfImportedPage page;

            for (int i = 1; i < pdfr.NumberOfPages + 1; i++)
            {
                page = writer.GetImportedPage(pdfr, i);
                cb.AddTemplate(page, PageSize.LETTER.Width / pdfr.GetPageSize(i).Width, 0, 0, PageSize.LETTER.Height / pdfr.GetPageSize(i).Height, 0, 0);
                doc.NewPage();
            }

            doc.Close();

            //just renaming, conversion / resize process ends at doc.close() above
            File.Delete(original);
            File.Copy(outPDF, original);
joe
  • 546
  • 1
  • 6
  • 20
  • The disadvantage of using `PdfWriter` for such tasks is that all interactive features are lost. – mkl May 08 '13 at 17:22
  • yes i knew it from reading the doc, no interaction is needed in my case, hope this will serve as a base to someone else – joe May 08 '13 at 17:35