I have this code to set make a document black and white as required, using aspose and jpedal
public static ByteArrayOutputStream setColorFlagPdf( ByteArrayOutputStream dstStream, PrintParams PrintJO) throws IOException, IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, PdfException
{
byte[] arr = dstStream.toByteArray();
ConvertPagesToImages extract=new ConvertPagesToImages( arr );
ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream in = new ByteArrayInputStream(dstStream.toByteArray());
com.aspose.pdf.Document Npdf = new com.aspose.pdf.Document();
int npages = 0;
if (extract.openPDFFile())
{
npages = extract.getPageCount();
}
double pageWidthR = 1.0;
double pageHeightR = 1.0;
int paperId = Paper.class.getField(PrintJO.PaperSize).getInt(null);
pageWidthR = Paper.getWidth(paperId);
pageHeightR = Paper.getHeight(paperId);
int pgno = 1 ;
int[] Pages ;
double lowerLeftX = 0;
double lowerLeftY = 0;
double upperRightX = (pageWidthR * 72);
double upperRightY = (pageHeightR * 72);
BufferedImage img = null;
PdfFileEditor pfe = new PdfFileEditor();
try
{
while ( pgno <= npages)
{
if (extract.openPDFFile())
{
img = extract.getPageAsImage(pgno, true );
}
BufferedImage gray = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = gray.createGraphics();
g.drawImage(img, 0, 0, null);
Npdf.getPages().add();
Pages = new int[pgno];
for (int i = 0; i < pgno ; i++)
{
Pages[i] = i+1;
}
pfe.resizeContents(Npdf, Pages, PdfFileEditor.ContentsResizeParameters.pageResize(pageWidthR * 72, pageHeightR * 72));
com.aspose.pdf.Page page = Npdf.getPages().get_Item(pgno);
// Add an image to the Images collection of the page resources
page.getResources().getImages().add( gray );
page.getContents().add(new com.aspose.pdf.Operator.GSave());
// Create Rectangle and Matrix objects
com.aspose.pdf.Rectangle rectangle = new com.aspose.pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
com.aspose.pdf.Matrix matrix = new com.aspose.pdf.Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY()- rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });
page.getContents().add(new com.aspose.pdf.Operator.ConcatenateMatrix(matrix));
com.aspose.pdf.XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
page.getContents().add(new com.aspose.pdf.Operator.Do(ximage.getName()));
page.getContents().add(new com.aspose.pdf.Operator.GRestore());
System.out.println("colorflag done Page No : " + pgno);
pgno++;
img.flush();
gray.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
extract.closePDFfile();
Npdf.save(output);
System.out.println(“colorflag done”);
}
return output;
}
In this code, it works completely fine on a project eclipse, but in a rest service on Wildfly, it gives this error on the line page.getResources().getImages().add( gray );
here is some of the stack trace
16:56:07,334 ERROR [stderr] (default task-2) java.lang.IllegalArgumentException: Map size (0) must be >= 1
16:56:07,341 ERROR [stderr] (default task-2) at java.awt.image.IndexColorModel.setRGBs(IndexColorModel.java:510)
16:56:07,347 ERROR [stderr] (default task-2) at java.awt.image.IndexColorModel.(IndexColorModel.java:176)
16:56:07,353 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p344.z1.m1(Unknown Source)
16:56:07,358 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p344.z1.read(Unknown Source)
16:56:07,363 ERROR [stderr] (default task-2) at javax.imageio.ImageReader.read(ImageReader.java:939)
16:56:07,369 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p282.z1.m1(Unknown Source)
16:56:07,375 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p282.z1.m6(Unknown Source)
16:56:07,382 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p243.z2.m1(Unknown Source)
16:56:07,389 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p243.z2.(Unknown Source)
16:56:07,396 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p243.z2.(Unknown Source)
16:56:07,401 ERROR [stderr] (default task-2) at com.aspose.pdf.internal.p243.z31.m1(Unknown Source)
16:56:07,406 ERROR [stderr] (default task-2) at com.aspose.pdf.XImageCollection.m1(Unknown Source)
16:56:07,412 ERROR [stderr] (default task-2) at com.aspose.pdf.XImageCollection.m1(Unknown Source)
16:56:07,417 ERROR [stderr] (default task-2) at com.aspose.pdf.XImageCollection.add(Unknown Source)
16:56:07,423 ERROR [stderr] (default task-2) at mentor.csp.Conversion.setColorFlagPdf(Conversion.java:866)
Document I tried is c2.pdf attached… No matter which document, i try… The error is same on server…
Hey i have checked, the error comes only when i convert the image to gray by
BufferedImage gray = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
But still it works fine in a project but not on the server…
Need Help
Hi Usman,
ByteArrayOutputStream output = new ByteArrayOutputStream();<o:p></o:p>
com.aspose.pdf.Document Npdf = new com.aspose.pdf.Document("c:/pdftest/c2.pdf");
int npages = 0;
npages = Npdf.getPages().size();
double pageWidthR = 1.0;
double pageHeightR = 1.0;
//int paperId = Paper.class.getField(PrintJO.PaperSize).getInt(null);
pageWidthR = 600;
pageHeightR = 800;
int pgno = 1 ;
int[] Pages ;
double lowerLeftX = 0;
double lowerLeftY = 0;
double upperRightX = (pageWidthR * 72);
double upperRightY = (pageHeightR * 72);
java.awt.image.BufferedImage img = null;
com.aspose.pdf.facades.PdfFileEditor pfe = new com.aspose.pdf.facades.PdfFileEditor();
try
{
while ( pgno <= npages)
{
// Create stream object to save the output image
java.io.OutputStream imageStream = new java.io.FileOutputStream("c:/pdftest/Converted_Image" + pgno + ".jpg");
// Create Resolution object
com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(300);
// Create JpegDevice object where second argument indicates the quality of resultant image
com.aspose.pdf.devices.JpegDevice jpegDevice = new com.aspose.pdf.devices.JpegDevice(resolution, 100);
// Convert a particular page and save the image to stream
jpegDevice.process(Npdf.getPages().get_Item(pgno), imageStream);
// Close the stream
imageStream.close();
img = ImageIO.read(new File("c:/pdftest/Converted_Image" + pgno + ".jpg"));
//img = extract.getPageAsImage(pgno, true );
java.awt.image.BufferedImage gray = new java.awt.image.BufferedImage(img.getWidth(), img.getHeight(),
java.awt.image.BufferedImage.TYPE_BYTE_GRAY);
java.awt.Graphics2D g = gray.createGraphics();
g.drawImage(img, 0, 0, null);
Npdf.getPages().add();
Pages = new int[pgno];
for (int i = 0; i < pgno ; i++)
{
Pages[i] = i+1;
}
pfe.resizeContents(Npdf, Pages, com.aspose.pdf.facades.PdfFileEditor.ContentsResizeParameters.pageResize(pageWidthR * 72, pageHeightR * 72));
com.aspose.pdf.Page page = Npdf.getPages().get_Item(pgno);
// Add an image to the Images collection of the page resources
page.getResources().getImages().add( gray );
page.getContents().add(new com.aspose.pdf.Operator.GSave());
// Create Rectangle and Matrix objects
com.aspose.pdf.Rectangle rectangle = new com.aspose.pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
com.aspose.pdf.Matrix matrix = new com.aspose.pdf.Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY()- rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });
page.getContents().add(new com.aspose.pdf.Operator.ConcatenateMatrix(matrix));
com.aspose.pdf.XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());
page.getContents().add(new com.aspose.pdf.Operator.Do(ximage.getName()));
page.getContents().add(new com.aspose.pdf.Operator.GRestore());
System.out.println("colorflag done Page No : " + pgno);
pgno++;
img.flush();
gray.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
Npdf.save("c:/pdftest/GrayScale_Conversion.pdf");
System.out.println("colorflag done");
}