|
Here we recolor one page out of a document. We pick up the
ProcessingObject
events so that we can store the source color space for all the
PixMap objects which are processed. We don't want to convert CMYK
pixmaps so we set the Cancel property to true if we find these.
We then pick up the ProcessedObject
events so that we can recompress the PixMap objects after they have
been recolored. We vary the recompression method used dependent on
the source color space and the size of the image.
Here we use standard delegates for backwards compatibility with
.NET 1.1 code. However .NET 2.0 anonymous delegates will provide a
more compact solution.
[C#]
Doc theDoc = new Doc();
theDoc.Read(Server.MapPath("../mypics/sample.pdf"));
MyOp.Recolor(theDoc, (Page)theDoc.ObjectSoup[theDoc.Page]);
theDoc.Save(Server.MapPath("RecolorOperation.pdf"));
theDoc.Clear();
private class MyOp{
public static void Recolor(Doc doc, Page page) {
RecolorOperation op = new
RecolorOperation();
op.DestinationColorSpace = new
ColorSpace(doc.ObjectSoup, ColorSpaceType.DeviceGray);
op.ConvertAnnotations = false;
op.ProcessingObject += Recoloring;
op.ProcessedObject += Recolored;
op.Recolor(page);
}
public static void Recoloring(object sender,
ProcessingObjectEventArgs e) {
PixMap pm = e.Object as PixMap;
if (pm != null) {
ColorSpaceType cs =
pm.ColorSpaceType;
if (cs ==
ColorSpaceType.DeviceCMYK)
e.Cancel =
true;
e.Tag = cs;
}
}
public static void Recolored(object sender,
ProcessedObjectEventArgs e) {
if (e.Successful) {
PixMap pm = e.Object as
PixMap;
if (pm != null) {
ColorSpaceType cs =
(ColorSpaceType)e.Tag;
if (pm.Width >
1000)
pm.CompressJpx(30);
else if (cs ==
ColorSpaceType.DeviceRGB)
pm.CompressJpeg(30);
else
pm.Compress();
// Flate
}
}
}
}
[Visual Basic]
Dim theDoc As Doc = New Doc()
theDoc.Read(Server.MapPath("../mypics/sample.pdf"))
MyOp.Recolor(theDoc, CType(theDoc.ObjectSoup(theDoc.Page),
Page))
theDoc.Save(Server.MapPath("RecolorOperation.pdf"))
theDoc.Clear()
Private Class MyOp
Public Shared Sub Recolor(doc As Doc, page As Page)
Dim op As New RecolorOperation()
op.DestinationColorSpace = New
ColorSpace(doc.ObjectSoup, ColorSpaceType.DeviceGray)
op.ConvertAnnotations = False
AddHandler op.ProcessingObject, AddressOf
Recoloring
AddHandler op.ProcessedObject, AddressOf
Recolored
op.Recolor(page)
End Sub 'Recolor
Public Shared Sub Recoloring(sender As Object, e As
ProcessingObjectEventArgs)
Dim pm As PixMap = TryCast(e.Object,
PixMap)
If pm IsNot Nothing Then
Dim cs As ColorSpaceType =
pm.ColorSpaceType
If cs =
ColorSpaceType.DeviceCMYK Then
e.Cancel = True
End If
e.Tag = cs
End If
End Sub 'Recoloring
Public Shared Sub Recolored(sender As Object, e As
ProcessedObjectEventArgs)
If e.Successful Then
Dim pm As PixMap =
TryCast(e.Object, PixMap)
If pm IsNot Nothing Then
Dim cs As
ColorSpaceType = CType(e.Tag, ColorSpaceType)
If pm.Width >
1000 Then
pm.CompressJpx(30)
Else
If cs =
ColorSpaceType.DeviceRGB Then
pm.CompressJpeg(30)
Else
pm.Compress()
' Flate
End
If
End If
End If
End If
End Sub 'Recolored
End Class 'MyOp
|
|
|