I am adding several TIFF images into a single PDF document.
Looking at the final PDF document, some (not all) images are appearing as white text on black background when the original image is black text on white background.
I also noticed that by only loading and saving the tiff image, you get the inverted black/white result.
I also tried all TiffExpectedFormat values and Photometric combinations, some make invalid files and others are also inverted.
This is the code:
final String sampleTiffImagePath = "/path/to/Document/sampletiff.tiff";
public void testTiffToPdf() {
final int IMG_MARGIN_LEFT = 70;
final int IMG_MARGIN_RIGHT = 70;
final int IMG_MARGIN_TOP = 72;
final int IMG_MARGIN_BOTTOM = 72;
final int MAX_IMG_WIDTH = 595;
final int MAX_IMG_HEIGHT = 792;
com.aspose.pdf.Document pdf = new com.aspose.pdf.Document();
TiffImage tiff = (TiffImage) Image.load(sampleTiffImagePath);
TiffFrame[] frames = tiff.getFrames();
int count = 1;
for (TiffFrame frame : tiff.getFrames()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
TiffOptions opt = new TiffOptions(TiffExpectedFormat.TiffNoCompressionRgb);
frame.save(os, opt);
ByteArrayInputStream tiffStream = new ByteArrayInputStream(os.toByteArray());
Page page = pdf.getPages().add();
page.getPageInfo().getMargin().setBottom(IMG_MARGIN_BOTTOM);
page.getPageInfo().getMargin().setTop(IMG_MARGIN_TOP);
page.getPageInfo().getMargin().setLeft(IMG_MARGIN_LEFT);
page.getPageInfo().getMargin().setRight(IMG_MARGIN_RIGHT);
double proportionalMaxHeight = frame.getHeight() * (MAX_IMG_WIDTH + IMG_MARGIN_LEFT) / frame.getWidth();
page.setCropBox(new Rectangle(0, 0, MAX_IMG_WIDTH, proportionalMaxHeight));
com.aspose.pdf.Image image = new com.aspose.pdf.Image();
page.getParagraphs().add(image);
image.setImageStream(tiffStream);
count++;
}
pdf.save("/path/to/Document/test/TiffToPdf.pdf");
pdf.close();
}
public void testTiffSaveFrame() {
TiffImage tiff = (TiffImage) Image.load(sampleTiffImagePath);
TiffFrame[] frames = tiff.getFrames();
int count = 1;
for (TiffFrame frame : tiff.getFrames()) {
TiffOptions opt = new TiffOptions(TiffExpectedFormat.TiffNoCompressionRgb);
frame.save("/path/to/Document/test/testSaveFrame-" + count + ".tiff", opt);
}
}
public void testTiffSave() {
TiffImage tiff = (TiffImage) Image.load(sampleTiffImagePath);
TiffOptions opt = new TiffOptions(TiffExpectedFormat.TiffNoCompressionRgb);
tiff.save("/path/to/Document/test/testSaveTiff.tiff", opt);
}