Recompress an existing PDF using color detection in C#

This example shows how to recompress an existing PDF document containing various images from a scanner.


C#
//We assume that GdPicture has been correctly installed and unlocked.
GdPicturePDF oGdPicturePDF = new GdPicturePDF();
oGdPicturePDF.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2);
oGdPicturePDF.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG);
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
GdPictureStatus status = oGdPicturePDF.LoadFromFile("c:\\input.pdf", true);
if (status == GdPictureStatus.OK)
{
int PageCount = oGdPicturePDF.GetPageCount();
for (int Page = 1; Page <= PageCount; Page++)
{
oGdPicturePDF.SelectPage(Page);
for (int Image = 1; Image <= oGdPicturePDF.GetPageImageCount(); Image++)
{
int imageID = oGdPicturePDF.ExtractPageImage(Image);
if (oGdPicturePDF.GetStat() == GdPictureStatus.OK)
{
oGdPictureImaging.ColorDetection(imageID, true, true, true);
if (oGdPicturePDF.ReplaceImage(oGdPicturePDF.GetPageImageResName(Image - 1), imageID, false) != GdPictureStatus.OK)
MessageBox.Show("Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
oGdPictureImaging.ReleaseGdPictureImage(imageID);
}
else
MessageBox.Show("Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
status = oGdPicturePDF.SaveToFile("c:\\output.pdf", true);
if (status == GdPictureStatus.OK)
MessageBox.Show("Done!", "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("The PDF file can't be saved. Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
oGdPicturePDF.CloseDocument();
oGdPictureImaging.Dispose();
}
else
MessageBox.Show("The PDF file can't be loaded. Error: " + status.ToString(), "Color Detection Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
oGdPicturePDF.Dispose();

This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.