From 9128ea7b8620fd57e7b74df21a314f262eca6ed5 Mon Sep 17 00:00:00 2001 From: Nick Miller Date: Sat, 6 Jul 2013 15:27:10 +1000 Subject: [PATCH] Site updated at 2013-07-06 05:27:10 UTC --- atom.xml | 313 +++++++++++++------------- blog/2013/07/03/pdfs/index.html | 311 +++++++++++++------------ blog/categories/-net-/atom.xml | 2 +- blog/categories/-net/atom.xml | 2 +- blog/categories/asp-net/atom.xml | 2 +- blog/categories/c-/atom.xml | 2 +- blog/categories/cheat-sheets/atom.xml | 2 +- blog/categories/exocortex/atom.xml | 2 +- blog/categories/howto/atom.xml | 2 +- blog/categories/lightswitch/atom.xml | 2 +- blog/categories/linux/atom.xml | 2 +- blog/categories/odata-/atom.xml | 2 +- blog/categories/silverlight/atom.xml | 2 +- blog/categories/underware/atom.xml | 2 +- blog/categories/xaml-/atom.xml | 2 +- index.html | 311 +++++++++++++------------ sitemap.xml | 6 +- 17 files changed, 482 insertions(+), 485 deletions(-) diff --git a/atom.xml b/atom.xml index a35c6df..51f82c2 100644 --- a/atom.xml +++ b/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ @@ -106,169 +106,152 @@ 4 5 6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22
[object number] 0 obj
 <<
 [metadata about object]
 >>
 [binary content in a stream]
 end obj
-
-In the PDF doc itself, the main parts are:
-
-* header
-* body
-* cross reference table (like dictionary for objects in the body)
-* trailer (locates cross reference table)
-
-Content layers, numbered 1 to 4:
-
-* 1 and 4 are for direct content
-* 2 and 3 are for higher level objects, like chunks, sections, chapters etc.
-* 1 is the bottom layer - under text and graphhics
-* 4 is the top layer- over text and graphics
-
-# Get writer for top layer:
 
-

PdfWriter.getDirectContent()

+

In the PDF doc itself, the main parts are:

+ + + + +

Content layers, numbered 1 to 4:

+ + + + +

Get writer for top layer:

1
-
Get writer for bottom layer:
+
PdfWriter.getDirectContent()
 
-

PdfWriter.getDirectContentUnder()

+

Get writer for bottom layer:

1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-16
-17
-
# Main classes for manipulating Pdf:
-
-* PdfWriter
-* PdfStamper
-* PdfImportedPage
-* PdfCopy
-
-
-# Reading docs
-To read PDF doc, use PdfReader.  Can tell you high level things about the doc like:
-
-* pages
-* rotation of pages
-* length
-* whether encrypted
-
-Get content stream of the page - the raw stream of the page with underlying PDF syntax:
+
PdfWriter.getDirectContentUnder()
 
-

var content stream = PdfReader.GetContentStream();

+

Main classes for manipulating Pdf:

+ + + + +

Reading docs

+ +

To read PDF doc, use PdfReader. Can tell you high level things about the doc like:

+ + + + +

Get content stream of the page - the raw stream of the page with underlying PDF syntax:

1
-2
-3
-4
-5
-
# How to extract a page from PDF:
-
-* DON'T attempt to extract a page using the PdfReader.
-* Pass the PdfReader to a PdfWriter and ask it to copy the page to another PDF.
-* This will give you a PdfImportedPage object.
+
var content stream = PdfReader.GetContentStream();
 
-

Document document = new Document(); -PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“extractPage.pdf”)); -document.open(); -PdfReader reader = new PdfReader(“sourceDocument.pdf”); -PdfImportedPage page = writer.getImportedPage(reader, 1); -// do something with it

+

How to extract a page from PDF:

+ + +
1
 2
 3
 4
 5
-
# To put a header and footer or stationary on a PDF:
+6
+7
+
Document document = new Document();
+PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("extractPage.pdf"));
+document.open();
+PdfReader reader = new PdfReader("sourceDocument.pdf");
+PdfImportedPage page = writer.getImportedPage(reader, 1);
+// do something with it
 
-* create the stationary in one PDF
-* import that page
-* superimpose that page on the target PDF
 
-

java -// open reader with source doc -// open target doc in writer -// create pdfContentByte by calling getDirectContent on the writer -// call addTemplate on the contentByte and feed it the imported page -PdfReader reader = new PdfReader(“stationary.pdf”); -Document document = new Document(); -PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“result.pdf”)); -document.open(); -PdfContentByte canvas = writer.getDirectContent(); -PdfImportedPage page;

+

To put a header and footer or stationary on a PDF:

-
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
-        page = writer.getImportedPage(reader, i);
-        canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
-    }
-
+ -

document.close(); -reader.close();

1
 2
 3
 4
 5
-
Problem with this: annotations are not part of the content stream, they are part of the annotations dictionary.
-They are not copied when using PdfImportedPage.
-
-
-# To stamp a page using PdfStamper:
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
// open reader with source doc
+// open target doc in writer
+// create pdfContentByte by calling getDirectContent on the writer
+// call addTemplate on the contentByte and feed it the imported page
+PdfReader reader = new PdfReader("stationary.pdf");
+Document document = new Document();
+PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("result.pdf"));
+document.open();
+PdfContentByte canvas = writer.getDirectContent();
+PdfImportedPage page;
+        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
+            page = writer.getImportedPage(reader, i);
+            canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
+        }
+document.close();
+reader.close();
 
-

java -PdfReader reader = new PdfReader(src); -PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); -PdfContentByte canvas = stamper.getOverContent(1); // note: compare to ‘getDirectContent()’ method on PdfWriter -ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(“Hello people!”), 36, 540, 0); -stamper.close(); -reader.close();

+

Problem with this: annotations are not part of the content stream, they are part of the annotations dictionary. +They are not copied when using PdfImportedPage.

+ +

To stamp a page using PdfStamper:

1
 2
@@ -276,38 +259,26 @@ reader.close();

4 5 6 -7 -8 -
Generally, use PdfStamper for adding to existing files and PdfWriter for building up files from scratch.
-
-# To concatenate files:
-
-* new Document
-* new PdfCopy with target stream
-* make new PdfReader for each source doc, iterating through the pages.
-* call GetImportedPage on the PdfCopy, passing it the reader for each source document.
+
PdfReader reader = new PdfReader(src);
+PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
+PdfContentByte canvas = stamper.getOverContent(1); // note: compare to 'getDirectContent()' method on PdfWriter
+ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
+stamper.close();
+reader.close();
 
-

java -Document document = new Document(); -PdfCopy copy = new PdfCopy(document, new FileOutputStream(“targetFile.pdf”)); -document.open(); -PdfReader reader; -int n; -for (int i = 0; i < files.length; i++) {

+

Generally, use PdfStamper for adding to existing files and PdfWriter for building up files from scratch.

+ +

To concatenate files:

-
reader = new PdfReader(files[i]);
-n = reader.getNumberOfPages();
-for (int page = 0; page < n; ) {
-    copy.addPage(copy.getImportedPage(reader, ++page));
-}
-copy.freeReader(reader);
-reader.close();
-
+ -

} -document.close();

1
 2
@@ -318,20 +289,48 @@ document.close();

7 8 9 -
PdfCopy does not copy forms and some other stuff.  See PdfSmartCopy.
-
-
-# To draw something onto the doc at a particular position:
-
-* subclass VerticalPositionMark.  It has a draw() method so you can add it to a Document object.
-* Imprement Draw interface, then wrap class in a chunk so you can add it to Document object.
-
-How to get the raw underlying pdf syntax:
+10
+11
+12
+13
+14
+15
+
Document document = new Document();
+PdfCopy copy = new PdfCopy(document, new FileOutputStream("targetFile.pdf"));
+document.open();
+PdfReader reader;
+int n;
+for (int i = 0; i < files.length; i++) {
+  reader = new PdfReader(files[i]);
+  n = reader.getNumberOfPages();
+  for (int page = 0; page < n; ) {
+      copy.addPage(copy.getImportedPage(reader, ++page));
+  }
+  copy.freeReader(reader);
+  reader.close();
+}
+document.close();
 
-

byte[] pdfStreamAsText = yourPdfReader.GetPageContent(1); -“`

+

PdfCopy does not copy forms and some other stuff. See PdfSmartCopy.

+ +

To draw something onto the doc at a particular position:

+ + + + +

How to get the raw underlying pdf syntax:

+ +
1
+
byte[] pdfStreamAsText = yourPdfReader.GetPageContent(1);
+
+ + + ]]> diff --git a/blog/2013/07/03/pdfs/index.html b/blog/2013/07/03/pdfs/index.html index 12e0883..d8535ec 100644 --- a/blog/2013/07/03/pdfs/index.html +++ b/blog/2013/07/03/pdfs/index.html @@ -163,169 +163,152 @@

Basic parts of PDF in iText’s object model:

4 5 6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22
[object number] 0 obj
 <<
 [metadata about object]
 >>
 [binary content in a stream]
 end obj
-
-In the PDF doc itself, the main parts are:
-
-* header
-* body
-* cross reference table (like dictionary for objects in the body)
-* trailer (locates cross reference table)
-
-Content layers, numbered 1 to 4:
-
-* 1 and 4 are for direct content
-* 2 and 3 are for higher level objects, like chunks, sections, chapters etc.
-* 1 is the bottom layer - under text and graphhics
-* 4 is the top layer- over text and graphics
-
-# Get writer for top layer:
 
-

PdfWriter.getDirectContent()

+

In the PDF doc itself, the main parts are:

+ + + + +

Content layers, numbered 1 to 4:

+ + + + +

Get writer for top layer:

1
-
Get writer for bottom layer:
+
PdfWriter.getDirectContent()
 
-

PdfWriter.getDirectContentUnder()

+

Get writer for bottom layer:

1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-16
-17
-
# Main classes for manipulating Pdf:
-
-* PdfWriter
-* PdfStamper
-* PdfImportedPage
-* PdfCopy
-
-
-# Reading docs
-To read PDF doc, use PdfReader.  Can tell you high level things about the doc like:
-
-* pages
-* rotation of pages
-* length
-* whether encrypted
-
-Get content stream of the page - the raw stream of the page with underlying PDF syntax:
+
PdfWriter.getDirectContentUnder()
 
-

var content stream = PdfReader.GetContentStream();

+

Main classes for manipulating Pdf:

+ + + + +

Reading docs

+ +

To read PDF doc, use PdfReader. Can tell you high level things about the doc like:

+ + + + +

Get content stream of the page - the raw stream of the page with underlying PDF syntax:

1
-2
-3
-4
-5
-
# How to extract a page from PDF:
-
-* DON'T attempt to extract a page using the PdfReader.
-* Pass the PdfReader to a PdfWriter and ask it to copy the page to another PDF.
-* This will give you a PdfImportedPage object.
+
var content stream = PdfReader.GetContentStream();
 
-

Document document = new Document(); -PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“extractPage.pdf”)); -document.open(); -PdfReader reader = new PdfReader(“sourceDocument.pdf”); -PdfImportedPage page = writer.getImportedPage(reader, 1); -// do something with it

+

How to extract a page from PDF:

+ + +
1
 2
 3
 4
 5
-
# To put a header and footer or stationary on a PDF:
+6
+7
+
Document document = new Document();
+PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("extractPage.pdf"));
+document.open();
+PdfReader reader = new PdfReader("sourceDocument.pdf");
+PdfImportedPage page = writer.getImportedPage(reader, 1);
+// do something with it
 
-* create the stationary in one PDF
-* import that page
-* superimpose that page on the target PDF
 
-

java -// open reader with source doc -// open target doc in writer -// create pdfContentByte by calling getDirectContent on the writer -// call addTemplate on the contentByte and feed it the imported page -PdfReader reader = new PdfReader(“stationary.pdf”); -Document document = new Document(); -PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“result.pdf”)); -document.open(); -PdfContentByte canvas = writer.getDirectContent(); -PdfImportedPage page;

+

To put a header and footer or stationary on a PDF:

-
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
-        page = writer.getImportedPage(reader, i);
-        canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
-    }
-
+ -

document.close(); -reader.close();

1
 2
 3
 4
 5
-
Problem with this: annotations are not part of the content stream, they are part of the annotations dictionary.
-They are not copied when using PdfImportedPage.
-
-
-# To stamp a page using PdfStamper:
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
// open reader with source doc
+// open target doc in writer
+// create pdfContentByte by calling getDirectContent on the writer
+// call addTemplate on the contentByte and feed it the imported page
+PdfReader reader = new PdfReader("stationary.pdf");
+Document document = new Document();
+PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("result.pdf"));
+document.open();
+PdfContentByte canvas = writer.getDirectContent();
+PdfImportedPage page;
+        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
+            page = writer.getImportedPage(reader, i);
+            canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
+        }
+document.close();
+reader.close();
 
-

java -PdfReader reader = new PdfReader(src); -PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); -PdfContentByte canvas = stamper.getOverContent(1); // note: compare to ‘getDirectContent()’ method on PdfWriter -ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(“Hello people!”), 36, 540, 0); -stamper.close(); -reader.close();

+

Problem with this: annotations are not part of the content stream, they are part of the annotations dictionary. +They are not copied when using PdfImportedPage.

+ +

To stamp a page using PdfStamper:

1
 2
@@ -333,38 +316,26 @@ 

Basic parts of PDF in iText’s object model:

4 5 6 -7 -8 -
Generally, use PdfStamper for adding to existing files and PdfWriter for building up files from scratch.
-
-# To concatenate files:
-
-* new Document
-* new PdfCopy with target stream
-* make new PdfReader for each source doc, iterating through the pages.
-* call GetImportedPage on the PdfCopy, passing it the reader for each source document.
+
PdfReader reader = new PdfReader(src);
+PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
+PdfContentByte canvas = stamper.getOverContent(1); // note: compare to 'getDirectContent()' method on PdfWriter
+ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
+stamper.close();
+reader.close();
 
-

java -Document document = new Document(); -PdfCopy copy = new PdfCopy(document, new FileOutputStream(“targetFile.pdf”)); -document.open(); -PdfReader reader; -int n; -for (int i = 0; i < files.length; i++) {

+

Generally, use PdfStamper for adding to existing files and PdfWriter for building up files from scratch.

-
reader = new PdfReader(files[i]);
-n = reader.getNumberOfPages();
-for (int page = 0; page < n; ) {
-    copy.addPage(copy.getImportedPage(reader, ++page));
-}
-copy.freeReader(reader);
-reader.close();
-
+

To concatenate files:

+ + -

} -document.close();

1
 2
@@ -375,20 +346,48 @@ 

Basic parts of PDF in iText’s object model:

7 8 9 -
PdfCopy does not copy forms and some other stuff.  See PdfSmartCopy.
-
-
-# To draw something onto the doc at a particular position:
-
-* subclass VerticalPositionMark.  It has a draw() method so you can add it to a Document object.
-* Imprement Draw interface, then wrap class in a chunk so you can add it to Document object.
-
-How to get the raw underlying pdf syntax:
+10
+11
+12
+13
+14
+15
+
Document document = new Document();
+PdfCopy copy = new PdfCopy(document, new FileOutputStream("targetFile.pdf"));
+document.open();
+PdfReader reader;
+int n;
+for (int i = 0; i < files.length; i++) {
+  reader = new PdfReader(files[i]);
+  n = reader.getNumberOfPages();
+  for (int page = 0; page < n; ) {
+      copy.addPage(copy.getImportedPage(reader, ++page));
+  }
+  copy.freeReader(reader);
+  reader.close();
+}
+document.close();
+
+ + +

PdfCopy does not copy forms and some other stuff. See PdfSmartCopy.

+ +

To draw something onto the doc at a particular position:

+ + + + +

How to get the raw underlying pdf syntax:

+ +
1
+
byte[] pdfStreamAsText = yourPdfReader.GetPageContent(1);
 
-

byte[] pdfStreamAsText = yourPdfReader.GetPageContent(1); -“`

+ diff --git a/blog/categories/-net-/atom.xml b/blog/categories/-net-/atom.xml index e8e03b8..d2b00e1 100644 --- a/blog/categories/-net-/atom.xml +++ b/blog/categories/-net-/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: .NET, | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/-net/atom.xml b/blog/categories/-net/atom.xml index 3ba3c2e..d605afb 100644 --- a/blog/categories/-net/atom.xml +++ b/blog/categories/-net/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: .net | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/asp-net/atom.xml b/blog/categories/asp-net/atom.xml index 6608670..90b832d 100644 --- a/blog/categories/asp-net/atom.xml +++ b/blog/categories/asp-net/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: ASP.NET | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/c-/atom.xml b/blog/categories/c-/atom.xml index a4189b9..712f499 100644 --- a/blog/categories/c-/atom.xml +++ b/blog/categories/c-/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: c#, | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/cheat-sheets/atom.xml b/blog/categories/cheat-sheets/atom.xml index 4b7b2f9..b636c73 100644 --- a/blog/categories/cheat-sheets/atom.xml +++ b/blog/categories/cheat-sheets/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: cheat-sheets | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/exocortex/atom.xml b/blog/categories/exocortex/atom.xml index e86a483..18dd751 100644 --- a/blog/categories/exocortex/atom.xml +++ b/blog/categories/exocortex/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: exocortex | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/howto/atom.xml b/blog/categories/howto/atom.xml index faadcca..a6757ec 100644 --- a/blog/categories/howto/atom.xml +++ b/blog/categories/howto/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: howto | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/lightswitch/atom.xml b/blog/categories/lightswitch/atom.xml index 482a0f3..87c4a63 100644 --- a/blog/categories/lightswitch/atom.xml +++ b/blog/categories/lightswitch/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: lightswitch | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/linux/atom.xml b/blog/categories/linux/atom.xml index 7c6b307..45a1df5 100644 --- a/blog/categories/linux/atom.xml +++ b/blog/categories/linux/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: linux | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/odata-/atom.xml b/blog/categories/odata-/atom.xml index 0acc184..f5359d2 100644 --- a/blog/categories/odata-/atom.xml +++ b/blog/categories/odata-/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: odata, | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/silverlight/atom.xml b/blog/categories/silverlight/atom.xml index 31caf69..caf43e4 100644 --- a/blog/categories/silverlight/atom.xml +++ b/blog/categories/silverlight/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: silverlight | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/underware/atom.xml b/blog/categories/underware/atom.xml index f61e2e4..6773f77 100644 --- a/blog/categories/underware/atom.xml +++ b/blog/categories/underware/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: underware | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/blog/categories/xaml-/atom.xml b/blog/categories/xaml-/atom.xml index 6cbd66e..3dd3c7b 100644 --- a/blog/categories/xaml-/atom.xml +++ b/blog/categories/xaml-/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: XAML, | Junk I Would Otherwise Forget]]> - 2013-07-06T15:25:40+10:00 + 2013-07-06T15:27:03+10:00 nickmiller.com.au/ diff --git a/index.html b/index.html index 61213b6..65fa5b0 100644 --- a/index.html +++ b/index.html @@ -207,169 +207,152 @@

Basic parts of PDF in iText’s object model:

4 5 6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22
[object number] 0 obj
 <<
 [metadata about object]
 >>
 [binary content in a stream]
 end obj
-
-In the PDF doc itself, the main parts are:
-
-* header
-* body
-* cross reference table (like dictionary for objects in the body)
-* trailer (locates cross reference table)
-
-Content layers, numbered 1 to 4:
-
-* 1 and 4 are for direct content
-* 2 and 3 are for higher level objects, like chunks, sections, chapters etc.
-* 1 is the bottom layer - under text and graphhics
-* 4 is the top layer- over text and graphics
-
-# Get writer for top layer:
 
-

PdfWriter.getDirectContent()

+

In the PDF doc itself, the main parts are:

+ +
    +
  • header
  • +
  • body
  • +
  • cross reference table (like dictionary for objects in the body)
  • +
  • trailer (locates cross reference table)
  • +
+ + +

Content layers, numbered 1 to 4:

+ +
    +
  • 1 and 4 are for direct content
  • +
  • 2 and 3 are for higher level objects, like chunks, sections, chapters etc.
  • +
  • 1 is the bottom layer - under text and graphhics
  • +
  • 4 is the top layer- over text and graphics
  • +
+ + +

Get writer for top layer:

1
-
Get writer for bottom layer:
+
PdfWriter.getDirectContent()
 
-

PdfWriter.getDirectContentUnder()

+

Get writer for bottom layer:

1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-16
-17
-
# Main classes for manipulating Pdf:
-
-* PdfWriter
-* PdfStamper
-* PdfImportedPage
-* PdfCopy
-
-
-# Reading docs
-To read PDF doc, use PdfReader.  Can tell you high level things about the doc like:
-
-* pages
-* rotation of pages
-* length
-* whether encrypted
-
-Get content stream of the page - the raw stream of the page with underlying PDF syntax:
+
PdfWriter.getDirectContentUnder()
 
-

var content stream = PdfReader.GetContentStream();

+

Main classes for manipulating Pdf:

+ +
    +
  • PdfWriter
  • +
  • PdfStamper
  • +
  • PdfImportedPage
  • +
  • PdfCopy
  • +
+ + +

Reading docs

+ +

To read PDF doc, use PdfReader. Can tell you high level things about the doc like:

+ +
    +
  • pages
  • +
  • rotation of pages
  • +
  • length
  • +
  • whether encrypted
  • +
+ + +

Get content stream of the page - the raw stream of the page with underlying PDF syntax:

1
-2
-3
-4
-5
-
# How to extract a page from PDF:
-
-* DON'T attempt to extract a page using the PdfReader.
-* Pass the PdfReader to a PdfWriter and ask it to copy the page to another PDF.
-* This will give you a PdfImportedPage object.
+
var content stream = PdfReader.GetContentStream();
 
-

Document document = new Document(); -PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“extractPage.pdf”)); -document.open(); -PdfReader reader = new PdfReader(“sourceDocument.pdf”); -PdfImportedPage page = writer.getImportedPage(reader, 1); -// do something with it

+

How to extract a page from PDF:

+ +
    +
  • DON’T attempt to extract a page using the PdfReader.
  • +
  • Pass the PdfReader to a PdfWriter and ask it to copy the page to another PDF.
  • +
  • This will give you a PdfImportedPage object.
  • +
+
1
 2
 3
 4
 5
-
# To put a header and footer or stationary on a PDF:
+6
+7
+
Document document = new Document();
+PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("extractPage.pdf"));
+document.open();
+PdfReader reader = new PdfReader("sourceDocument.pdf");
+PdfImportedPage page = writer.getImportedPage(reader, 1);
+// do something with it
 
-* create the stationary in one PDF
-* import that page
-* superimpose that page on the target PDF
 
-

java -// open reader with source doc -// open target doc in writer -// create pdfContentByte by calling getDirectContent on the writer -// call addTemplate on the contentByte and feed it the imported page -PdfReader reader = new PdfReader(“stationary.pdf”); -Document document = new Document(); -PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“result.pdf”)); -document.open(); -PdfContentByte canvas = writer.getDirectContent(); -PdfImportedPage page;

+

To put a header and footer or stationary on a PDF:

-
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
-        page = writer.getImportedPage(reader, i);
-        canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
-    }
-
+
    +
  • create the stationary in one PDF
  • +
  • import that page
  • +
  • superimpose that page on the target PDF
  • +
-

document.close(); -reader.close();

1
 2
 3
 4
 5
-
Problem with this: annotations are not part of the content stream, they are part of the annotations dictionary.
-They are not copied when using PdfImportedPage.
-
-
-# To stamp a page using PdfStamper:
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+
// open reader with source doc
+// open target doc in writer
+// create pdfContentByte by calling getDirectContent on the writer
+// call addTemplate on the contentByte and feed it the imported page
+PdfReader reader = new PdfReader("stationary.pdf");
+Document document = new Document();
+PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("result.pdf"));
+document.open();
+PdfContentByte canvas = writer.getDirectContent();
+PdfImportedPage page;
+        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
+            page = writer.getImportedPage(reader, i);
+            canvas.addTemplate(page, 1f, 0, 0, 1, 0, 0);
+        }
+document.close();
+reader.close();
 
-

java -PdfReader reader = new PdfReader(src); -PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); -PdfContentByte canvas = stamper.getOverContent(1); // note: compare to ‘getDirectContent()’ method on PdfWriter -ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(“Hello people!”), 36, 540, 0); -stamper.close(); -reader.close();

+

Problem with this: annotations are not part of the content stream, they are part of the annotations dictionary. +They are not copied when using PdfImportedPage.

+ +

To stamp a page using PdfStamper:

1
 2
@@ -377,38 +360,26 @@ 

Basic parts of PDF in iText’s object model:

4 5 6 -7 -8 -
Generally, use PdfStamper for adding to existing files and PdfWriter for building up files from scratch.
-
-# To concatenate files:
-
-* new Document
-* new PdfCopy with target stream
-* make new PdfReader for each source doc, iterating through the pages.
-* call GetImportedPage on the PdfCopy, passing it the reader for each source document.
+
PdfReader reader = new PdfReader(src);
+PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
+PdfContentByte canvas = stamper.getOverContent(1); // note: compare to 'getDirectContent()' method on PdfWriter
+ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
+stamper.close();
+reader.close();
 
-

java -Document document = new Document(); -PdfCopy copy = new PdfCopy(document, new FileOutputStream(“targetFile.pdf”)); -document.open(); -PdfReader reader; -int n; -for (int i = 0; i < files.length; i++) {

+

Generally, use PdfStamper for adding to existing files and PdfWriter for building up files from scratch.

-
reader = new PdfReader(files[i]);
-n = reader.getNumberOfPages();
-for (int page = 0; page < n; ) {
-    copy.addPage(copy.getImportedPage(reader, ++page));
-}
-copy.freeReader(reader);
-reader.close();
-
+

To concatenate files:

+ +
    +
  • new Document
  • +
  • new PdfCopy with target stream
  • +
  • make new PdfReader for each source doc, iterating through the pages.
  • +
  • call GetImportedPage on the PdfCopy, passing it the reader for each source document.
  • +
-

} -document.close();

1
 2
@@ -419,20 +390,48 @@ 

Basic parts of PDF in iText’s object model:

7 8 9 -
PdfCopy does not copy forms and some other stuff.  See PdfSmartCopy.
-
-
-# To draw something onto the doc at a particular position:
-
-* subclass VerticalPositionMark.  It has a draw() method so you can add it to a Document object.
-* Imprement Draw interface, then wrap class in a chunk so you can add it to Document object.
-
-How to get the raw underlying pdf syntax:
+10
+11
+12
+13
+14
+15
+
Document document = new Document();
+PdfCopy copy = new PdfCopy(document, new FileOutputStream("targetFile.pdf"));
+document.open();
+PdfReader reader;
+int n;
+for (int i = 0; i < files.length; i++) {
+  reader = new PdfReader(files[i]);
+  n = reader.getNumberOfPages();
+  for (int page = 0; page < n; ) {
+      copy.addPage(copy.getImportedPage(reader, ++page));
+  }
+  copy.freeReader(reader);
+  reader.close();
+}
+document.close();
+
+ + +

PdfCopy does not copy forms and some other stuff. See PdfSmartCopy.

+ +

To draw something onto the doc at a particular position:

+ +
    +
  • subclass VerticalPositionMark. It has a draw() method so you can add it to a Document object.
  • +
  • Imprement Draw interface, then wrap class in a chunk so you can add it to Document object.
  • +
+ + +

How to get the raw underlying pdf syntax:

+ +
1
+
byte[] pdfStreamAsText = yourPdfReader.GetPageContent(1);
 
-

byte[] pdfStreamAsText = yourPdfReader.GetPageContent(1); -“`

+ diff --git a/sitemap.xml b/sitemap.xml index f4c3acc..6f47a11 100644 --- a/sitemap.xml +++ b/sitemap.xml @@ -324,7 +324,7 @@ nickmiller.com.au/blog/2013/07/03/pdfs/ - 2013-07-06T15:25:31+10:00 + 2013-07-06T15:26:54+10:00 nickmiller.com.au/blog/2013/07/03/warm-underware/ @@ -332,10 +332,10 @@ nickmiller.com.au/blog/archives/ - 2013-07-06T15:25:31+10:00 + 2013-07-06T15:26:54+10:00 nickmiller.com.au/ - 2013-07-06T15:25:31+10:00 + 2013-07-06T15:26:54+10:00 \ No newline at end of file