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();
}