| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.pdf; | |
| 6: | ||
| 7: | ||
| 8: | namespace iTextSharp.tutorial.Chap01 | |
| 9: | { | |
| 10: | /// <summary> | |
| 11: | /// Split srcfile destfile1 destfile2 pagenumber | |
| 12: | /// </summary> | |
| 13: | public class Split | |
| 14: | { | |
| 15: | public Split(String[] args) | |
| 16: | { | |
| 17: | if (args.Length != 4) | |
| 18: | { | |
| 19: | Console.Error.WriteLine("This tools needs 4 parameters:\njava Split srcfile destfile1 destfile2 pagenumber"); | |
| 20: | } | |
| 21: | else | |
| 22: | { | |
| 23: | try | |
| 24: | { | |
| 25: | int pagenumber = int.Parse(args[3]); | |
| 26: | ||
| 27: | // we create a reader for a certain document | |
| 28: | PdfReader reader = new PdfReader(args[0]); | |
| 29: | // we retrieve the total number of pages | |
| 30: | int n = reader.NumberOfPages; | |
| 31: | Console.WriteLine("There are " + n + " pages in the original file."); | |
| 32: | ||
| 33: | if (pagenumber < 2 || pagenumber > n) | |
| 34: | { | |
| 35: | throw new DocumentException("You can't split this document at page " + pagenumber + "; there is no such page."); | |
| 36: | } | |
| 37: | ||
| 38: | // step 1: creation of a document-object | |
| 39: | Document document1 = new Document(reader.GetPageSizeWithRotation(1)); | |
| 40: | Document document2 = new Document(reader.GetPageSizeWithRotation(pagenumber)); | |
| 41: | // step 2: we create a writer that listens to the document | |
| 42: | PdfWriter writer1 = PdfWriter.GetInstance(document1, new FileStream(args[1], FileMode.Create)); | |
| 43: | PdfWriter writer2 = PdfWriter.GetInstance(document2, new FileStream(args[2], FileMode.Create)); | |
| 44: | // step 3: we open the document | |
| 45: | document1.Open(); | |
| 46: | PdfContentByte cb1 = writer1.DirectContent; | |
| 47: | document2.Open(); | |
| 48: | PdfContentByte cb2 = writer2.DirectContent; | |
| 49: | PdfImportedPage page; | |
| 50: | int rotation; | |
| 51: | int i = 0; | |
| 52: | // step 4: we add content | |
| 53: | while (i < pagenumber - 1) | |
| 54: | { | |
| 55: | i++; | |
| 56: | document1.SetPageSize(reader.GetPageSizeWithRotation(i)); | |
| 57: | document1.NewPage(); | |
| 58: | page = writer1.GetImportedPage(reader, i); | |
| 59: | rotation = reader.GetPageRotation(i); | |
| 60: | if (rotation == 90 || rotation == 270) | |
| 61: | { | |
| 62: | cb1.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); | |
| 63: | } | |
| 64: | else | |
| 65: | { | |
| 66: | cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); | |
| 67: | } | |
| 68: | } | |
| 69: | while (i < n) | |
| 70: | { | |
| 71: | i++; | |
| 72: | document2.SetPageSize(reader.GetPageSizeWithRotation(i)); | |
| 73: | document2.NewPage(); | |
| 74: | page = writer2.GetImportedPage(reader, i); | |
| 75: | rotation = reader.GetPageRotation(i); | |
| 76: | if (rotation == 90 || rotation == 270) | |
| 77: | { | |
| 78: | cb2.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); | |
| 79: | } | |
| 80: | else | |
| 81: | { | |
| 82: | cb2.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); | |
| 83: | } | |
| 84: | Console.WriteLine("Processed page " + i); | |
| 85: | } | |
| 86: | // step 5: we close the document | |
| 87: | document1.Close(); | |
| 88: | document2.Close(); | |
| 89: | } | |
| 90: | catch(Exception e) | |
| 91: | { | |
| 92: | Console.Error.WriteLine(e.Message); | |
| 93: | Console.Error.WriteLine(e.StackTrace); | |
| 94: | } | |
| 95: | } | |
| 96: | ||
| 97: | } | |
| 98: | } | |
| 99: | } |