如何重新排序PDF文件的页面? [英] How to reorder the pages of a PDF file?

查看:218
本文介绍了如何重新排序PDF文件的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最后生成目录,我想在开始时移动目录。
假设我的PDF中有16页,TOC从第13页开始,到第15页结束。
我想将TOC移到第二页,这样第一页仍然是第1页最后一页仍然是第16页。
这段代码没有给我我想要的东西:

  public void changePagesOrder (){
try {
PdfReader sourcePDFReader = new PdfReader(RESULT1);
int n = sourcePDFReader.getNumberOfPages();
System.out.println(pdf文件中没有页面......+ n);
int totalNoPages = n;
int tocStartsPage = 13;

sourcePDFReader.selectPages(String.format(%d-%d,2-%d,tocStartsPage,totalNoPages-1,tocStartsPage -2));
PdfStamper stamper = new PdfStamper(sourcePDFReader,new FileOutputStream(RESULT2));
stamper.close();

System.out.println(pdf更改完成.....);
}
catch(Exception ex){
}
}

请提出一些解决方案。

解决方案

你的公式错了。你有:

  sourcePDFReader.selectPages(String.format(%d-%d,2-%d,tocStartsPage, totalNoPages-1,tocStartsPage -2); 

但是这会将你的TOC放在第1页。这不是根据你的描述你想要什么。



你想要这样的东西:

  PdfReader reader = new PdfReader(baos.toByteArray()); 
int startToc = 13;
int n = reader.getNumberOfPages();
reader.selectPages(String.format( 1,%s-%s,2%s,%s,startToc,n-1,startToc - 1,n));
PdfStamper压模=新PdfStamper(读者,新FileOutputStream(dest)) ;
stamper.close();

此代码使用重新排序页面示例PDF文件有16页,文字第1页第2页,...,第16页作为内容。结果是以下PDF :(重新排序) .pdf)[ http://itextpdf.com/sites/default/files/reordered.pdf]



页面现在按此顺序排列:第1页,第13页,第14页,第15页,第2页,第3页,第4页,第5页,第6页,第7页,第8页,第9页,第10页,第11页,第12页,第16页。这是您在问题中描述的顺序。



更新:



在评论中,你问的是 String.format()在这种情况下如何工作。



让我们先看看我们想要实现的目标。我们按此顺序有页面:

  1,2,3,4,5,6,7,8,9,10 ,11,12,13,14,15,16 

我们想对它们重新排序:

  1,13,14,15,2,3,4,5,6,7,8,9,10,11 ,12,16 

这意味着我们需要这种模式:

  1,13-15,2-12,16 

这是一个硬编码模式,其中两个变量很重要:




  • TOC的开始:第13页( startToc

  • 最后一页:16( n



从这些变量中,我们得到两个变量:




  • TOC的最后一页。这是最后一页减一,或16 - 1 = 15( n - 1

  • TOC之前的最后一页: 13 - 1 = 12( startToc - 1



我们现在可以重写像这样的模式:

  1,startToc-(n  -  1),2-(startToc  -  1),n 

我们需要将其设为 String ,这样就可以了为什么我们使用 String.format()

  String.format( 1,%s-%s,2%s,%s,startToc,n-1,startToc  -  1,n)

第一次出现的%s String 后的第一个参数替换,第二次出现的%s String 后的第二个参数替换,依此类推...... / p>

如果 startToc = 13 n = 16 ,这个结果:

  1,13-15,2-12,16 


I am generating Table of Contents at last,I want to move Table of Contents at Beginning. Suppose that I have 16 pages in my PDF and that the TOC starts from page 13 and ends on page 15. I want to move the TOC to the second page, so that the first page remains page 1 and the last page remains page 16. This code doesn't give me what I want:

public void changePagesOrder()  {
    try {
         PdfReader sourcePDFReader = new PdfReader(RESULT1);    
         int n = sourcePDFReader.getNumberOfPages();
         System.out.println("no of pages in pdf files..."+n);
         int totalNoPages=n;
         int tocStartsPage=13;

         sourcePDFReader.selectPages(String.format("%d-%d, 2-%d", tocStartsPage, totalNoPages-1, tocStartsPage -2));
         PdfStamper stamper = new PdfStamper(sourcePDFReader, new FileOutputStream(RESULT2));
         stamper.close();   

         System.out.println("pdf changes are done.....");
    }
    catch(Exception ex) {
    }
}

Please suggest some solutions.

解决方案

Your formula is wrong. You have:

sourcePDFReader.selectPages(String.format("%d-%d, 2-%d", tocStartsPage, totalNoPages-1, tocStartsPage -2);

But that puts your TOC at page 1. That is not what you want according to your description.

You want something like this:

PdfReader reader = new PdfReader(baos.toByteArray());
int startToc = 13;
int n = reader.getNumberOfPages();
reader.selectPages(String.format("1,%s-%s, 2-%s, %s", startToc, n-1, startToc - 1, n));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();

This code was tested using the ReorderPage example on a PDF with 16 pages, having the text Page 1, Page 2, ..., Page 16 as content. The result was the following PDF: (reordered.pdf)[http://itextpdf.com/sites/default/files/reordered.pdf]

The pages are now in this order: page 1, page 13, page 14, page 15, page 2, page 3, page 4, page 5, page 6, page 7, page 8, page 9, page 10, page 11, page 12, page 16. This is the order you described in your question.

Update:

In a comment, you were asking how String.format() worked in this case.

Let's look at what we want to achieve first. We have pages in this order:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16

We want to reorder them like this:

1, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16

This means that we need this pattern:

1, 13-15, 2-12, 16

This is a hard-coded pattern, where two variables are important:

  • The start of the TOC: page 13 (startToc)
  • The final page: 16 (n)

From these variables, we derive two more variables:

  • The last page of the TOC. This is the last page minus one, or 16 - 1 = 15 (n - 1)
  • The last page before the TOC: 13 - 1 = 12 (startToc - 1)

We can now rewrite the pattern like this:

1, startToc-(n - 1), 2-(startToc - 1), n

We need to make this a String, so that's why we use String.format():

String.format("1,%s-%s, 2-%s, %s", startToc, n-1, startToc - 1, n)

The first occurrence of %s is replaced by the first parameter after the String, the second occurrence of %s is replaced by the second parameter after the String, and so on...

If startToc = 13 and n = 16, this results in:

1, 13-15, 2-12, 16

这篇关于如何重新排序PDF文件的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆