Created
July 12, 2016 20:16
-
-
Save JoelGeraci-Datalogics/b48070cf69fb58bad00acfa0426ebb02 to your computer and use it in GitHub Desktop.
This sample removes the Collection dictionary from a PDF Portfolio making it a regular PDF file with attachments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright Datalogics, Inc. 2015 | |
*/ | |
package pdfjt.cookbook.document; | |
import com.adobe.internal.io.ByteReader; | |
import com.adobe.internal.io.InputStreamByteReader; | |
import com.adobe.pdfjt.core.types.ASName; | |
import com.adobe.pdfjt.pdf.document.PDFCatalog; | |
import com.adobe.pdfjt.pdf.document.PDFDocument; | |
import com.adobe.pdfjt.pdf.document.PDFFileSpecification; | |
import com.adobe.pdfjt.pdf.document.PDFNameDictionary; | |
import com.adobe.pdfjt.pdf.document.PDFNamedEmbeddedFiles; | |
import com.adobe.pdfjt.pdf.document.PDFOpenOptions; | |
import com.adobe.pdfjt.pdf.interactive.navigation.collection.PDFCollectionItem; | |
import com.adobe.pdfjt.pdf.interactive.navigation.collection.PDFCollectionItemData; | |
import com.adobe.pdfjt.pdf.interactive.navigation.collection.PDFCollectionSubItem; | |
import com.adobe.pdfjt.pdf.page.PDFPageMode; | |
import com.adobe.pdfjt.services.manipulations.PMMService; | |
import com.datalogics.pdf.document.DocumentHelper; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Iterator; | |
/** | |
* This sample removes the Collection dictionary from a PDF Portfolio making it a regular PDF file with attachments. | |
*/ | |
public class UnPortfolio { | |
private static final String inputPDFURL = "http://dev.datalogics.com/cookbook/portfolios/portfolio-sample.pdf"; | |
private static final String coverPDFURL = "http://dev.datalogics.com/cookbook/portfolios/DL_coversheet.pdf"; | |
private static final String outputDir = "cookbook/Document/output/"; | |
static public void main(String[] args) throws Exception { | |
// First read in the two PDF files we need. The Portfolio source and | |
// then the new cover sheet. The cover sheet will be the file that | |
// displayed when the PDF is opened. | |
URLConnection connection = new URL(inputPDFURL).openConnection(); | |
connection.setRequestProperty("User-Agent", | |
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); | |
connection.connect(); | |
InputStream fis = connection.getInputStream(); | |
ByteReader byteReader = new InputStreamByteReader(fis); | |
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance()); | |
connection = new URL(coverPDFURL).openConnection(); | |
connection.setRequestProperty("User-Agent", | |
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); | |
connection.connect(); | |
fis = connection.getInputStream(); | |
ByteReader coverSheetByteReader = new InputStreamByteReader(fis); | |
PDFDocument coverSheetDocument = PDFDocument.newInstance(coverSheetByteReader, PDFOpenOptions.newInstance()); | |
// Get the PDF Catalog | |
PDFCatalog pdfCatalog = pdfDocument.requireCatalog(); | |
if (pdfCatalog.getCollection() != null) { | |
// If the Collection dictionary is present, remove it. | |
pdfCatalog.removeValue(ASName.k_Collection); | |
// Set the Page Mode to display the attachments panel if the viewer | |
// supports it. | |
pdfCatalog.setPageMode(PDFPageMode.WithAttachments); | |
// Insert the new cover sheet then delete the original | |
PMMService pmmService = new PMMService(pdfDocument); | |
pmmService.appendPages(coverSheetDocument, null, null); | |
pmmService.deletePages(pdfDocument.requirePages().getPage(0), 1); | |
/* | |
* This section of the code is optional. The PDF Portfolio templates | |
* that come with Adobe Acrobat contain a "Summary" field that is | |
* generally used to enter a description of the file attachment. In | |
* this section we copy that data to the Description metadata field | |
* for the attachment so it can be seen in the Acrobat Attachments | |
* panel. | |
*/ | |
PDFNameDictionary pdfNameDictionary = pdfCatalog.getNameDictionary(); | |
PDFNamedEmbeddedFiles pdfNamedEmbeddedFiles = pdfNameDictionary.getNamedEmbeddedFiles(); | |
Iterator pdfNamedEmbeddedFilesIterator = pdfNamedEmbeddedFiles.iterator(); | |
while (pdfNamedEmbeddedFilesIterator.hasNext()) { | |
PDFNamedEmbeddedFiles.Entry curEntry = (PDFNamedEmbeddedFiles.Entry) pdfNamedEmbeddedFilesIterator.next(); | |
PDFFileSpecification pdfFileSpecification = curEntry.getValue(); | |
// Get the Collection Item (CI) dictionary | |
PDFCollectionItem pdfCollectionItem = pdfFileSpecification.getCollectionItem(); | |
String description; | |
if (pdfCollectionItem != null) { | |
/* | |
* Get the data or string in the Summary field. It could be | |
* a string or a dictionary that contains the pure string | |
* and a rich text representation. | |
*/ | |
PDFCollectionSubItem summaryItem = pdfCollectionItem.getSubItem(ASName.create("adobe:Summary")); | |
PDFCollectionItemData summaryItemData = summaryItem.getData(); | |
if (summaryItemData.isText() == false) { | |
description = summaryItem.getDictionaryTextStringOrStreamValue(ASName.k_D); | |
} else { | |
description = summaryItem.toString(); | |
} | |
// Set the description | |
pdfFileSpecification.setDescription(description); | |
// Remove the Collection Information since it will no longer be used. | |
pdfFileSpecification.setCollectionItem(null); | |
} | |
} | |
} | |
// Save and close | |
DocumentHelper.saveFullAndClose(pdfDocument, outputDir + "Not_A_Portfolio.pdf"); | |
// Save the file. | |
System.out.println("Done!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment