[英]Rotation with pdfbox
I'm new to java and I am trying to get PDFBOX to rotate a PDF file.. I can't seem to get the error right.. I know it must be something simple but it says The operator < is undefined for the argument type(s) int, Dimension
on this line: 我是java的新手,我正尝试使PDFBOX旋转PDF文件。.我似乎无法正确解决错误。.我知道它一定很简单,但它说
The operator < is undefined for the argument type(s) int, Dimension
此行的The operator < is undefined for the argument type(s) int, Dimension
:
for (int i = 0; i < pages.size(); i++) {
And it strikes through size.. 它触及到大小。
My Code: 我的代码:
import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class PdfRotator {
private static final String pdfFile = null;
public static void main(String[] args) throws IOException {
PDDocument document = PDDocument.load("PDFrotatorTEST.pdf");
//public static void main(String[] args) throws Exception {
List pages = (List) document.getDocumentCatalog().getAllPages();
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) ((java.util.List) pages).get(i);// PDPage page = (PDPage) pages.get(i);
if ((i + 1) % 2 == 0) {
page.setRotation(0);
}
}
File f = new File(pdfFile);// File f = new File(pdfFile);
String newFile = f.getParent() + File.separator + "out.pdf";
document.save(newFile);
document.close();
}
}
Any help would be greatly appreciated 任何帮助将不胜感激
You import the class List
as 您将类
List
导入为
import java.awt.List;
But that is not the List
you want. 但这不是您想要的
List
。 instead you want 相反,你想要
import java.util.List;
As the OP is new to java , some backgrounds: 由于OP是java的新手 ,因此有一些背景:
There are several classes called List
in the java API and even more in third-party APIs. 在Java API中有几种称为
List
类,在第三方API中甚至更多。 Thus, when importing a List class
, you have to make sure you import the correct one. 因此,在导入
List class
,必须确保导入正确的类。
In particular there are: 特别是:
java.awt.List
, a component presents the user with a scrolling list of text items from the Abstract Window Toolkit , the oldest Java GUI API; java.awt.List
,一个组件为用户提供来自Abstract Window Toolkit (最早的Java GUI API)中文本项的滚动列表 ; and java.util.List
, the interface for an ordered collection (also known as a 'sequence') from the Java Collections Framework . java.util.List
,来自Java Collections Framework的有序集合(也称为“序列”)的接口。 In your code you are dealing with the result of document.getDocumentCatalog().getAllPages()
which returns a list of PDPage
instances in a java.util.List
object. 在您的代码中,您正在处理
document.getDocumentCatalog().getAllPages()
的结果,该结果返回java.util.List
对象中的PDPage
实例的列表。
The error message The operator < is undefined for the argument type(s) int, Dimension is due to the fact that java.awt.List
(like java.util.List
) has a method size()
but this method returns a java.awt.Dimension
(a class that encapsulates the width and height of a component ) and not an int
. 错误消息operator <未为参数类型int定义,Dimension是由于
java.awt.List
(如java.util.List
)具有方法size()
但该方法返回一个java.awt.Dimension
的事实java.awt.Dimension
( 封装组件的宽度和高度的类),而不是int
。
Thus, i < pages.size()
tries to compare entities which are not naturally comparable. 因此,
i < pages.size()
尝试比较自然不具有可比性的实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.