本文整理汇总了Java中org.apache.pdfbox.pdmodel.common.PDStream.getStream方法的典型用法代码示例。如果您正苦于以下问题:Java PDStream.getStream方法的具体用法?Java PDStream.getStream怎么用?Java PDStream.getStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.common.PDStream
的用法示例。
在下文中一共展示了PDStream.getStream方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildImgKeyToPolNameMapping
import org.apache.pdfbox.pdmodel.common.PDStream; //导入方法依赖的package包/类
private static void buildImgKeyToPolNameMapping(PDPage page) throws IOException {
PDStream contents = page.getContents();
PDFStreamParser parser = new PDFStreamParser(contents.getStream());
parser.parse();
List tokens = parser.getTokens();
boolean concatStringPhase = false;
String polName = "";
String lastText = "";
for (int index = 0; index < tokens.size(); index++) {
Object obj = tokens.get(index);
if (obj instanceof PDFOperator) {
PDFOperator op = (PDFOperator) obj;
if (op.getOperation().equals("BT")) {
concatStringPhase = true;
polName = lastText;
lastText = "";
}
else if (op.getOperation().equals("ET")) {
concatStringPhase = false;
}
}
else if (concatStringPhase && obj instanceof COSString) {
COSString cosString = (COSString) obj;
lastText += " " + cosString.getString();
lastText = lastText.trim();
}
else if (!concatStringPhase && obj instanceof COSName) {
COSName cosName = (COSName) obj;
if (cosName.getName().startsWith("img")) {
mapImgKeyToPolName.put(cosName.getName(), polName);
}
}
}
}
示例2: processPages
import org.apache.pdfbox.pdmodel.common.PDStream; //导入方法依赖的package包/类
/**
* This will process all of the pages and the text that is in them.
*
* @param pages The pages object in the document.
*
* @throws IOException If there is an error parsing the text.
*/
protected void processPages( List<COSObjectable> pages ) throws IOException
{
if( startBookmark != null )
{
startBookmarkPageNumber = getPageNumber( startBookmark, pages );
}
if( endBookmark != null )
{
endBookmarkPageNumber = getPageNumber( endBookmark, pages );
}
if( startBookmarkPageNumber == -1 && startBookmark != null &&
endBookmarkPageNumber == -1 && endBookmark != null &&
startBookmark.getCOSObject() == endBookmark.getCOSObject() )
{
//this is a special case where both the start and end bookmark
//are the same but point to nothing. In this case
//we will not extract any text.
startBookmarkPageNumber = 0;
endBookmarkPageNumber = 0;
}
Iterator<COSObjectable> pageIter = pages.iterator();
while( pageIter.hasNext() )
{
PDPage nextPage = (PDPage)pageIter.next();
PDStream contentStream = nextPage.getContents();
currentPageNo++;
if( contentStream != null )
{
COSStream contents = contentStream.getStream();
processPage( nextPage, contents );
}
}
}
示例3: processPages
import org.apache.pdfbox.pdmodel.common.PDStream; //导入方法依赖的package包/类
protected void processPages(List<COSObjectable> pages) throws IOException
{
if (startBookmark != null)
{
startBookmarkPageNumber = getPageNumber(startBookmark, pages);
}
if (endBookmark != null)
{
endBookmarkPageNumber = getPageNumber(endBookmark, pages);
}
if (startBookmarkPageNumber == -1 && startBookmark != null &&
endBookmarkPageNumber == -1 && endBookmark != null &&
startBookmark.getCOSObject() == endBookmark.getCOSObject())
{
//this is a special case where both the start and end bookmark
//are the same but point to nothing. In this case
//we will not extract any getText.
startBookmarkPageNumber = 0;
endBookmarkPageNumber = 0;
}
for (COSObjectable page : pages)
{
PDPage nextPage = (PDPage) page;
PDStream contentStream = nextPage.getContents();
currentPageNo++;
if (contentStream != null)
{
COSStream contents = contentStream.getStream();
processPage(nextPage, contents);
}
}
}
示例4: hasText
import org.apache.pdfbox.pdmodel.common.PDStream; //导入方法依赖的package包/类
private static boolean hasText(PDPage page) throws IOException {
PDStream contents = page.getContents();
PDFStreamParser parser = new PDFStreamParser(contents.getStream());
parser.parse();
List tokens = parser.getTokens();
for (Object next : tokens) {
if (next instanceof PDFOperator) {
PDFOperator op = (PDFOperator) next;
if (op.getOperation().equalsIgnoreCase(Consts.PDFMetadata.TEXT_OPERATOR)) {
return true;
}
}
}
return false;
}
示例5: testDrunkenfistOriginal
import org.apache.pdfbox.pdmodel.common.PDStream; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/29220165/editing-content-in-pdf-using-pdfbox-removes-last-line-from-pdf">
* Editing content in pdf using PDFBox removes last line from pdf
* </a>
*
* Reproducing the issue.
*/
@Test
public void testDrunkenfistOriginal() throws IOException, COSVisitorException
{
try ( InputStream originalStream = getClass().getResourceAsStream("Original.pdf") )
{
PDDocument doc = PDDocument.load(originalStream);
PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
PDStream contents = page.getContents();
PDFStreamParser parser = new PDFStreamParser(contents.getStream());
parser.parse();
List<Object> tokens = parser.getTokens();
for (int j = 0; j < tokens.size(); j++) {
Object next = tokens.get(j);
if (next instanceof PDFOperator) {
PDFOperator op = (PDFOperator) next;
if (op.getOperation().equals("Tj")) {
COSString previous = (COSString) tokens.get(j - 1);
String string = previous.getString();
string = string.replace("@ordnum&", "-ORDERNR-");
string = string.replace("@shipid&", "-SHIPMENTID-");
string = string.replace("@customer&", "-CUSTOMERNR-");
string = string.replace("@fromname&", "-FROMNAME-");
tokens.set(j - 1, new COSString(string.trim()));
}
}
}
PDStream updatedStream = new PDStream(doc);
OutputStream out = updatedStream.createOutputStream();
ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
tokenWriter.writeTokens(tokens);
page.setContents(updatedStream);
doc.save(new File(RESULT_FOLDER, "Original-edited.pdf"));
doc.close();
}
}
示例6: removeBlueRectangles
import org.apache.pdfbox.pdmodel.common.PDStream; //导入方法依赖的package包/类
/**
* This document removes all blue filled rectangles. As the sample document
* only uses RGB colors, actually only uses "rg" to set the blue fill color,
* the code could be somewhat simplified.
*/
void removeBlueRectangles(PDDocument document) throws IOException
{
List<?> pages = document.getDocumentCatalog().getAllPages();
for (int i = 0; i < pages.size(); i++)
{
PDPage page = (PDPage) pages.get(i);
PDStream contents = page.getContents();
PDFStreamParser parser = new PDFStreamParser(contents.getStream());
parser.parse();
List<Object> tokens = parser.getTokens();
Stack<Boolean> blueState = new Stack<Boolean>();
blueState.push(false);
for (int j = 0; j < tokens.size(); j++)
{
Object next = tokens.get(j);
if (next instanceof PDFOperator)
{
PDFOperator op = (PDFOperator) next;
if (op.getOperation().equals("q"))
{
blueState.push(blueState.peek());
}
else if (op.getOperation().equals("Q"))
{
blueState.pop();
}
else if (op.getOperation().equals("rg"))
{
if (j > 2)
{
Object r = tokens.get(j-3);
Object g = tokens.get(j-2);
Object b = tokens.get(j-1);
if (r instanceof COSNumber && g instanceof COSNumber && b instanceof COSNumber)
{
blueState.pop();
blueState.push((
Math.abs(((COSNumber)r).floatValue() - 0) < 0.001 &&
Math.abs(((COSNumber)g).floatValue() - 0) < 0.001 &&
Math.abs(((COSNumber)b).floatValue() - 1) < 0.001));
}
}
}
else if (op.getOperation().equals("f"))
{
if (blueState.peek() && j > 0)
{
Object re = tokens.get(j-1);
if (re instanceof PDFOperator && ((PDFOperator)re).getOperation().equals("re"))
{
tokens.set(j, PDFOperator.getOperator("n"));
}
}
}
}
}
PDStream updatedStream = new PDStream(document);
OutputStream out = updatedStream.createOutputStream();
ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
tokenWriter.writeTokens(tokens);
page.setContents(updatedStream);
}
}