public void CompactFileExample() { RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "CompactFile1_Src.tif"; string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "CompactFile1_Dest.tif"; // Create a RasterImage with 4 pages containing text showing the page number RasterImage image = null; Font f = new Font("Arial", 36, FontStyle.Bold); Bitmap btmp = new Bitmap(320, 200); Rectangle rc = new Rectangle(0, 0, btmp.Width, btmp.Height); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; const int pageCount = 4; for(int i = 1; i <= pageCount; i++) { // Create a GDI+ bitmap with the text string text = "Page " + i; Graphics g = Graphics.FromImage(btmp); g.FillRectangle(Brushes.White, rc); g.DrawString(text, f, Brushes.Black, rc, sf); g.Dispose(); RasterImage tempImage = new RasterImage(btmp); if(image == null) image = tempImage; else image.AddPage(tempImage); } sf.Dispose(); btmp.Dispose(); // Save all the pages to the file // The file should have 4 pages now: 1, 2, 3, 4 codecs.Save(image, srcFileName, RasterImageFormat.Tif, 1, 1, pageCount, 1, CodecsSavePageMode.Overwrite); image.Dispose(); // All the pages in the source TIFF file. This will create the destination file codecs.CompactFile(srcFileName, destFileName, 0); // Compact the source file again and append all the pages to the existing destination file int pagesToAdd = 0; // 0 means all pages int srcStartPage = 1; int destStartPage = 5; // insert at the end codecs.CompactFile( srcFileName, destFileName, pagesToAdd, srcStartPage, false, 0, destStartPage, false, 0, CodecsSavePageMode.Insert, false, false); // Clean up codecs.Dispose(); RasterCodecs.Shutdown(); } |