[英]PDFBox: How to print a range of pages from a PDF
I sincerely don't know how to approach this. 我真诚地不知道如何处理这个问题。 I'm new to this library (PDFBox) and i managed to implemented code (using Java), that prints any selected PDF.
我是这个库(PDFBox)的新手,我设法实现了代码(使用Java),可以打印任何选定的PDF。
Now i need to allow users specify a range of pages to print, if needed. 现在,如果需要,我需要允许用户指定要打印的页面范围。 Here is the part of my code that handles printing...
这是我处理打印的代码的一部分......
try
{
// TODO add your handling code here:
PrintService myPrintService = findPrintService(printerCmb.getSelectedItem().toString());
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(doc);
job.setPrintService(myPrintService);
job.print( );
}
catch (PrinterException ex)
{
Logger.getLogger(PrintDialog.class.getName()).log(Level.SEVERE, null, ex);
}
What do i do next? 我接下来做什么?
This is how i created "doc". 这就是我创建“doc”的方式。
public Pageable doc; JFileChooser getPDF = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("PDF File", "pdf");
getPDF.setFileFilter(filter);
getPDF.setDialogTitle("Select a PDF file");
getPDF.showOpenDialog(getPDF);
try
{
Connection conn = null;
conn = DriverManager.getConnection(urlDist);
//SQLiteConnection new2 = new SQLiteConnection(urlDist, filename);
File selPdf = getPDF.getSelectedFile();
doc = PDDocument.load(selPdf);
if (doc != null)
{
count = doc.getNumberOfPages();
noPagestxt.setText(String.valueOf(count ));
filename = selPdf.getName();
fileNametxt.setText(filename);
pagesPrint.setEnabled(true);
}
// cleaning memory
// cleaning memory
}
catch (Exception ex)
{
Logger.getLogger(BioProject.class.getName()).log(Level.SEVERE, null, ex);
}
So, I fixed the issue using user TilmanHausherr 's suggestion. 所以,我使用用户TilmanHausherr的建议解决了这个问题。 I specified the range using the
PageRanges()
function Here is the code. 我使用
PageRanges()
函数指定了范围这是代码。
...
job.setPageable(doc);
job.setPrintService(myPrintService);
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
PageRanges pageRng = new PageRanges( lower , upper);
attr.add(pageRng);
job.print(attr);
NOTE: upper
and lower
are Integer variables gotten from the user. 注意:
upper
和lower
是从用户获得的Integer变量。
Below is the code which can be helpful for printing data from specific pages in the whole PDF file hope this would solve your issue.
PDDocument doc = PDDocument.load("Your PDF path");
PDFTextStripper stripper = new PDFTextStripper();
stripper.setStartPage( 1 );
stripper.setEndPage( Integer.MAX_VALUE );
List<String> ans= Arrays.asList(changeText.split(",\n"));
System.out.println(ans);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.