How do I edit / replace / convert images in PDF using PDFNet SDK?

Q:
I am trying to produce a PDF which is a copy of an input PDF but with all the images converted to grayscale.

The attached sample produces a grayscale image and apparently embeds that in the PDF (judging by file size) however, when opening the new PDF in adobe, it is blank.

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;

}

}

}

A:

Please see the attached code. I was able to get your sample working by making a few changes. I only used one PDFDoc object, which is read from disk, modified in-memory, and then saved to a new location. Also, instead of GetCTM() and WritePlaceElement(), I used GetGState().GetTransform() and WriteElement().

An alternative way to implement what you are looking for is to replace images without modifying the content stream:

For more info see:
https://groups.google.com/forum/?hl=en&fromgroups#!searchin/pdfnet-sdk/replace$20images

specifically: https://groups.google.com/d/msg/pdfnet-sdk/oWlA10uDOdk/gzYUqCCDxeEJ