private static void ConvertPDF()
{
string pdfSource = @"C:\test.pdf";
string destination = @"C:\test-gray.pdf";
PDFDoc pdf = new PDFDoc(pdfSource);
PDFDoc newPDF = new PDFDoc();
ElementWriter writer = new ElementWriter();
ElementReader reader = new ElementReader();
_imgCounter = 0;
for (int i = 1; i <= pdf.GetPageCount(); ++i)
{
Page page = pdf.GetPage(i);
reader.Begin(page);
Page newPage = newPDF.PageCreate(new Rect(0, 0, page.GetPageWidth(), page.GetPageHeight()));
writer.Begin(newPage);
ConvertImagesToGrayscale(reader, writer, newPDF);
writer.End();
reader.End();
newPDF.PagePushBack(newPage);
}
writer.Dispose();
reader.Dispose();
newPDF.Save(destination, pdftron.SDF.SDFDoc.SaveOptions.e_linearized |
pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
newPDF.Close();
newPDF.Dispose();
pdf.Close();
pdf.Dispose();
}
private static int _imgCounter = 0;
private static void ConvertImagesToGrayscale(ElementReader reader, ElementWriter writer, PDFDoc pdf)
{
Element element;
while ((element = reader.Next()) != null) // Read page contents
{
bool written = false;
switch (element.GetType())
{
case Element.Type.e_image:
case Element.Type.e_inline_image:
_imgCounter++;
Bitmap bmp = MakeGrayscale(element.GetBitmap());
bmp.Save(@"C:\gray" + _imgCounter + ".bmp");
//byte[] imageData = null;
//using (MemoryStream ms = new MemoryStream())
//{
// bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
// imageData = ms.ToArray();
//}
pdftron.PDF.Image img = pdftron.PDF.Image.Create(pdf, bmp);
//pdftron.PDF.Image img = pdftron.PDF.Image.Create(pdf, imageData, bmp.Width,
// bmp.Height, 8, ColorSpace.CreateDeviceGray(), _JBIG2_hint);
ElementBuilder b = new ElementBuilder();
element = b.CreateImage(img, element.GetCTM());
written = true;
writer.WritePlacedElement(element);
break;
case Element.Type.e_form: // Recursively process form XObjects
{
reader.FormBegin();
ConvertImagesToGrayscale(reader, writer, pdf);
reader.End();
break;
}
default:
break;
}
if (!written)
{
writer.WriteElement(element);
}
}
}
// see http://tech.pro/tutorial/660/csharp-tutorial-convert-a-color-image-to-grayscale
public static Bitmap MakeGrayscale(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
}
}