Dim caption As String = "Example: ReplaceImage"
Dim gdpicturePDF As New GdPicturePDF()
If gdpicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
Dim imageCount As Integer = 0, imageID As Integer = 0, bitDepth As Integer = 0
Dim message As String = "", imageResName As String = ""
Dim maskMode As PdfImageMaskType = PdfImageMaskType.PdfMaskTypeUnknown
Dim mask As Boolean = False
Dim oImage As New GdPictureImaging()
Dim pageCount As Integer = gdpicturePDF.GetPageCount()
Dim status As GdPictureStatus = gdpicturePDF.GetStat()
If (status = GdPictureStatus.OK) AndAlso (pageCount > 0) Then
For i As Integer = 1 To pageCount
status = gdpicturePDF.SelectPage(i)
If status = GdPictureStatus.OK Then
message = message + "Page nr." + i.ToString() + vbCrLf
imageCount = gdpicturePDF.GetPageImageCount()
status = gdpicturePDF.GetStat()
If (status = GdPictureStatus.OK) AndAlso (imageCount > 0) Then
For j As Integer = 0 To imageCount - 1
imageResName = gdpicturePDF.GetPageImageResName(j)
status = gdpicturePDF.GetStat()
If status = GdPictureStatus.OK Then
imageID = gdpicturePDF.ExtractPageImage(j + 1)
status = gdpicturePDF.GetStat()
If status = GdPictureStatus.OK Then
bitDepth = oImage.GetBitDepth(imageID)
status = oImage.GetStat()
If (status = GdPictureStatus.OK) AndAlso
(oImage.GetBitDepth(imageID) > 1) AndAlso
(oImage.ConvertTo1Bpp(imageID) = GdPictureStatus.OK) Then
maskMode = gdpicturePDF.GetPageImageMaskMode(j)
mask = ((maskMode <> PdfImageMaskType.PdfMaskTypeNone) AndAlso (maskMode <> PdfImageMaskType.PdfMaskTypeUnknown))
status = gdpicturePDF.ReplaceImage(imageResName, imageID, mask)
If status = GdPictureStatus.OK Then
message = message + "The image named as " + imageResName + " has been successfully replaced." + vbCrLf
Else
message = message + "The ReplaceImage() method has failed for the image numbered as " + (j + 1).ToString() + " with the status: " + status.ToString() + vbCrLf
End If
Else
message = message + "The image numbered as " + (j + 1).ToString() + " has not been converted and replaced." + vbCrLf
End If
Else
message = message + "The ExtractPageImage() method has failed for the image numbered as " + (j + 1).ToString() + " with the status: " + status.ToString() + vbCrLf
End If
'The extracted image need to be released.
oImage.ReleaseGdPictureImage(imageID)
Else
message = message + "The GetPageImageResName() method has failed for the image indexed as " + j.ToString() + " with the status: " + status.ToString() + vbCrLf
End If
Next
Else
If status = GdPictureStatus.OK Then
message = message + "This page doesn't contain any image." + vbCrLf
Else
message = message + "The GetPageImageCount() method has failed with the status: " + status.ToString() + vbCrLf
End If
End If
Else
message = message + "The SelectPage() method has failed with the status: " + status.ToString() + vbCrLf
End If
Next
'It is recommended to pack the document in such cases.
If gdpicturePDF.SaveToFile("test_ReplaceImage.pdf", True) = GdPictureStatus.OK Then
message = message + "The file has been saved successfully."
Else
message = message + "The file can't be saved. Status: " + gdpicturePDF.GetStat().ToString()
End If
MessageBox.Show(message, caption)
Else
If status = GdPictureStatus.OK Then
MessageBox.Show("This file doesn't contain any page.", caption)
Else
MessageBox.Show("The GetPageCount() method has failed with the status: " + status.ToString(), caption)
End If
End If
oImage.Dispose()
Else
MessageBox.Show("The file can't be loaded.", caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: ReplaceImage";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if (gdpicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
{
int imageCount = 0, imageID = 0, bitDepth = 0;
string message = "", imageResName = "";
PdfImageMaskType maskMode = PdfImageMaskType.PdfMaskTypeUnknown;
bool mask = false;
GdPictureImaging oImage = new GdPictureImaging();
int pageCount = gdpicturePDF.GetPageCount();
GdPictureStatus status = gdpicturePDF.GetStat();
if ((status == GdPictureStatus.OK) && (pageCount > 0))
{
for (int i = 1; i <= pageCount; i++)
{
status = gdpicturePDF.SelectPage(i);
if (status == GdPictureStatus.OK)
{
message = message + "Page nr." + i.ToString() + "\n";
imageCount = gdpicturePDF.GetPageImageCount();
status = gdpicturePDF.GetStat();
if ((status == GdPictureStatus.OK) && (imageCount > 0))
{
for (int j = 0; j < imageCount; j++)
{
imageResName = gdpicturePDF.GetPageImageResName(j);
status = gdpicturePDF.GetStat();
if (status == GdPictureStatus.OK)
{
imageID = gdpicturePDF.ExtractPageImage(j+1);
status = gdpicturePDF.GetStat();
if (status == GdPictureStatus.OK)
{
bitDepth = oImage.GetBitDepth(imageID);
status = oImage.GetStat();
if ((status == GdPictureStatus.OK) &&
(oImage.GetBitDepth(imageID) > 1) &&
(oImage.ConvertTo1Bpp(imageID) == GdPictureStatus.OK))
{
maskMode = gdpicturePDF.GetPageImageMaskMode(j);
mask = ((maskMode != PdfImageMaskType.PdfMaskTypeNone) && (maskMode != PdfImageMaskType.PdfMaskTypeUnknown));
status = gdpicturePDF.ReplaceImage(imageResName, imageID, mask);
if (status == GdPictureStatus.OK)
message = message + "The image named as " + imageResName + " has been successfully replaced.\n";
else
message = message + "The ReplaceImage() method has failed for the image numbered as " + (j + 1).ToString() + " with the status: " + status.ToString() + "\n";
}
else
message = message + "The image numbered as " + (j + 1).ToString() + " has not been converted and replaced.\n";
}
else
message = message + "The ExtractPageImage() method has failed for the image numbered as " + (j+1).ToString() + " with the status: " + status.ToString() + "\n";
//The extracted image need to be released.
oImage.ReleaseGdPictureImage(imageID);
}
else
message = message + "The GetPageImageResName() method has failed for the image indexed as " + j.ToString() + " with the status: " + status.ToString() + "\n";
}
}
else
{
if (status == GdPictureStatus.OK)
message = message + "This page doesn't contain any image.\n";
else
message = message + "The GetPageImageCount() method has failed with the status: " + status.ToString() + "\n";
}
}
else
message = message + "The SelectPage() method has failed with the status: " + status.ToString() + "\n";
}
//It is recommended to pack the document in such cases.
if (gdpicturePDF.SaveToFile("test_ReplaceImage.pdf", true) == GdPictureStatus.OK)
message = message + "The file has been saved successfully.";
else
message = message + "The file can't be saved. Status: " + gdpicturePDF.GetStat().ToString();
MessageBox.Show(message, caption);
}
else
{
if (status == GdPictureStatus.OK)
MessageBox.Show("This file doesn't contain any page.", caption);
else
MessageBox.Show("The GetPageCount() method has failed with the status: " + status.ToString(), caption);
}
oImage.Dispose();
}
else
MessageBox.Show("The file can't be loaded.", caption);
gdpicturePDF.Dispose();