• iText实战--在现有PDF上工作


    6.1 使用PdfReader读取PDF

    检索文档和页面信息

    D:/data/iText/inAction/chapter03/image_direct.pdf
    Number of pages: 1
    Size of page 1: [0.0,0.0,283.0,416.0]
    Rotation of page 1: 0
    Page size with rotation of page 1: Rectangle: 283.0x416.0 (rot: 0 degrees)
    Is rebuilt? false
    Is encrypted? false

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Rectangle;
    6. import com.itextpdf.text.pdf.PdfReader;
    7. public class PageInformation {
    8. /** The resulting text file with info about a PDF. */
    9. public static final String RESULT
    10. = "D:/data/iText/inAction/chapter06/page_info.txt";
    11. /**
    12. * Main method.
    13. * @param args no arguments needed
    14. * @throws DocumentException
    15. * @throws IOException
    16. */
    17. public static void main(String[] args)
    18. throws DocumentException, IOException {
    19. // Inspecting PDFs
    20. PrintWriter writer = new PrintWriter(new FileOutputStream(RESULT));
    21. inspect(writer, "D:/data/iText/inAction/chapter03/image_direct.pdf");
    22. writer.close();
    23. }
    24. /**
    25. * Inspect a PDF file and write the info to a txt file
    26. * @param writer Writer to a text file
    27. * @param filename Path to the PDF file
    28. * @throws IOException
    29. */
    30. public static void inspect(PrintWriter writer, String filename)
    31. throws IOException {
    32. PdfReader reader = new PdfReader(filename);
    33. writer.println(filename);
    34. writer.print("Number of pages: ");
    35. writer.println(reader.getNumberOfPages());
    36. Rectangle mediabox = reader.getPageSize(1);
    37. writer.print("Size of page 1: [");
    38. writer.print(mediabox.getLeft());
    39. writer.print(',');
    40. writer.print(mediabox.getBottom());
    41. writer.print(',');
    42. writer.print(mediabox.getRight());
    43. writer.print(',');
    44. writer.print(mediabox.getTop());
    45. writer.println("]");
    46. writer.print("Rotation of page 1: ");
    47. writer.println(reader.getPageRotation(1));
    48. writer.print("Page size with rotation of page 1: ");
    49. writer.println(reader.getPageSizeWithRotation(1));
    50. writer.print("Is rebuilt? ");
    51. writer.println(reader.isRebuilt());
    52. writer.print("Is encrypted? ");
    53. writer.println(reader.isEncrypted());
    54. writer.println();
    55. writer.flush();
    56. }
    57. }

    Page Size 页面大小

    损坏的PDF

    加密的PDF

    使用PdfReader降低内存

    部分读取

    1. /**
    2. * Do a full read of a PDF file
    3. * @param writer a writer to a report file
    4. * @param filename the file to read
    5. * @throws IOException
    6. */
    7. public static void fullRead(PrintWriter writer, String filename)
    8. throws IOException {
    9. long before = getMemoryUse();
    10. PdfReader reader = new PdfReader(filename);
    11. reader.getNumberOfPages();
    12. writer.println(String.format("Memory used by full read: %d",
    13. getMemoryUse() - before));
    14. writer.flush();
    15. }
    16. /**
    17. * Do a partial read of a PDF file
    18. * @param writer a writer to a report file
    19. * @param filename the file to read
    20. * @throws IOException
    21. */
    22. public static void partialRead(PrintWriter writer, String filename)
    23. throws IOException {
    24. long before = getMemoryUse();
    25. PdfReader reader = new PdfReader(
    26. new RandomAccessFileOrArray(filename), null);
    27. reader.getNumberOfPages();
    28. writer.println(String.format("Memory used by partial read: %d",
    29. getMemoryUse() - before));
    30. writer.flush();
    31. }

    选择页面

    PdfReader.selectPages("3");

    PdfReader.selectPages("4-8");

    执行selectPages()后,页数就变成选中的实际页数,要注意越界。

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.pdf.PdfCopy;
    6. import com.itextpdf.text.pdf.PdfReader;
    7. import com.itextpdf.text.pdf.PdfStamper;
    8. public class SelectPages {
    9. /** A resulting PDF file. */
    10. public static final String RESULT1 = "results/part2/chapter06/timetable_stamper.pdf";
    11. /** A resulting PDF file. */
    12. public static final String RESULT2 = "results/part2/chapter06/timetable_copy.pdf";
    13. /**
    14. * Main method.
    15. * @param args no arguments needed
    16. * @throws DocumentException
    17. * @throws IOException
    18. */
    19. public static void main(String[] args) throws IOException, DocumentException {
    20. PdfReader reader = new PdfReader("D:/data/iText/inAction/chapter03/movie_posters.pdf");
    21. reader.selectPages("4-8");
    22. manipulateWithStamper(reader);
    23. manipulateWithCopy(reader);
    24. }
    25. /**
    26. * Creates a new PDF based on the one in the reader
    27. * @param reader a reader with a PDF file
    28. * @throws IOException
    29. * @throws DocumentException
    30. */
    31. private static void manipulateWithStamper(PdfReader reader)
    32. throws IOException, DocumentException {
    33. PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT1));
    34. stamper.close();
    35. }
    36. /**
    37. * Creates a new PDF based on the one in the reader
    38. * @param reader a reader with a PDF file
    39. * @throws IOException
    40. * @throws DocumentException
    41. */
    42. private static void manipulateWithCopy(PdfReader reader)
    43. throws IOException, DocumentException {
    44. int n = reader.getNumberOfPages();
    45. Document document = new Document();
    46. PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT2));
    47. document.open();
    48. for (int i = 0; i < n;) {
    49. copy.addPage(copy.getImportedPage(reader, ++i));
    50. }
    51. document.close();
    52. }
    53. }

    6.2 从PDF拷贝页面

    导入页面

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Image;
    6. import com.itextpdf.text.pdf.PdfImportedPage;
    7. import com.itextpdf.text.pdf.PdfPTable;
    8. import com.itextpdf.text.pdf.PdfReader;
    9. import com.itextpdf.text.pdf.PdfWriter;
    10. public class ImportingPages1 {
    11. /** The resulting PDF file. */
    12. public static final String RESULT
    13. = "D:/data/iText/inAction/chapter06/time_table_imported1.pdf";
    14. /**
    15. * Main method.
    16. * @param args no arguments needed
    17. * @throws DocumentException
    18. * @throws IOException
    19. */
    20. public static void main(String[] args)
    21. throws IOException, DocumentException {
    22. // step 1
    23. Document document = new Document();
    24. // step 2
    25. PdfWriter writer
    26. = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    27. // step 3
    28. document.open();
    29. // step 4
    30. PdfPTable table = new PdfPTable(2);
    31. PdfReader reader = new PdfReader("D:/data/iText/inAction/chapter03/movie_posters.pdf");
    32. int n = reader.getNumberOfPages();
    33. PdfImportedPage page;
    34. for (int i = 1; i <= n; i++) {
    35. page = writer.getImportedPage(reader, i);
    36. table.addCell(Image.getInstance(page));
    37. }
    38. document.add(table);
    39. // step 5
    40. document.close();
    41. }
    42. }

    缩放和叠加页面

    叠加PDF页面

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.Element;
    6. import com.itextpdf.text.Font;
    7. import com.itextpdf.text.Image;
    8. import com.itextpdf.text.PageSize;
    9. import com.itextpdf.text.Paragraph;
    10. import com.itextpdf.text.Font.FontFamily;
    11. import com.itextpdf.text.pdf.BaseFont;
    12. import com.itextpdf.text.pdf.PdfContentByte;
    13. import com.itextpdf.text.pdf.PdfGState;
    14. import com.itextpdf.text.pdf.PdfImportedPage;
    15. import com.itextpdf.text.pdf.PdfReader;
    16. import com.itextpdf.text.pdf.PdfWriter;
    17. public class Layers {
    18. /** The resulting PDF. */
    19. public static final String SOURCE
    20. = "D:/data/iText/inAction/chapter06/layers_orig.pdf";
    21. /** The resulting PDF. */
    22. public static final String RESULT
    23. = "D:/data/iText/inAction/chapter06/layers.pdf";
    24. /** The movie poster. */
    25. public static final String RESOURCE
    26. = "E:/study/PDF/SourceCodeiText/itext-book/book/resources/img/loa.jpg";
    27. /**
    28. * Main method.
    29. * @param args no arguments needed
    30. * @throws DocumentException
    31. * @throws IOException
    32. */
    33. public static void main(String[] args)
    34. throws IOException, DocumentException {
    35. new Layers().createPdf(SOURCE);
    36. // Create a reader
    37. PdfReader reader = new PdfReader(SOURCE);
    38. // step 1
    39. Document document = new Document(PageSize.A5.rotate());
    40. // step 2
    41. PdfWriter writer
    42. = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    43. // step 3
    44. document.open();
    45. // step 4
    46. PdfContentByte canvas = writer.getDirectContent();
    47. PdfImportedPage page;
    48. BaseFont bf
    49. = BaseFont.createFont(BaseFont.ZAPFDINGBATS, "", BaseFont.EMBEDDED);
    50. for (int i = 0; i < reader.getNumberOfPages(); ) {
    51. page = writer.getImportedPage(reader, ++i);
    52. canvas.addTemplate(page, 1f, 0, 0.4f, 0.4f, 72, 50 * i);
    53. canvas.beginText();
    54. canvas.setFontAndSize(bf, 20);
    55. canvas.showTextAligned(Element.ALIGN_CENTER,
    56. String.valueOf((char)(181 + i)), 496, 150 + 50 * i, 0);
    57. canvas.endText();
    58. }
    59. // step 5
    60. document.close();
    61. }
    62. /**
    63. * Creates a PDF document.
    64. * @param filename the path to the new PDF document
    65. * @throws DocumentException
    66. * @throws IOException
    67. */
    68. public void createPdf(String filename)
    69. throws IOException, DocumentException {
    70. // step 1
    71. Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
    72. // step 2
    73. PdfWriter writer = PdfWriter.getInstance(document,
    74. new FileOutputStream(filename));
    75. // step 3
    76. document.open();
    77. // step 4
    78. PdfContentByte under = writer.getDirectContentUnder();
    79. // Page 1: a rectangle
    80. drawRectangle(under, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
    81. under.setRGBColorFill(0xFF, 0xD7, 0x00);
    82. under.rectangle(5, 5, PageSize.POSTCARD.getWidth() - 10, PageSize.POSTCARD.getHeight() - 10);
    83. under.fill();
    84. document.newPage();
    85. // Page 2: an image
    86. drawRectangle(under, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
    87. Image img = Image.getInstance(RESOURCE);
    88. img.setAbsolutePosition((PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
    89. (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
    90. document.add(img);
    91. document.newPage();
    92. // Page 3: the words "Foobar Film Festival"
    93. drawRectangle(under, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());;
    94. Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
    95. p.setAlignment(Element.ALIGN_CENTER);
    96. document.add(p);
    97. document.newPage();
    98. // Page 4: the words "SOLD OUT"
    99. drawRectangle(under, PageSize.POSTCARD.getWidth(), PageSize.POSTCARD.getHeight());
    100. PdfContentByte over = writer.getDirectContent();
    101. over.saveState();
    102. float sinus = (float)Math.sin(Math.PI / 60);
    103. float cosinus = (float)Math.cos(Math.PI / 60);
    104. BaseFont bf = BaseFont.createFont();
    105. over.beginText();
    106. over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
    107. over.setLineWidth(1.5f);
    108. over.setRGBColorStroke(0xFF, 0x00, 0x00);
    109. over.setRGBColorFill(0xFF, 0xFF, 0xFF);
    110. over.setFontAndSize(bf, 36);
    111. over.setTextMatrix(cosinus, sinus, -sinus, cosinus, 50, 324);
    112. over.showText("SOLD OUT");
    113. over.setTextMatrix(0, 0);
    114. over.endText();
    115. over.restoreState();
    116. // step 5
    117. document.close();
    118. }
    119. /**
    120. * Draws a rectangle
    121. * @param content the direct content layer
    122. * @param width the width of the rectangle
    123. * @param height the height of the rectangle
    124. */
    125. public static void drawRectangle(PdfContentByte content, float width, float height) {
    126. content.saveState();
    127. PdfGState state = new PdfGState();
    128. state.setFillOpacity(0.6f);
    129. content.setGState(state);
    130. content.setRGBColorFill(0xFF, 0xFF, 0xFF);
    131. content.setLineWidth(3);
    132. content.rectangle(0, 0, width, height);
    133. content.fillStroke();
    134. content.restoreState();
    135. }
    136. }

    导入公司信封

    从第N页复制页面

    6.3 使用PdfStamper添加内容

    在绝对位置添加内容

    PdfStamper.getOverContent() 类似 getDirectContent()

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.DocumentException;
    4. import com.itextpdf.text.Element;
    5. import com.itextpdf.text.Phrase;
    6. import com.itextpdf.text.pdf.ColumnText;
    7. import com.itextpdf.text.pdf.PdfContentByte;
    8. import com.itextpdf.text.pdf.PdfReader;
    9. import com.itextpdf.text.pdf.PdfStamper;
    10. import com.ygsoft.abc.component.cdes.itext.chapter1.HelloWorldLandscape1;
    11. import com.ygsoft.abc.component.cdes.itext.chapter1.HelloWorldLandscape2;
    12. public class StampText {
    13. /** A resulting PDF file. */
    14. public static final String RESULT1
    15. = "D:/data/iText/inAction/chapter06/hello1.pdf";
    16. /** A resulting PDF file. */
    17. public static final String RESULT2
    18. = "D:/data/iText/inAction/chapter06/hello2.pdf";
    19. /** A resulting PDF file. */
    20. public static final String RESULT3
    21. = "D:/data/iText/inAction/chapter06/hello3.pdf";
    22. /**
    23. * Main method.
    24. * @param args no arguments needed
    25. * @throws DocumentException
    26. * @throws IOException
    27. */
    28. public static void main(String[] args)
    29. throws DocumentException, IOException {
    30. HelloWorldLandscape1.main(args);
    31. HelloWorldLandscape2.main(args);
    32. stamp(HelloWorldLandscape1.RESULT, RESULT1);
    33. stampIgnoreRotation(HelloWorldLandscape1.RESULT, RESULT2);
    34. stamp(HelloWorldLandscape2.RESULT, RESULT3);
    35. }
    36. /**
    37. * Manipulates a PDF file src with the file dest as result
    38. * @param src the original PDF
    39. * @param dest the resulting PDF
    40. * @throws IOException
    41. * @throws DocumentException
    42. */
    43. public static void stamp(String src, String dest)
    44. throws IOException, DocumentException {
    45. PdfReader reader = new PdfReader(src);
    46. PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    47. PdfContentByte canvas = stamper.getOverContent(1);
    48. ColumnText.showTextAligned(canvas,
    49. Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
    50. stamper.close();
    51. }
    52. /**
    53. * Manipulates a PDF file src with the file dest as result
    54. * @param src the original PDF
    55. * @param dest the resulting PDF
    56. * @throws IOException
    57. * @throws DocumentException
    58. */
    59. public static void stampIgnoreRotation(String src, String dest)
    60. throws IOException, DocumentException {
    61. PdfReader reader = new PdfReader(src);
    62. PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    63. stamper.setRotateContents(false);
    64. PdfContentByte canvas = stamper.getOverContent(1);
    65. ColumnText.showTextAligned(canvas,
    66. Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
    67. stamper.close();
    68. }
    69. }

    2步创建PDF

    第一步,创建文档内容,第二步,添加页码

    1. import java.io.ByteArrayOutputStream;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.sql.SQLException;
    5. import com.itextpdf.text.Document;
    6. import com.itextpdf.text.DocumentException;
    7. import com.itextpdf.text.Element;
    8. import com.itextpdf.text.PageSize;
    9. import com.itextpdf.text.Rectangle;
    10. import com.itextpdf.text.pdf.PdfPTable;
    11. import com.itextpdf.text.pdf.PdfReader;
    12. import com.itextpdf.text.pdf.PdfStamper;
    13. import com.itextpdf.text.pdf.PdfWriter;
    14. public class TwoPasses {
    15. /** The resulting PDF file. */
    16. public static final String RESULT
    17. = "results/part2/chapter06/page_x_of_y.pdf";
    18. /**
    19. * Main method.
    20. * @param args no arguments needed
    21. * @throws DocumentException
    22. * @throws IOException
    23. * @throws SQLException
    24. * @throws SQLException
    25. */
    26. public static void main(String[] args)
    27. throws SQLException, DocumentException, IOException {
    28. // FIRST PASS, CREATE THE PDF WITHOUT HEADER
    29. // step 1
    30. Document document = new Document(PageSize.A4, 36, 36, 54, 36);
    31. // step 2
    32. ByteArrayOutputStream baos = new ByteArrayOutputStream();
    33. PdfWriter.getInstance(document, baos);
    34. // step 3
    35. document.open();
    36. // step 4
    37. // PDF文档创建...
    38. // step 5
    39. document.close();
    40. // SECOND PASS, ADD THE HEADER
    41. // Create a reader
    42. PdfReader reader = new PdfReader(baos.toByteArray());
    43. // Create a stamper
    44. PdfStamper stamper
    45. = new PdfStamper(reader, new FileOutputStream(RESULT));
    46. // Loop over the pages and add a header to each page
    47. int n = reader.getNumberOfPages();
    48. for (int i = 1; i <= n; i++) {
    49. getHeaderTable(i, n).writeSelectedRows(
    50. 0, -1, 34, 803, stamper.getOverContent(i));
    51. }
    52. // Close the stamper
    53. stamper.close();
    54. }
    55. /**
    56. * Create a header table with page X of Y
    57. * @param x the page number
    58. * @param y the total number of pages
    59. * @return a table that can be used as header
    60. */
    61. public static PdfPTable getHeaderTable(int x, int y) {
    62. PdfPTable table = new PdfPTable(2);
    63. table.setTotalWidth(527);
    64. table.setLockedWidth(true);
    65. table.getDefaultCell().setFixedHeight(20);
    66. table.getDefaultCell().setBorder(Rectangle.BOTTOM);
    67. table.addCell("FOOBAR FILMFESTIVAL");
    68. table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
    69. table.addCell(String.format("Page %d of %d", x, y));
    70. return table;
    71. }
    72. }

    添加公司信封到一个存在的文档

    1. /**
    2. * Manipulates a PDF file src with the file dest as result
    3. * @param src the original PDF
    4. * @param stationery a PDF that will be added as background
    5. * @param dest the resulting PDF
    6. * @throws IOException
    7. * @throws DocumentException
    8. */
    9. public void manipulatePdf(String src, String stationery, String dest)
    10. throws IOException, DocumentException {
    11. // Create readers
    12. PdfReader reader = new PdfReader(src);
    13. PdfReader s_reader = new PdfReader(stationery);
    14. // Create the stamper
    15. PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    16. // Add the stationery to each page
    17. PdfImportedPage page = stamper.getImportedPage(s_reader, 1);
    18. int n = reader.getNumberOfPages();
    19. PdfContentByte background;
    20. for (int i = 1; i <= n; i++) {
    21. background = stamper.getUnderContent(i);
    22. background.addTemplate(page, 0, 0);
    23. }
    24. // CLose the stamper
    25. stamper.close();
    26. }

    插入页面到一个存在的文档

    填充PDF表单

    6.4 使用PdfCopy 拷贝页面

    拼接和拆分PDF文档

    拼接文档

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import java.sql.SQLException;
    4. import com.itextpdf.text.Document;
    5. import com.itextpdf.text.DocumentException;
    6. import com.itextpdf.text.Element;
    7. import com.itextpdf.text.Phrase;
    8. import com.itextpdf.text.pdf.ColumnText;
    9. import com.itextpdf.text.pdf.PdfCopy;
    10. import com.itextpdf.text.pdf.PdfImportedPage;
    11. import com.itextpdf.text.pdf.PdfReader;
    12. public class ConcatenateStamp {
    13. /** The resulting PDF file. */
    14. public static final String RESULT
    15. = "results/part2/chapter06/concatenated_stamped.pdf";
    16. /**
    17. * Main method.
    18. * @param args no arguments needed
    19. * @throws DocumentException
    20. * @throws IOException
    21. * @throws SQLException
    22. */
    23. public static void main(String[] args)
    24. throws IOException, DocumentException, SQLException {
    25. // use old examples to create PDFs
    26. MovieLinks1.main(args);
    27. MovieHistory.main(args);
    28. // step 1
    29. Document document = new Document();
    30. // step 2
    31. PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
    32. // step 3
    33. document.open();
    34. // step 4
    35. // reader for document 1
    36. PdfReader reader1 = new PdfReader(MovieLinks1.RESULT);
    37. int n1 = reader1.getNumberOfPages();
    38. // reader for document 2
    39. PdfReader reader2 = new PdfReader(MovieHistory.RESULT);
    40. int n2 = reader2.getNumberOfPages();
    41. // initializations
    42. PdfImportedPage page;
    43. PdfCopy.PageStamp stamp;
    44. // Loop over the pages of document 1
    45. for (int i = 0; i < n1; ) {
    46. page = copy.getImportedPage(reader1, ++i);
    47. stamp = copy.createPageStamp(page);
    48. // add page numbers
    49. ColumnText.showTextAligned(
    50. stamp.getUnderContent(), Element.ALIGN_CENTER,
    51. new Phrase(String.format("page %d of %d", i, n1 + n2)),
    52. 297.5f, 28, 0);
    53. stamp.alterContents();
    54. copy.addPage(page);
    55. }
    56. // Loop over the pages of document 2
    57. for (int i = 0; i < n2; ) {
    58. page = copy.getImportedPage(reader2, ++i);
    59. stamp = copy.createPageStamp(page);
    60. // add page numbers
    61. ColumnText.showTextAligned(
    62. stamp.getUnderContent(), Element.ALIGN_CENTER,
    63. new Phrase(String.format("page %d of %d", n1 + i, n1 + n2)),
    64. 297.5f, 28, 0);
    65. stamp.alterContents();
    66. copy.addPage(page);
    67. }
    68. // step 5
    69. document.close();
    70. }
    71. }

    拆分文档

    1. import java.io.FileOutputStream;
    2. import java.io.IOException;
    3. import com.itextpdf.text.Document;
    4. import com.itextpdf.text.DocumentException;
    5. import com.itextpdf.text.pdf.PdfCopy;
    6. import com.itextpdf.text.pdf.PdfReader;
    7. public class Burst {
    8. /** Format of the resulting PDF files. */
    9. public static final String RESULT
    10. = "D:/data/iText/inAction/chapter06/timetable_p%d.pdf";
    11. /**
    12. * Main method.
    13. * @param args no arguments needed
    14. * @throws DocumentException
    15. * @throws IOException
    16. */
    17. public static void main(String[] args)
    18. throws IOException, DocumentException {
    19. // Create a reader
    20. PdfReader reader = new PdfReader("D:/data/iText/inAction/chapter03/movie_posters.pdf");
    21. // We'll create as many new PDFs as there are pages
    22. Document document;
    23. PdfCopy copy;
    24. // loop over all the pages in the original PDF
    25. int n = reader.getNumberOfPages();
    26. for (int i = 0; i < n; ) {
    27. // step 1
    28. document = new Document();
    29. // step 2
    30. copy = new PdfCopy(document,
    31. new FileOutputStream(String.format(RESULT, ++i)));
    32. // step 3
    33. document.open();
    34. // step 4
    35. copy.addPage(copy.getImportedPage(reader, i));
    36. // step 5
    37. document.close();
    38. }
    39. }
    40. }

    PdfCopy VS PdfSmartCopy

    PdfSmartCopy 继承自PdfCopy,其会检查每页添加的冗余对象, 因此可以节省大量磁盘空间或

    带宽。这种额外的“智慧”是要付出代价的。PdfSmartCopy 需要更多的内存和时间去拼接文档。

    文件大小、带宽优先,选PdfSmartCopy

    内存、时间优先,选PdfCopy

    拼接表单

  • 相关阅读:
    数仓-oltp和olap
    基于JavaWeb+SpringBoot+Vue电子商城微信小程序系统的设计和实现
    python结合Airtest框架APP自动化
    JS起源与简介
    web漏洞-PHP反序列化
    threejs(3)-详解材质与纹理
    C语言日记 33 构造函数
    生产依赖与开发依赖区别: 前端程序没有区别,后端程序有点区别
    nodejs切换
    左支座零件的机械加工工艺规程及工艺装备设计【计算机辅助设计与制造CAD】
  • 原文地址:https://blog.csdn.net/gqltt/article/details/132920635