ReplaceImageMRC(String,Int32) Method
Replaces an image, specified by its resource name and the unique image identifier as well, located on the current page of the loaded PDF document, by compressing this image using the advanced mixed raster content (MRC) compression mechanism and draws a resulting image on the entire page surface.
Please note that both parameters relate to the same image located on the current page. At the same, this method can be used for image-based pages only, as the MRC engine always draws resulting images on the entire page surface. The attached example demonstrates the proper use of the method.
'Declaration
Public Overloads Function ReplaceImageMRC( _
ByVal As String, _
ByVal As Integer _
) As GdPictureStatus
Parameters
- ImageResName
- The image resource name of the source image you want to compress, located on the current page. Please use the GdPicturePDF.GetPageImageResName method to obtain this name.
- ImageID
- The unique image identifier of the source image you want to compress, located on the current page, referring to an object of the type GdPictureImage. Please use the GdPicturePDF.ExtractPageImage method to obtain this identifier.
Return Value
A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.
We strongly recommend always checking this status first.
How to compress image-based pages in the PDF document using the MRC mechanism.
Dim caption As String = "Example: ReplaceImageMRC"
Using pdf As GdPicturePDF = New GdPicturePDF()
Dim status As GdPictureStatus = pdf.LoadFromFile("source.pdf", False)
If status = GdPictureStatus.OK Then
'Setting MRC options.
pdf.SetMRCImageBackgroundResolution(100)
'You can set it to true to obtain a better quality rendering, but it produces a bigger file.
pdf.SetMRCPreserveSmoothing(False)
'You can also set the compression values for images here, for example:
pdf.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2)
pdf.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG2000)
'Using the negative value permits to specify better quality factor versus a compression rate.
pdf.SetJpeg2000Quality(-60)
Dim pageCount As Integer = pdf.GetPageCount()
status = pdf.GetStat()
Dim pageNo As Integer = 1
While (pageNo <= pageCount) AndAlso (status = GdPictureStatus.OK)
status = pdf.SelectPage(pageNo)
If status = GdPictureStatus.OK Then
'We can ignore hidden text in the example.
Dim isPageImage As Boolean = pdf.IsPageImage(True)
status = pdf.GetStat()
If status = GdPictureStatus.OK Then
If isPageImage Then
'As the page is image-based, the image number is 1 and we can extract this image.
Dim imageID As Integer = pdf.ExtractPageImage(1)
status = pdf.GetStat()
If status = GdPictureStatus.OK Then
'Getting the resource name of the extracted image, the index of the image to be used here is 0.
Dim imageResName As String = pdf.GetPageImageResName(0)
status = pdf.GetStat()
If status = GdPictureStatus.OK Then
'The original image is replaced with the compressed image on the whole page surface here.
status = pdf.ReplaceImageMRC(imageResName, imageID)
'You need to dispose of the used image resource here.
GdPictureDocumentUtilities.DisposeImage(imageID)
End If
End If
End If
End If
End If
pageNo += 1
End While
If status = GdPictureStatus.OK Then
status = pdf.SaveToFile("dest.pdf", True)
If status = GdPictureStatus.OK Then
MessageBox.Show("The example has been followed successfully and the file has been saved.", caption)
Else
MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption)
End If
Else
MessageBox.Show("The example has NOT been followed successfully. Status: " + status.ToString(), caption)
End If
Else
MessageBox.Show("The file can't be loaded. Status: " + status.ToString(), caption)
End If
End Using
string caption = "Example: ReplaceImageMRC";
using (GdPicturePDF pdf = new GdPicturePDF())
{
GdPictureStatus status = pdf.LoadFromFile("source.pdf", false);
if (status == GdPictureStatus.OK)
{
//Setting MRC options.
pdf.SetMRCImageBackgroundResolution(100);
//You can set it to true to obtain a better quality rendering, but it produces a bigger file.
pdf.SetMRCPreserveSmoothing(false);
//You can also set the compression values for images here, for example:
pdf.SetCompressionForBitonalImage(PdfCompression.PdfCompressionJBIG2);
pdf.SetCompressionForColorImage(PdfCompression.PdfCompressionJPEG2000);
//Using the negative value permits to specify better quality factor versus a compression rate.
pdf.SetJpeg2000Quality(-60);
int pageCount = pdf.GetPageCount();
status = pdf.GetStat();
for (int pageNo = 1; (pageNo <= pageCount) && (status == GdPictureStatus.OK); pageNo++)
{
status = pdf.SelectPage(pageNo);
if (status == GdPictureStatus.OK)
{
//We can ignore hidden text in the example.
bool isPageImage = pdf.IsPageImage(true);
status = pdf.GetStat();
if (status == GdPictureStatus.OK)
{
if (isPageImage)
{
//As the page is image-based, the image number is 1 and we can extract this image.
int imageID = pdf.ExtractPageImage(1);
status = pdf.GetStat();
if (status == GdPictureStatus.OK)
{
//Getting the resource name of the extracted image, the index of the image to be used here is 0.
string imageResName = pdf.GetPageImageResName(0);
status = pdf.GetStat();
if (status == GdPictureStatus.OK)
{
//The original image is replaced with the compressed image on the whole page surface here.
status = pdf.ReplaceImageMRC(imageResName, imageID);
//You need to dispose of the used image resource here.
GdPictureDocumentUtilities.DisposeImage(imageID);
}
}
}
}
}
}
if (status == GdPictureStatus.OK)
{
status = pdf.SaveToFile("dest.pdf", true);
if (status == GdPictureStatus.OK)
MessageBox.Show("The example has been followed successfully and the file has been saved.", caption);
else
MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption);
}
else
MessageBox.Show("The example has NOT been followed successfully. Status: " + status.ToString(), caption);
}
else
MessageBox.Show("The file can't be loaded. Status: " + status.ToString(), caption);
}