コード例 #1
0
        // ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            using (ZipFile zip = new ZipFile())
            {
                // previous example
                Stationery s          = new Stationery();
                byte[]     stationary = s.CreatePdf(s.CreateStationary());
                // reader for the src file
                PdfReader reader = new PdfReader(stationary);
                // initializations
                int pow = 1;

                do
                {
                    Rectangle pageSize = reader.GetPageSize(1);
                    Rectangle newSize  = (pow % 2) == 0
                      ? new Rectangle(pageSize.Width, pageSize.Height)
                      : new Rectangle(pageSize.Height, pageSize.Width)
                    ;
                    Rectangle unitSize = new Rectangle(pageSize.Width, pageSize.Height);
                    for (int i = 0; i < pow; i++)
                    {
                        unitSize = new Rectangle(unitSize.Height / 2, unitSize.Width);
                    }
                    int n = (int)Math.Pow(2, pow);
                    int r = (int)Math.Pow(2, pow / 2);
                    int c = n / r;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        // step 1
                        using (Document document = new Document(newSize, 0, 0, 0, 0))
                        {
                            // step 2
                            PdfWriter writer = PdfWriter.GetInstance(document, ms);
                            // step 3
                            document.Open();
                            // step 4
                            PdfContentByte  cb = writer.DirectContent;
                            PdfImportedPage page;
                            Rectangle       currentSize;
                            float           offsetX, offsetY, factor;
                            int             total = reader.NumberOfPages;

                            for (int i = 0; i < total;)
                            {
                                if (i % n == 0)
                                {
                                    document.NewPage();
                                }
                                currentSize = reader.GetPageSize(++i);
                                factor      = Math.Min(
                                    unitSize.Width / currentSize.Width,
                                    unitSize.Height / currentSize.Height
                                    );
                                offsetX = unitSize.Width * ((i % n) % c)
                                          + (unitSize.Width - (currentSize.Width * factor)) / 2f
                                ;
                                offsetY = newSize.Height
                                          - (unitSize.Height * (((i % n) / c) + 1))
                                          + (unitSize.Height - (currentSize.Height * factor)) / 2f
                                ;
                                page = writer.GetImportedPage(reader, i);
                                cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
                            }
                        }
                        zip.AddEntry(string.Format(RESULT, n), ms.ToArray());
                        ++pow;
                    }
                } while (pow < 5);
                zip.AddEntry(Utility.ResultFileName(s.ToString() + ".pdf"), stationary);
                zip.Save(stream);
            }
        }
コード例 #2
0
ファイル: NUpTool.cs プロジェクト: kuujinbo/iTextInAction2Ed
        // ---------------------------------------------------------------------------     
        public void Write(Stream stream)
        {
            using (ZipFile zip = new ZipFile())
            {
                // previous example
                Stationery s = new Stationery();
                byte[] stationary = s.CreatePdf(s.CreateStationary());
                // reader for the src file
                PdfReader reader = new PdfReader(stationary);
                // initializations
                int pow = 1;

                do
                {
                    Rectangle pageSize = reader.GetPageSize(1);
                    Rectangle newSize = (pow % 2) == 0
                      ? new Rectangle(pageSize.Width, pageSize.Height)
                      : new Rectangle(pageSize.Height, pageSize.Width)
                    ;
                    Rectangle unitSize = new Rectangle(pageSize.Width, pageSize.Height);
                    for (int i = 0; i < pow; i++)
                    {
                        unitSize = new Rectangle(unitSize.Height / 2, unitSize.Width);
                    }
                    int n = (int)Math.Pow(2, pow);
                    int r = (int)Math.Pow(2, pow / 2);
                    int c = n / r;

                    using (MemoryStream ms = new MemoryStream())
                    {
                        // step 1
                        using (Document document = new Document(newSize, 0, 0, 0, 0))
                        {
                            // step 2
                            PdfWriter writer = PdfWriter.GetInstance(document, ms);
                            // step 3
                            document.Open();
                            // step 4
                            PdfContentByte cb = writer.DirectContent;
                            PdfImportedPage page;
                            Rectangle currentSize;
                            float offsetX, offsetY, factor;
                            int total = reader.NumberOfPages;

                            for (int i = 0; i < total; )
                            {
                                if (i % n == 0)
                                {
                                    document.NewPage();
                                }
                                currentSize = reader.GetPageSize(++i);
                                factor = Math.Min(
                                    unitSize.Width / currentSize.Width,
                                    unitSize.Height / currentSize.Height
                                );
                                offsetX = unitSize.Width * ((i % n) % c)
                                  + (unitSize.Width - (currentSize.Width * factor)) / 2f
                                ;
                                offsetY = newSize.Height
                                  - (unitSize.Height * (((i % n) / c) + 1))
                                  + (unitSize.Height - (currentSize.Height * factor)) / 2f
                                ;
                                page = writer.GetImportedPage(reader, i);
                                cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
                            }
                        }
                        zip.AddEntry(string.Format(RESULT, n), ms.ToArray());
                        ++pow;
                    }
                } while (pow < 5);
                zip.AddEntry(Utility.ResultFileName(s.ToString() + ".pdf"), stationary);
                zip.Save(stream);
            }
        }