addImageWatermark method
Future<ApiResponse>
addImageWatermark(
- File file,
- File watermarkImage,
- ImageWatermarkOptions options
Add an image watermark to a PDF
file
PDF file to watermark
watermarkImage
Image file to use as watermark
options
Image watermark options
Implementation
Future<ApiResponse> addImageWatermark(
File file,
File watermarkImage,
ImageWatermarkOptions options,
) async {
// Create a multipart request manually
final uri = Uri.parse('${ScanProConfig.apiUrl}/api/pdf/watermark');
final request = http.MultipartRequest('POST', uri);
// Add API key header
request.headers.addAll({'x-api-key': ScanProConfig.apiKey});
// Add PDF file
final pdfFileName = file.path.split('/').last;
final pdfBytes = await file.readAsBytes();
request.files.add(
http.MultipartFile.fromBytes(
'file',
pdfBytes,
filename: pdfFileName,
contentType: MediaType.parse('application/pdf'),
),
);
// Add watermark image file
final imageFileName = watermarkImage.path.split('/').last;
final imageBytes = await watermarkImage.readAsBytes();
// Determine image content type
String imageContentType = 'image/jpeg';
if (imageFileName.toLowerCase().endsWith('.png')) {
imageContentType = 'image/png';
} else if (imageFileName.toLowerCase().endsWith('.svg')) {
imageContentType = 'image/svg+xml';
}
request.files.add(
http.MultipartFile.fromBytes(
'watermarkImage',
imageBytes,
filename: imageFileName,
contentType: MediaType.parse(imageContentType),
),
);
// Add watermark options
request.fields.addAll(options.toParams());
// Send the request
final streamedResponse = await request.send().timeout(
ScanProConfig.timeout,
);
final response = await http.Response.fromStream(streamedResponse);
// Parse the response
try {
final data = json.decode(response.body);
if (response.statusCode >= 200 && response.statusCode < 300) {
return ApiResponse(
success: true,
data: data,
statusCode: response.statusCode,
);
} else {
String errorMessage = 'An error occurred';
if (data is Map && data.containsKey('error')) {
errorMessage = data['error'].toString();
}
return ApiResponse(
success: false,
error: errorMessage,
statusCode: response.statusCode,
data: data,
);
}
} catch (e) {
return ApiResponse(
success: false,
error: 'Failed to parse response: ${e.toString()}',
statusCode: response.statusCode,
data: response.body,
);
}
}