---
title: "java itext 清空PDF标签"
author: "reymont"
date: "2011-05-11T10:55:26Z"
tags: ["Blog", "ITeye", "Java"]
categories: ["编程语言"]
slug: "java-itext-pdf"
draft: false
lastmod: "2011-06-13T10:48:22Z"
id: 1039741
url: "https://reymont.iteye.com/blog/1039741"
---

欢迎转载，请注明来自

### [http://reymont.iteye.com/blog/1039741](/blog/1039741)

 

思路：使用itext-2.0.8，请自行下载。重新生成一个PDF文件，然后设置该文件的标签为空标签

 

import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;

import com.lowagie.text.DocumentException;  
import com.lowagie.text.pdf.PdfReader;  
import com.lowagie.text.pdf.PdfStamper;

public class ClearBookmark {

    public static void main(String\[] args) {  
        String oldFile = "D:/download/1.pdf";  
        String newFile = "D:/download/2.pdf";  
        new ClearBookmark().setOutlines(oldFile, newFile);  
    }

    public void setOutlines(String oldFile, String newFile) {  
        try {  
            // create a reader for a certain document  
            PdfReader reader = new PdfReader(oldFile);

            // we create a stamper that will copy the document to a new file  
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(  
                    newFile));

            // read the outlines structure file  
            // readOutlinesFile(outlinesFile);

            // set the outlines  
            //stamp.setOutlines(createOutlines());  
            stamp.setOutlines(new ArrayList());

            // closing PdfStamper will generate the new PDF file  
            stamp.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }

    }

    public List createOutlines() {  
        List list = new ArrayList();

        HashMap map = null;  
        map = new HashMap();

        list.add(map);

        return list;  
    }  
}

 

欢迎转载，请注明来自

### [http://reymont.iteye.com/blog/1039741](/blog/1039741)