4 changed files with 345 additions and 11 deletions
@ -0,0 +1,93 @@ |
|||
package co.jp.techsor.pdf; |
|||
|
|||
import com.itextpdf.kernel.pdf.*; |
|||
import com.itextpdf.layout.Document; |
|||
import com.itextpdf.layout.element.Paragraph; |
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* Test program for layer duplicate creation fix |
|||
*/ |
|||
public class TestLayerFix { |
|||
|
|||
public static void main(String[] args) { |
|||
try { |
|||
// Set test file paths
|
|||
String inputPdf = "test-input.pdf"; |
|||
String outputPdf1 = "test-output-1.pdf"; |
|||
String outputPdf2 = "test-output-2.pdf"; |
|||
|
|||
System.out.println("=== Layer Duplicate Fix Test Start ==="); |
|||
|
|||
// 1. Create test PDF
|
|||
createTestPdf(inputPdf); |
|||
System.out.println("1. Test PDF created: " + inputPdf); |
|||
|
|||
// 2. First run - add layers and content
|
|||
List<LayerDrawingOptions> drawingOptions1 = new ArrayList<>(); |
|||
drawingOptions1.add(LayerDrawingOptions.text(2, 100, 700, "First content (layer 2)")); |
|||
|
|||
PdfMemoLayerService service = new PdfMemoLayerService(); |
|||
service.addMemoLayers(inputPdf, outputPdf1, "Memo1", "Memo2", |
|||
true, true, drawingOptions1, false, 1); |
|||
System.out.println("2. First processing completed: " + outputPdf1); |
|||
|
|||
// 3. Second run - add more content to the same file
|
|||
List<LayerDrawingOptions> drawingOptions2 = new ArrayList<>(); |
|||
drawingOptions2.add(LayerDrawingOptions.text(2, 150, 650, "Second content (layer 2)")); |
|||
|
|||
service.addMemoLayers(outputPdf1, outputPdf2, "Memo1", "Memo2", |
|||
true, true, drawingOptions2, false, 1); |
|||
System.out.println("3. Second processing completed: " + outputPdf2); |
|||
|
|||
// 4. Check layer count in final PDF
|
|||
int layerCount = countLayers(outputPdf2); |
|||
System.out.println("\n=== Test Results ==="); |
|||
System.out.println("Final PDF layer count: " + layerCount); |
|||
|
|||
if (layerCount == 2) { |
|||
System.out.println("✅ Test passed: Layer count remains 2, no duplicates!"); |
|||
} else { |
|||
System.out.println("❌ Test failed: Layer count is " + layerCount + ", expected 2"); |
|||
} |
|||
|
|||
} catch (Exception e) { |
|||
System.err.println("Error during test: " + e.getMessage()); |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Create a simple test PDF |
|||
*/ |
|||
private static void createTestPdf(String filePath) throws IOException { |
|||
PdfDocument pdf = new PdfDocument(new PdfWriter(filePath)); |
|||
Document document = new Document(pdf); |
|||
|
|||
document.add(new Paragraph("Test PDF Document - Layer Fix Verification")); |
|||
document.add(new Paragraph("This is a test page for PDF layer functionality.")); |
|||
|
|||
document.close(); |
|||
} |
|||
|
|||
/** |
|||
* Count layers in PDF |
|||
*/ |
|||
private static int countLayers(String filePath) throws IOException { |
|||
try (PdfDocument pdf = new PdfDocument(new PdfReader(filePath))) { |
|||
PdfDictionary catalog = pdf.getCatalog().getPdfObject(); |
|||
PdfDictionary ocProps = catalog.getAsDictionary(new PdfName("OCProperties")); |
|||
|
|||
if (ocProps != null) { |
|||
PdfArray ocgs = ocProps.getAsArray(new PdfName("OCGs")); |
|||
if (ocgs != null) { |
|||
return ocgs.size(); |
|||
} |
|||
} |
|||
|
|||
return 0; // No layers
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
package co.jp.techsor.pdf; |
|||
|
|||
import com.itextpdf.kernel.pdf.*; |
|||
import com.itextpdf.layout.Document; |
|||
import com.itextpdf.layout.element.Paragraph; |
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 详细测试程序:模拟用户实际使用场景 |
|||
*/ |
|||
public class TestLayerFixDetailed { |
|||
|
|||
public static void main(String[] args) { |
|||
try { |
|||
// 模拟用户实际使用的图层名称
|
|||
String layer1Name = "メモ1"; |
|||
String layer2Name = "メモ2"; |
|||
|
|||
// 设置测试文件路径
|
|||
String testDir = "layer-test-results/"; |
|||
new File(testDir).mkdirs(); |
|||
|
|||
String inputPdf = testDir + "original.pdf"; |
|||
String outputPdf1 = testDir + "output-1.pdf"; |
|||
String outputPdf2 = testDir + "output-2.pdf"; |
|||
|
|||
System.out.println("=== 图层重复创建修复详细测试 ==="); |
|||
System.out.println("测试图层名称: " + layer1Name + " 和 " + layer2Name); |
|||
|
|||
// 1. 创建测试PDF
|
|||
createTestPdf(inputPdf); |
|||
System.out.println("\n1. 原始PDF创建完成: " + inputPdf); |
|||
System.out.println(" 初始图层数量: " + countLayers(inputPdf)); |
|||
|
|||
// 2. 第一次运行程序 - 添加图层和内容
|
|||
System.out.println("\n2. 第一次运行程序添加图层和内容..."); |
|||
List<LayerDrawingOptions> drawingOptions1 = new ArrayList<>(); |
|||
drawingOptions1.add(LayerDrawingOptions.text(2, 100, 700, "第一次添加的内容(图层2)")); |
|||
|
|||
PdfMemoLayerService service = new PdfMemoLayerService(); |
|||
service.addMemoLayers(inputPdf, outputPdf1, layer1Name, layer2Name, |
|||
true, true, drawingOptions1, false, 1); |
|||
|
|||
int layerCount1 = countLayers(outputPdf1); |
|||
System.out.println(" 第一次处理后图层数量: " + layerCount1); |
|||
System.out.println(" 预期图层数量: 2"); |
|||
|
|||
// 3. 第二次运行程序 - 在同一文件上添加更多内容
|
|||
System.out.println("\n3. 第二次运行程序添加更多内容..."); |
|||
List<LayerDrawingOptions> drawingOptions2 = new ArrayList<>(); |
|||
drawingOptions2.add(LayerDrawingOptions.text(2, 150, 650, "第二次添加的内容(图层2)")); |
|||
drawingOptions2.add(LayerDrawingOptions.text(1, 200, 600, "第二次添加的内容(图层1)")); |
|||
|
|||
service.addMemoLayers(outputPdf1, outputPdf2, layer1Name, layer2Name, |
|||
true, true, drawingOptions2, false, 1); |
|||
|
|||
int layerCount2 = countLayers(outputPdf2); |
|||
System.out.println(" 第二次处理后图层数量: " + layerCount2); |
|||
System.out.println(" 预期图层数量: 2"); |
|||
|
|||
// 4. 检查最终结果
|
|||
System.out.println("\n=== 测试结果汇总 ==="); |
|||
System.out.println("原始PDF图层数: " + countLayers(inputPdf)); |
|||
System.out.println("第一次处理后图层数: " + layerCount1); |
|||
System.out.println("第二次处理后图层数: " + layerCount2); |
|||
|
|||
if (layerCount1 == 2 && layerCount2 == 2) { |
|||
System.out.println("✅ 测试通过:图层数量始终保持为2个,没有重复创建!"); |
|||
System.out.println(" 最终文件:" + outputPdf2); |
|||
} else { |
|||
System.out.println("❌ 测试失败:图层数量不符合预期!"); |
|||
System.out.println(" 第一次处理后预期2个,实际" + layerCount1 + "个"); |
|||
System.out.println(" 第二次处理后预期2个,实际" + layerCount2 + "个"); |
|||
} |
|||
|
|||
} catch (Exception e) { |
|||
System.err.println("\n测试过程中发生错误: " + e.getMessage()); |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 创建一个简单的测试PDF |
|||
*/ |
|||
private static void createTestPdf(String filePath) throws IOException { |
|||
PdfDocument pdf = new PdfDocument(new PdfWriter(filePath)); |
|||
Document document = new Document(pdf); |
|||
|
|||
document.add(new Paragraph("原始PDF文档")); |
|||
document.add(new Paragraph("这是一个用于测试图层重复创建修复的文档。")); |
|||
document.add(new Paragraph("将通过程序两次添加图层和内容,检查是否会重复创建图层。")); |
|||
|
|||
document.close(); |
|||
} |
|||
|
|||
/** |
|||
* 统计PDF中的图层数量 |
|||
*/ |
|||
private static int countLayers(String filePath) throws IOException { |
|||
try (PdfDocument pdf = new PdfDocument(new PdfReader(filePath))) { |
|||
PdfDictionary catalog = pdf.getCatalog().getPdfObject(); |
|||
PdfDictionary ocProps = catalog.getAsDictionary(new PdfName("OCProperties")); |
|||
|
|||
if (ocProps != null) { |
|||
PdfArray ocgs = ocProps.getAsArray(new PdfName("OCGs")); |
|||
if (ocgs != null) { |
|||
return ocgs.size(); |
|||
} |
|||
} |
|||
|
|||
return 0; // No layers
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue