0

I'm struggling to limit the pages to be printed.

I'm using: Java17, Ubuntu 20.04

Using this example, selecting a PDF and limiting the page with PageRanges attribute will just not work.

IPP specification has the page-ranges attribute.

public class Main {
 public static void main(String[] args) throws FileNotFoundException, PrintException {
 JFileChooser fileChooser = new JFileChooser();
 int result = fileChooser.showOpenDialog(null);
 if (result == JFileChooser.APPROVE_OPTION) {
 DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
 FileInputStream inputStream = new FileInputStream(fileChooser.getSelectedFile());
 Doc doc = new SimpleDoc(inputStream, flavor, null);
 PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
 DocPrintJob printJob = ps.createPrintJob();
 PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
 attributeSet.add(new PageRanges(1));
 printJob.print(doc, attributeSet);
 }
 }
}

Update:

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService printService : printServices) {
 System.out.println("---> %s".formatted(printService.getName()));
 List.of(PageRanges.class, Copies.class, PrintQuality.class, Sides.class)
 .forEach(i -> System.out.println("%s -> %s".formatted(
 i.getName(),
 printService.isAttributeCategorySupported(i))));
}

Outputs:

---> PDF
javax.print.attribute.standard.PageRanges -> true
javax.print.attribute.standard.Copies -> true
javax.print.attribute.standard.PrintQuality -> false
javax.print.attribute.standard.Sides -> true
---> SP-3710SF
javax.print.attribute.standard.PageRanges -> true
javax.print.attribute.standard.Copies -> true
javax.print.attribute.standard.PrintQuality -> false
javax.print.attribute.standard.Sides -> true

Edit 2:

There's a statement on PageRanges about IPP (But I did not really know what it means):

IPP Compatibility: The PageRanges attribute's canonical array form gives the lower and upper bound for each range of pages to be included in and IPP "page-ranges" attribute. See class SetOfIntegerSyntax for an explanation of canonical array form. The category name returned by getName() gives the IPP attribute name.

asked Aug 18, 2023 at 15:00
5
  • What about attributeSet.add(new PageRanges(1, 2));? Commented Aug 18, 2023 at 15:16
  • What does System.out.println(ps.isAttributeCategorySupported(PageRanges.class)); display? Commented Aug 18, 2023 at 15:49
  • @g00se Yes, I've tried this one an new PageRanges(new int[][]{{1}}) Commented Aug 18, 2023 at 17:36
  • Different. What's the answer to @VGR's question? Commented Aug 18, 2023 at 17:45
  • @VGR I thought It would return false, but it returns true. I've updated the question. Commented Aug 18, 2023 at 17:46

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.