Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PdfCopy to be used for writing new pages #1166

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,6 @@ public void close() {
}
}

public PdfIndirectReference add(PdfOutline outline) {
return null;
}

public void addAnnotation(PdfAnnotation annot) {
}

PdfIndirectReference add(PdfPage page, PdfContents contents) throws PdfException {
return null;
}

rasmusfaber marked this conversation as resolved.
Show resolved Hide resolved
public void freeReader(PdfReader reader) throws IOException {
indirectMap.remove(reader);
if (currentPdfReaderInstance != null) {
Expand Down
64 changes: 64 additions & 0 deletions openpdf/src/test/java/com/lowagie/text/pdf/PdfSmartCopyTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.lowagie.text.pdf;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Duration;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -43,4 +48,63 @@ private void check(File orig, int counter) {
});
}

@Test
void canWriteAndCopy() throws IOException {
try (PdfReader reader = new PdfReader("src/test/resources/pdfsmartcopy_bec.pdf")) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (Document document = new Document()) {
try (PdfCopy copy = new PdfSmartCopy(document, outputStream)) {
document.open();

document.add(new Paragraph("Front page"));
document.newPage();

int n = reader.getNumberOfPages();
for (int currentPage = 1; currentPage <= n; currentPage++) {
PdfImportedPage page = copy.getImportedPage(reader, currentPage);
copy.addPage(page);
}
copy.freeReader(reader);

document.newPage();
document.add(new Paragraph("Last page"));
}
}
try (PdfReader reader2 = new PdfReader(outputStream.toByteArray())) {
assertEquals(2 + reader.getNumberOfPages(), reader2.getNumberOfPages());
}
}
}

@Test
void canInterleaveTwoFilesWithCustomPages() throws IOException {
// Interleaves two PDF files with a custom page in front of each
try (PdfReader reader1 = new PdfReader("src/test/resources/pdfsmartcopy_bec.pdf");
PdfReader reader2 = new PdfReader("src/test/resources/HelloWorldMeta.pdf")
) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (Document document = new Document()) {
try (PdfCopy copy = new PdfSmartCopy(document, outputStream)) {
document.open();

for (int i = 1; i <= 10; i++) {
document.add(new Paragraph("Page " + i + " from document 1"));
document.newPage();
PdfImportedPage page1 = copy.getImportedPage(reader1, ((i - 1) % reader1.getNumberOfPages()) + 1);
copy.addPage(page1);
document.add(new Paragraph("Page " + i + " from document 2"));
document.newPage();
PdfImportedPage page2 = copy.getImportedPage(reader2, ((i - 1) % reader2.getNumberOfPages()) + 1);
copy.addPage(page2);
}

copy.freeReader(reader1);
copy.freeReader(reader2);
}
}
try (PdfReader resultReader = new PdfReader(outputStream.toByteArray())) {
assertEquals(40, resultReader.getNumberOfPages());
}
}
}
}