本文整理汇总了Java中com.itextpdf.text.pdf.PdfDictionary.put方法的典型用法代码示例。如果您正苦于以下问题:Java PdfDictionary.put方法的具体用法?Java PdfDictionary.put怎么用?Java PdfDictionary.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.pdf.PdfDictionary
的用法示例。
在下文中一共展示了PdfDictionary.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSwitchOrientation
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/34394199/i-cant-rotate-my-page-from-existing-pdf">
* I can't rotate my page from existing PDF
* </a>
* <p>
* Switching between portrait and landscape like this obviously will cut off some parts of the page.
* </p>
*/
@Test
public void testSwitchOrientation() throws DocumentException, IOException
{
try (InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/n2013.00849449.pdf"))
{
PdfReader reader = new PdfReader(resourceStream);
int n = reader.getNumberOfPages();
PdfDictionary pageDict;
for (int i = 1; i <= n; i++) {
Rectangle rect = reader.getPageSize(i);
Rectangle crop = reader.getCropBox(i);
pageDict = reader.getPageN(i);
pageDict.put(PdfName.MEDIABOX, new PdfArray(new float[] {rect.getBottom(), rect.getLeft(), rect.getTop(), rect.getRight()}));
pageDict.put(PdfName.CROPBOX, new PdfArray(new float[] {crop.getBottom(), crop.getLeft(), crop.getTop(), crop.getRight()}));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "n2013.00849449-switch.pdf")));
stamper.close();
reader.close();
}
}
示例2: buildSignedPDF
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
public byte[] buildSignedPDF(String digestOID, byte[] signature, byte[] hash) throws Exception {
byte[] hashTmp = null;
if (dateTime != null)
hashTmp = hash;
byte[] pkcs7enc = PKCS7Manager.buildPDFPKCS7(digestOID, x509Certificate, signature, hashTmp, dateTime);
PdfDictionary dic = new PdfDictionary();
PdfString contents = new PdfString(pkcs7enc).setHexWriting(true);
contentsSize = contents.length();
dic.put(PdfName.CONTENTS, contents);
sap.close(dic);
return bout.toByteArray();
}
示例3: tiff2Pdf
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
public void tiff2Pdf(File singleTifFile, File pdfFile) {
// based on tiff2pdf from itext toolbox 0.0.2
// (cfr.http://itexttoolbox.sourceforge.net/doku.php?id=download&DokuWiki=
// ecde1bfec0b8cca87dd8c6c042183992)
try {
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(
singleTifFile.getAbsolutePath());
// RandomAccessFileOrArray ra = new
// RandomAccessFileOrArray(tempByteArray);
int comps = TiffImage.getNumberOfPages(ra);
Document document = new Document(PageSize.A4);
float width = PageSize.A4.getWidth();
float height = PageSize.A4.getHeight();
Image img = TiffImage.getTiffImage(ra, 1);
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
// pdf/a
// from
// http://www.opensubscriber.com/message/[email protected]
// .net/7593470.html
// check that it is really pdf/a:
// http://www.intarsys.de/produkte/pdf-a-live/pdf-a-check-1
// => 2 warnings
// Keine eindeutige ID gefunden
// Kein History-Eintrag vorhanden
writer.setPDFXConformance(PdfWriter.PDFA1B);
document.open();
PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
outi.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString(
"sRGB IEC61966-2.1"));
outi.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
outi.put(PdfName.S, PdfName.GTS_PDFA1);
ICC_Profile icc = ICC_Profile.getInstance(Thread.currentThread()
.getContextClassLoader().getResourceAsStream(
"/srgb.profile"));
PdfICCBased ib = new PdfICCBased(icc);
ib.remove(PdfName.ALTERNATE);
outi.put(PdfName.DESTOUTPUTPROFILE, writer.addToBody(ib)
.getIndirectReference());
writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS,
new PdfArray(outi));
PdfContentByte cb = writer.getDirectContent();
for (int c = 0; c < comps; ++c) {
img = TiffImage.getTiffImage(ra, c + 1);
if (img != null) {
document.setPageSize(PageSize.A4);
document.newPage();
img.setAbsolutePosition(0, 0);
img.scaleToFit(width, height);
cb.addImage(img);
logger.debug("Finished page " + (c + 1));
}
}
ra.close();
writer.createXmpMetadata();// pdfa
document.close();
} catch (Throwable e) {
// catch Throwable because we encountere a java.lang.InternalError
// cfr. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6503430
// probably better to move to later java version for poller
logger.error("Pdf not created", e);
}
}
示例4: testWidgetToField
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/31402602/how-to-rename-only-the-first-found-duplicate-acrofield-in-pdf">
* How to rename only the first found duplicate acrofield in pdf?
* </a>
* <br>
* <a href="http://s000.tinyupload.com/index.php?file_id=34970992934525199618">
* test_duplicate_field2.pdf
* </a>
* <p>
* Demonstration of how to transform generate a new field for a widget.
* </p>
*/
@Test
public void testWidgetToField() throws IOException, DocumentException
{
try ( InputStream resource = getClass().getResourceAsStream("test_duplicate_field2.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test_duplicate_field2-widgetToField.pdf")) )
{
PdfReader reader = new PdfReader(resource);
PdfDictionary form = reader.getCatalog().getAsDict(PdfName.ACROFORM);
PdfArray fields = form.getAsArray(PdfName.FIELDS);
for (PdfObject object: fields)
{
PdfDictionary field = (PdfDictionary) PdfReader.getPdfObject(object);
if ("Text1".equals(field.getAsString(PdfName.T).toString()))
{
PdfDictionary newField = new PdfDictionary();
PRIndirectReference newFieldRef = reader.addPdfObject(newField);
fields.add(newFieldRef);
newField.putAll(field);
newField.put(PdfName.T, new PdfString("foobar"));
PdfArray newKids = new PdfArray();
newField.put(PdfName.KIDS, newKids);
PdfArray kids = field.getAsArray(PdfName.KIDS);
PdfObject widget = kids.remove(0);
newKids.add(widget);
PdfDictionary widgetDict = (PdfDictionary) PdfReader.getPdfObject(widget);
widgetDict.put(PdfName.PARENT, newFieldRef);
break;
}
}
PdfStamper stamper = new PdfStamper(reader, result);
stamper.close();
}
}
示例5: testMarkAnnotationsReadOnly
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/37275267/how-to-make-pdf-annotation-as-read-only-using-itext">
* how to make pdf annotation as read only using itext?
* </a>
* <br/>
* test-annotated.pdf <i>simple PDF with sticky note</i>
*
* <p>
* This test shows how to set the read-only flags of all annotations of a document.
* </p>
*/
@Test
public void testMarkAnnotationsReadOnly() throws IOException, DocumentException
{
try ( InputStream resourceStream = getClass().getResourceAsStream("test-annotated.pdf");
OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "test-annotated-ro.pdf")) )
{
PdfReader reader = new PdfReader(resourceStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);
for (int page = 1; page <= reader.getNumberOfPages(); page++)
{
PdfDictionary pageDictionary = reader.getPageN(page);
PdfArray annotationArray = pageDictionary.getAsArray(PdfName.ANNOTS);
if (annotationArray == null)
continue;
for (PdfObject object : annotationArray)
{
PdfObject directObject = PdfReader.getPdfObject(object);
if (directObject instanceof PdfDictionary)
{
PdfDictionary annotationDictionary = (PdfDictionary) directObject;
PdfNumber flagsNumber = annotationDictionary.getAsNumber(PdfName.F);
int flags = flagsNumber != null ? flagsNumber.intValue() : 0;
flags |= PdfAnnotation.FLAGS_READONLY;
annotationDictionary.put(PdfName.F, new PdfNumber(flags));
}
}
}
stamper.close();
}
}
示例6: testWithStamper
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
@Test
public void testWithStamper() throws DocumentException, IOException
{
InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
try
{
PdfReader reader = new PdfReader(resourceStream);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper.pdf")));
// Go through all pages
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++)
{
Rectangle pageSize = reader.getPageSize(i);
Rectangle rect = getOutputPageSize(pageSize, reader, i);
PdfDictionary page = reader.getPageN(i);
page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
stamper.markUsed(page);
}
stamper.close();
}
finally
{
if (resourceStream != null)
resourceStream.close();
}
}
示例7: testWithStamperTopBottom
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
@Test
public void testWithStamperTopBottom() throws DocumentException, IOException
{
InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
try
{
PdfReader reader = new PdfReader(resourceStream);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-top-bottom.pdf")));
// Go through all pages
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++)
{
Rectangle pageSize = reader.getPageSize(i);
Rectangle rect = getOutputPageSize2(pageSize, reader, i);
PdfDictionary page = reader.getPageN(i);
page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
stamper.markUsed(page);
}
stamper.close();
}
finally
{
if (resourceStream != null)
resourceStream.close();
}
}
示例8: testWithStamperCentered
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
@Test
public void testWithStamperCentered() throws DocumentException, IOException
{
InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
try
{
PdfReader reader = new PdfReader(resourceStream);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-centered.pdf")));
// Go through all pages
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++)
{
Rectangle pageSize = reader.getPageSize(i);
Rectangle rect = getOutputPageSize3(pageSize, reader, i);
PdfDictionary page = reader.getPageN(i);
page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
stamper.markUsed(page);
}
stamper.close();
}
finally
{
if (resourceStream != null)
resourceStream.close();
}
}
示例9: testWithStamperExtFinder
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
@Test
public void testWithStamperExtFinder() throws DocumentException, IOException
{
InputStream resourceStream = getClass().getResourceAsStream("test.pdf");
try
{
PdfReader reader = new PdfReader(resourceStream);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-ext.pdf")));
// Go through all pages
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++)
{
Rectangle pageSize = reader.getPageSize(i);
Rectangle rect = getOutputPageSize4(pageSize, reader, i);
PdfDictionary page = reader.getPageN(i);
page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()}));
stamper.markUsed(page);
}
stamper.close();
}
finally
{
if (resourceStream != null)
resourceStream.close();
}
}
示例10: splitIntoHalfPages
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* This methods creates a copy of the source document containing each page twice,
* once with the cropbox limited to the left half page, once to the right one.
*/
void splitIntoHalfPages(InputStream source, File target) throws IOException, DocumentException
{
final PdfReader reader = new PdfReader(source);
try ( OutputStream targetStream = new FileOutputStream(target) )
{
Document document = new Document();
PdfCopy copy = new PdfCopy(document, targetStream);
document.open();
for (int page = 1; page <= reader.getNumberOfPages(); page++)
{
PdfDictionary pageN = reader.getPageN(page);
Rectangle cropBox = reader.getCropBox(page);
PdfArray leftBox = new PdfArray(new float[]{cropBox.getLeft(), cropBox.getBottom(), (cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getTop()});
PdfArray rightBox = new PdfArray(new float[]{(cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getBottom(), cropBox.getRight(), cropBox.getTop()});
PdfImportedPage importedPage = copy.getImportedPage(reader, page);
pageN.put(PdfName.CROPBOX, leftBox);
copy.addPage(importedPage);
pageN.put(PdfName.CROPBOX, rightBox);
copy.addPage(importedPage);
}
document.close();
}
finally
{
reader.close();
}
}
示例11: testChangeAppearances
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/37027579/how-to-associate-a-previous-signature-in-a-new-signature-field">
* How to associate a previous signature in a new signature field
* </a>
* <br/>
* <span>BLANK-signed.pdf, <em>a blank file from elsewhere with an invisible signature.</em></span>
* <p>
* Quite surprisingly it turns out that changing the signature appearance is possible without
* breaking the signature, merely a warning appears which can be hidden by simply signing again.
* </p>
*/
@Test
public void testChangeAppearances() throws IOException, DocumentException
{
try ( InputStream resource = getClass().getResourceAsStream("BLANK-signed.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "BLANK-signed-app.pdf")))
{
PdfReader pdfReader = new PdfReader(resource);
PdfStamper pdfStamper = new PdfStamper(pdfReader, result, '\0', true);
AcroFields acroFields = pdfStamper.getAcroFields();
for (String signatureName : acroFields.getSignatureNames())
{
Item field = acroFields.getFieldItem(signatureName);
field.writeToAll(PdfName.RECT, new PdfArray(new int[]{100,100,200,200}), Item.WRITE_WIDGET);
field.markUsed(acroFields, Item.WRITE_WIDGET);
PdfAppearance appearance = PdfAppearance.createAppearance(pdfStamper.getWriter(), 100, 100);
appearance.setColorStroke(BaseColor.RED);
appearance.moveTo(0, 0);
appearance.lineTo(99, 99);
appearance.moveTo(0, 99);
appearance.lineTo(99, 0);
appearance.stroke();
PdfDictionary appDict = new PdfDictionary();
appDict.put(PdfName.N, appearance.getIndirectReference());
field.writeToAll(PdfName.AP, appDict, Item.WRITE_WIDGET);
}
pdfStamper.close();
}
}
示例12: testPbNoToUnicode
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/37748346/extract-text-with-itext-not-works-encoding-or-crypted-text">
* Extract text with iText not works: encoding or crypted text?
* </a>
* <br/>
* <a href="https://dl.dropboxusercontent.com/u/6413030/pb.pdf">
* pb.pdf
* </a>
* <p>
* The document has not been provided by the OP but by
* <a href="http://stackoverflow.com/users/1127485/sschuberth">sschuberth</a>
* in a comment.
* </p>
* <p>
* In contrast to {@link #testPb()}, we here first remove the <b>ToUnicode</b>
* tables of the fonts. And indeed, now extraction succeeds.
* </p>
*/
@Test
public void testPbNoToUnicode() throws Exception
{
InputStream resourceStream = getClass().getResourceAsStream("pb.pdf");
try
{
PdfReader reader = new PdfReader(resourceStream);
for (int i = 1; i <= reader.getNumberOfPages(); i++)
{
PdfDictionary pageResources = reader.getPageResources(i);
if (pageResources == null)
continue;
PdfDictionary pageFonts = pageResources.getAsDict(PdfName.FONT);
if (pageFonts == null)
continue;
for (PdfName key : pageFonts.getKeys())
{
PdfDictionary fontDictionary = pageFonts.getAsDict(key);
fontDictionary.put(PdfName.TOUNICODE, null);
}
}
String content = extractAndStore(reader, new File(RESULT_FOLDER, "pb-noToUnicode.%s.txt").toString());
System.out.println("\nText pb.pdf without ToUnicode\n************************");
System.out.println(content);
System.out.println("************************");
}
finally
{
if (resourceStream != null)
resourceStream.close();
}
}
示例13: addPage
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
public void addPage(PdfReader reader, int pageNumber, PdfRectangle cropBox) throws TaskException {
PdfImportedPage page = pdfCopy.getImportedPage(reader, pageNumber);
PdfDictionary dictionary = reader.getPageN(pageNumber);
dictionary.put(PdfName.MEDIABOX, cropBox);
dictionary.put(PdfName.CROPBOX, cropBox);
addPage(page);
}
示例14: testInsertTitlePage
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/28911509/how-to-retain-page-labels-when-concatenating-an-existing-pdf-with-a-pdf-created">
* How to retain page labels when concatenating an existing pdf with a pdf created from scratch?
* </a>
* <p>
* A proposal how to implement the task using a {@link PdfStamper}.
*/
@Test
public void testInsertTitlePage() throws IOException, DocumentException
{
try ( InputStream documentStream = getClass().getResourceAsStream("Labels.pdf");
InputStream titleStream = getClass().getResourceAsStream("Cover.pdf");
OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "labels-with-cover-page.pdf")) )
{
PdfReader titleReader = new PdfReader(titleStream);
PdfReader reader = new PdfReader(documentStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);
PdfImportedPage page = stamper.getImportedPage(titleReader, 1);
stamper.insertPage(1, titleReader.getPageSize(1));
PdfContentByte content = stamper.getUnderContent(1);
content.addTemplate(page, 0, 0);
copyLinks(stamper, 1, titleReader, 1);
PdfDictionary root = reader.getCatalog();
PdfDictionary labels = root.getAsDict(PdfName.PAGELABELS);
if (labels != null)
{
PdfArray newNums = new PdfArray();
newNums.add(new PdfNumber(0));
PdfDictionary coverDict = new PdfDictionary();
coverDict.put(PdfName.P, new PdfString("Cover Page"));
newNums.add(coverDict);
PdfArray nums = labels.getAsArray(PdfName.NUMS);
if (nums != null)
{
for (int i = 0; i < nums.size() - 1; )
{
int n = nums.getAsNumber(i++).intValue();
newNums.add(new PdfNumber(n+1));
newNums.add(nums.getPdfObject(i++));
}
}
labels.put(PdfName.NUMS, newNums);
stamper.markUsed(labels);
}
stamper.close();
}
}
示例15: testComplex
import com.itextpdf.text.pdf.PdfDictionary; //导入方法依赖的package包/类
@Test
public void testComplex() throws FileNotFoundException, DocumentException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "transparencyComplex.pdf")));
writer.setCompressionLevel(0);
document.open();
PdfContentByte content = writer.getDirectContent();
content.setRGBColorStroke(0, 255, 0);
for (int y = 0; y <= 400; y+= 10)
{
content.moveTo(0, y);
content.lineTo(500, y);
}
for (int x = 0; x <= 500; x+= 10)
{
content.moveTo(x, 0);
content.lineTo(x, 400);
}
content.stroke();
PdfTemplate template = content.createTemplate(500, 400);
PdfTransparencyGroup group = new PdfTransparencyGroup();
group.put(PdfName.CS, PdfName.DEVICEGRAY);
group.setIsolated(false);
group.setKnockout(false);
template.setGroup(group);
PdfShading radial = PdfShading.simpleRadial(writer, 262, 186, 10, 262, 186, 190, BaseColor.WHITE, BaseColor.BLACK, true, true);
template.paintShading(radial);
PdfDictionary mask = new PdfDictionary();
mask.put(PdfName.TYPE, PdfName.MASK);
mask.put(PdfName.S, new PdfName("Luminosity"));
mask.put(new PdfName("G"), template.getIndirectReference());
content.saveState();
PdfGState state = new PdfGState();
state.put(PdfName.SMASK, mask);
content.setGState(state);
content.setRGBColorFill(255, 0, 0);
content.moveTo(162, 86);
content.lineTo(162, 286);
content.lineTo(362, 286);
content.lineTo(362, 86);
content.closePath();
//content.fillStroke();
content.fill();
content.restoreState();
document.close();
}