예제 #1
0
        public bool Split(Stream os, long sizeInBytes)
        {
            if (!HasMorePages())
            {
                os.Close();
                return(false);
            }
            overSized = false;
            Document document = new Document();
            PdfCopy  copy     = new PdfSmartCopy(document, os);

            document.Open();
            bool hasPage = false;
            PdfResourceCounter counter = new PdfResourceCounter(reader.Trailer);
            long trailer = counter.GetLength(null);
            IDictionary <int, PdfObject> resources = counter.Resources;
            long length = 0;
            long page;

            while (HasMorePages())
            {
                counter   = new PdfResourceCounter(reader.GetPageN(currentPage));
                page      = counter.GetLength(resources);
                resources = counter.Resources;
                length   += page + trailer + XrefLength(resources.Count);
                if (LOGGER.IsLogging(Level.INFO))
                {
                    LOGGER.Info(String.Format("Page {0}: Comparing {1} with {2}", currentPage, length, sizeInBytes));
                    LOGGER.Info(String.Format("   page {0} trailer {1} xref {2}", page, trailer, XrefLength(resources.Count)));
                }
                if (!hasPage || length < sizeInBytes)
                {
                    hasPage = true;
                    copy.AddPage(copy.GetImportedPage(reader, currentPage));
                    length = copy.Os.Counter;
                    if (LOGGER.IsLogging(Level.INFO))
                    {
                        LOGGER.Info(String.Format("Size after adding page: {0}", length));
                    }
                    if (length > sizeInBytes)
                    {
                        overSized = true;
                    }
                    currentPage++;
                }
                else
                {
                    LOGGER.Info("Page doesn't fit");
                    break;
                }
            }
            document.Close();
            return(true);
        }
예제 #2
0
 public bool Split(Stream os, long sizeInBytes) {
     if (!HasMorePages()) {
         os.Close();
         return false;
     }
     overSized = false;
     Document document = new Document();
     PdfCopy copy = new PdfSmartCopy(document, os);
     document.Open();
     bool hasPage = false;
     PdfResourceCounter counter = new PdfResourceCounter(reader.Trailer);
     long trailer = counter.GetLength(null);
     IDictionary<int, PdfObject> resources = counter.Resources;
     long length = 0;
     long page;
     while (HasMorePages()) {
         counter = new PdfResourceCounter(reader.GetPageN(currentPage));
         page = counter.GetLength(resources);
         resources = counter.Resources;
         length += page + trailer + XrefLength(resources.Count);
         LOGGER.Info(String.Format("Page {0}: Comparing {1} with {2}", currentPage, length, sizeInBytes));
         LOGGER.Info(String.Format("   page {0} trailer {1} xref {2}", page, trailer, XrefLength(resources.Count)));
         if (!hasPage || length < sizeInBytes) {
             hasPage = true;
             copy.AddPage(copy.GetImportedPage(reader, currentPage));
             length = copy.Os.Counter;
             LOGGER.Info(String.Format("Size after adding page: {0}", length));
             if (length > sizeInBytes) overSized = true;
             currentPage++;
         } else {
             LOGGER.Info("Page doesn't fit");
             break;
         }
     }
     document.Close();
     return true;
 }