Package org.pdfclown.bytes

Examples of org.pdfclown.bytes.IBuffer


    int streamIndex = -1;
    for(PdfDataObject streamObject : streamObjects)
    {
      PdfStream stream = (PdfStream)File.resolve(streamObject);
      IBuffer streamBody = stream.getBody();
      model.addRow(
        new Object[]
        {
          "(stream " + (++streamIndex) + ")",
          streamBody.getString(0, (int)streamBody.getLength())
        }
        );
    }
  }
View Full Code Here


  {
    // 1. Body.
    int dataByteOffset;
    {
      // Serializing the entries into the stream buffer...
      IBuffer indexBuffer = new Buffer();
      IBuffer dataBuffer = new Buffer();
      IndirectObjects indirectObjects = file.getIndirectObjects();
      int objectIndex = -1;
      for(Map.Entry<Integer,ObjectEntry> entry : getEntries().entrySet())
      {
        final int objectNumber = entry.getKey();

        // Update the xref entry!
        XRefEntry xrefEntry = indirectObjects.get(objectNumber).getXrefEntry();
        xrefEntry.setOffset(++objectIndex);

        /*
          NOTE: The entry offset MUST be updated only after its serialization, in order not to interfere
          with its possible data-object retrieval from the old serialization.
        */
        int entryValueOffset = (int)dataBuffer.getLength();

        // Index.
        indexBuffer
          .append(Integer.toString(objectNumber)).append(Chunk.Space) // Object number.
          .append(Integer.toString(entryValueOffset)).append(Chunk.Space); // Byte offset (relative to the first one).

        // Data.
        entry.getValue().getDataObject().writeTo(dataBuffer);
        entry.getValue().offset = entryValueOffset;
      }

      // Get the stream buffer!
      final IBuffer body = getBody();

      // Delete the old entries!
      body.setLength(0);

      // Add the new entries!
      body.append(indexBuffer);
      dataByteOffset = (int)body.getLength();
      body.append(dataBuffer);
    }

    // 2. Header.
    {
      final PdfDictionary header = getHeader();
View Full Code Here

    PdfDataObject scriptObject = getBaseDataObject().get(PdfName.JS);
    if(scriptObject instanceof PdfTextString)
    {return ((PdfTextString)scriptObject).getValue();}
    else
    {
      IBuffer scriptBuffer = ((PdfStream)scriptObject).getBody();
      return scriptBuffer.getString(0,(int)scriptBuffer.getLength());
    }
  }
View Full Code Here

    PdfDataObject scriptObject = getBaseDataObject().get(PdfName.JS);
    if(scriptObject instanceof PdfTextString)
    {((PdfTextString)scriptObject).setValue(value);}
    else
    {
      IBuffer scriptBuffer = ((PdfStream)scriptObject).getBody();
      scriptBuffer.setLength(0);
      scriptBuffer.append(value);
    }
  }
View Full Code Here

        getFieldSize((int)stream.getLength()), // NOTE: We assume this xref stream is the last indirect object.
        EntryField2Size
      };
    {
      // Get the stream buffer!
      final IBuffer body = getBody();

      // Delete the old entries!
      body.setLength(0);

      // Serializing the entries into the stream buffer...
      int prevObjectNumber = -2; // Previous-entry object number.
      for(XRefEntry entry : entries.values())
      {
        int entryNumber = entry.getNumber();
        if(entryNumber - prevObjectNumber != 1) // Current subsection terminated.
        {
          if(!indexArray.isEmpty())
          {indexArray.add(new PdfInteger(prevObjectNumber-((PdfInteger)indexArray.get(indexArray.size()-1)).getValue()+1));} // Number of entries in the previous subsection.
          indexArray.add(new PdfInteger(entryNumber)); // First object number in the next subsection.
        }
        prevObjectNumber = entryNumber;

        switch(entry.getUsage())
        {
          case Free:
            body.append((byte)FreeEntryType);
            body.append(numberToByteArray(entry.getOffset(),entryFieldSizes[1]));
            body.append(numberToByteArray(entry.getGeneration(),entryFieldSizes[2]));
            break;
          case InUse:
            body.append((byte)InUseEntryType);
            body.append(numberToByteArray(entry.getOffset(),entryFieldSizes[1]));
            body.append(numberToByteArray(entry.getGeneration(),entryFieldSizes[2]));
            break;
          case InUseCompressed:
            body.append((byte)InUseCompressedEntryType);
            body.append(numberToByteArray(entry.getStreamNumber(),entryFieldSizes[1]));
            body.append(numberToByteArray(entry.getOffset(),entryFieldSizes[2]));
            break;
          default:
            throw new UnsupportedOperationException();
        }
      }
View Full Code Here

        {
          // Which kind of image?
          if(header.get(PdfName.Filter).equals(PdfName.DCTDecode)) // JPEG image.
          {
            // Get the image data (keeping it encoded)!
            IBuffer body = ((PdfStream)dataObject).getBody(false);
            // Export the image!
            exportImage(
              body,
              getOutputPath() + java.io.File.separator + "ImageExtractionSample_" + (index++) + ".jpg"
              );
View Full Code Here

  {
    if(entries == null)
    {
      entries = new TreeMap<Integer,XRefEntry>();

      final IBuffer body = getBody();
      if(body.getLength() > 0)
      {
        final PdfDictionary header = getHeader();
        final int size = ((PdfInteger)header.get(PdfName.Size)).getValue();
        final int[] entryFieldSizes;
        {
          final PdfArray entryFieldSizesObject = (PdfArray)header.get(PdfName.W);
          entryFieldSizes = new int[entryFieldSizesObject.size()];
          for(int index = 0, length = entryFieldSizes.length; index < length; index++)
          {entryFieldSizes[index] = ((PdfInteger)entryFieldSizesObject.get(index)).getValue();}
        }

        final PdfArray subsectionBounds;
        if(header.containsKey(PdfName.Index))
        {subsectionBounds = (PdfArray)header.get(PdfName.Index);}
        else
        {
          subsectionBounds = new PdfArray();
          subsectionBounds.add(new PdfInteger(0));
          subsectionBounds.add(new PdfInteger(size));
        }

        body.setByteOrder(ByteOrder.BIG_ENDIAN);
        body.seek(0);

        final Iterator<PdfDirectObject> subsectionBoundIterator = subsectionBounds.iterator();
        while(subsectionBoundIterator.hasNext())
        {
          try
          {
            final int start = ((PdfInteger)subsectionBoundIterator.next()).getValue();
            final int count = ((PdfInteger)subsectionBoundIterator.next()).getValue();
            for(
              int entryIndex = start,
                length = start + count;
              entryIndex < length;
              entryIndex++
              )
            {
              final int entryFieldType = (entryFieldSizes[0] == 0 ? 1 : body.readInt(entryFieldSizes[0]));
              switch(entryFieldType)
              {
                case FreeEntryType:
                {
                  final int nextFreeObjectNumber = body.readInt(entryFieldSizes[1]);
                  final int generation = body.readInt(entryFieldSizes[2]);
                  entries.put(
                    entryIndex,
                    new XRefEntry(
                      entryIndex,
                      generation,
                      nextFreeObjectNumber,
                      XRefEntry.UsageEnum.Free
                      )
                    );
                  break;
                }
                case InUseEntryType:
                {
                  final int offset = body.readInt(entryFieldSizes[1]);
                  final int generation = body.readInt(entryFieldSizes[2]);
                  entries.put(
                    entryIndex,
                    new XRefEntry(
                      entryIndex,
                      generation,
                      offset,
                      XRefEntry.UsageEnum.InUse
                      )
                    );
                  break;
                }
                case InUseCompressedEntryType:
                {
                  final int streamNumber = body.readInt(entryFieldSizes[1]);
                  final int innerNumber = body.readInt(entryFieldSizes[2]);
                  entries.put(
                    entryIndex,
                    new XRefEntry(
                      entryIndex,
                      innerNumber,
View Full Code Here

        */
        // Get the existing data object from the corresponding indirect object!
        PdfStream toUnicodeStream = (PdfStream)toUnicodeReference.getDataObject();

        // Editing the data object...
        IBuffer streamBody = toUnicodeStream.getBody();
        streamBody.setLength(0); // Erases the stream content to prepare it for new content insertion.
        streamBody.append("... modified ..."); // Adds arbitrary contents (NOTE: this would NOT be done in a real ToUnicode stream! We are just testing the editing functionality...).
      }
      else
      {
        /*
          Approach 2: Creating a new data object.
        */
        // Create a new data object!
        PdfStream toUnicodeStream = new PdfStream();
        // Associate the new data object to the existing indirect object, replacing the old one!
        toUnicodeReference.setDataObject(toUnicodeStream);

        // Editing the data object...
        IBuffer streamBody = toUnicodeStream.getBody();
        streamBody.append("... created ..."); // Adds arbitrary contents (NOTE: this would NOT be done in a real ToUnicode stream! We are just testing the editing functionality...).
      }

      toUnicodeReference.getIndirectObject().update(); // Ensures that the indirect object is updated.
    }

View Full Code Here

        stream = (PdfStream)streamReference.getDataObject();
      }
    }

    // Get the stream buffer!
    IBuffer buffer = stream.getBody();
    // Delete old contents from the stream buffer!
    buffer.setLength(0);
    // Serializing the new contents into the stream buffer...
    for(ContentObject item : items)
    {item.writeTo(buffer);}

    // Update the content stream container!
View Full Code Here

      }
    }

    // Body (contents).
    {
      IBuffer formBody = formStream.getBody();
      PdfDataObject contentsDataObject = getBaseDataObject().resolve(PdfName.Contents);
      if(contentsDataObject instanceof PdfStream)
      {formBody.append(((PdfStream)contentsDataObject).getBody());}
      else
      {
        for(PdfDirectObject contentStreamObject : (PdfArray)contentsDataObject)
        {formBody.append(((PdfStream)File.resolve(contentStreamObject)).getBody());}
      }
    }

    return form;
  }
View Full Code Here

TOP

Related Classes of org.pdfclown.bytes.IBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.