PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDF document converted to grayscale when splitted
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2082
Page 1 of 1

Author:  datamex [ Thu Jul 12, 2012 9:14 am ]
Post subject:  PDF document converted to grayscale when splitted

Hello,

I'm using some very basic code to split up a PDF document into multiple documents. For some reason, the resulting documents are saved in grayscale instead of color. Why could that be and how can I make sure it doesn't happen? The color should match the color of the original document, so if that contains color; the results should contain color as well.

Code:
    public Stream[] SplitDocument(Stream fileStream, int[] splitOnPageNumbers)
    {
      PdfDocument inputDocument = PdfReader.Open(fileStream, PdfDocumentOpenMode.Import);

      List<Stream> result = new List<Stream>();
      PdfDocument outputDocument = null;

      for (int page = 0; page < inputDocument.PageCount; page++)
      {
        // when there is no active document; create one
        if (outputDocument == null)
        {
          outputDocument = new PdfDocument();
          outputDocument.Version = inputDocument.Version;
          outputDocument.Info.Title = inputDocument.Info.Title;
          outputDocument.Info.Creator = inputDocument.Info.Creator;
        }

        // add the page and save to stream
        outputDocument.AddPage(inputDocument.Pages[page]);

        // when this page was indicated as a split number; save the document
        // also save by default when it's the last page
        // add 1 to the page number because that's zero based, and the splitOnPageNumbers var is one based
        if (splitOnPageNumbers.Contains(page + 1) || page + 1 == inputDocument.PageCount)
        {
          Stream outputStream = new MemoryStream();
          outputDocument.Save(outputStream, false);       // don't close stream, it will be unusable
          result.Add(outputStream);

          outputDocument = null;                          // reset to null to spawn a new document
        }
      }

      return result.ToArray();
    }

Author:  Thomas Hoevel [ Thu Jul 12, 2012 2:43 pm ]
Post subject:  Re: PDF document converted to grayscale when splitted

Hi!
datamex wrote:
Why could that be and how can I make sure it doesn't happen?
I don't know.
The problem could be related to your PDF file.
It would be good to have the PDF file and a working Visual Studio project to replicate the problem.

Author:  datamex [ Fri Jul 13, 2012 7:41 am ]
Post subject:  Re: PDF document converted to grayscale when splitted

Never mind, it's my own mistake. The document is processed by a couple of components actually and it's not PDFSharp causing this problem. Sorry to have bothered you!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/