Java iText ปรับขนาดเอกสารเป็น A4
ฉันมีวิธีการต่อไปนี้ที่ "ปรับขนาด" หน้าทั้งหมดของเอกสารเป็นขนาดหน้า A4:
for (PdfDocument doc : pdfDocuments) {
int n = doc.getNumberOfPages();
for (int i = 1; i <= n; i++) {
PdfPage page = doc.getPage(i);
Rectangle media = page.getCropBox();
if (media == null) {
media = page.getMediaBox();
}
Rectangle crop = new Rectangle(0, 0, 210, 297);
page.setMediaBox(crop);
page.setCropBox(crop);
// The content, placed on a content stream before, will be rendered before the other content
// and, therefore, could be understood as a background (bottom "layer")
new PdfCanvas(page.newContentStreamBefore(),
page.getResources(), doc).writeLiteral("\nq 0.5 0 0 0.5 0 0 cm\nq\n");
// The content, placed on a content stream after, will be rendered after the other content
// and, therefore, could be understood as a foreground (top "layer")
new PdfCanvas(page.newContentStreamAfter(),
page.getResources(), doc).writeLiteral("\nQ\nQ\n");
}
}
อย่างไรก็ตามสิ่งนี้ไม่ได้ผลตามที่คาดไว้หน้าจะถูกเปลี่ยนเป็น A4 (297x210) แต่เนื้อหาไม่ได้อยู่ภายใน (ปรับขนาด) เนื้อหาจะถูกตัดออกเนื่องจากหน้าต้นฉบับมีขนาดใหญ่กว่า 297X210 ฉันจะแก้ไขปัญหานี้ได้อย่างไร?
คำตอบ
ในความคิดเห็นที่คุณชี้แจง
ฉันต้องการให้กรอบขอบเขตของเนื้อหาในอดีตถูกปรับขนาดและเพิ่มระยะขอบในเป้าหมาย
ดังนั้นก่อนอื่นเราต้องกำหนดกรอบขอบเขตของเนื้อหาหน้าเดิม สามารถทำได้โดยใช้MarginFinderคลาสจากคำตอบนี้ ระวัง: คลาสนั้นกำหนดกรอบขอบเขตของเนื้อหาทั้งหมดแม้ว่าจะเป็นเพียงสี่เหลี่ยมผืนผ้าสีขาวที่มองเห็นได้ไม่แตกต่างจากไม่มีเนื้อหาหรือสิ่งที่อยู่นอกกล่องครอบตัด ... หากกรณีการใช้งานของคุณต้องการคุณอาจต้องขยายคลาสนั้น เพื่อนำสถานการณ์ดังกล่าวมาพิจารณาด้วย
ด้วยการกำหนดกรอบขอบเขตเนื้อหาสิ่งที่ต้องทำคือการคำนวณเล็กน้อย
วิธีการต่อไปนี้กำหนดกรอบขอบเขตโดยใช้คลาสด้านบนแปลงเนื้อหาตามนั้นและเปลี่ยนกล่องครอบตัดผลลัพธ์
void scale(PdfDocument pdfDocument, Rectangle pageSize, Rectangle pageBodySize) {
int n = pdfDocument.getNumberOfPages();
for (int i = 1; i <= n; i++) {
PdfPage page = pdfDocument.getPage(i);
MarginFinder marginFinder = new MarginFinder();
PdfCanvasProcessor pdfCanvasProcessor = new PdfCanvasProcessor(marginFinder);
pdfCanvasProcessor.processPageContent(page);
Rectangle boundingBox = marginFinder.getBoundingBox();
if (boundingBox == null || boundingBox.getWidth() == 0 || boundingBox.getHeight() == 0) {
System.err.printf("Cannot scale page %d contents with bounding box %s\n", i , boundingBox);
continue;
} else {
// Scale and move content into A4 with margin
double scale = 0, xDiff= 0, yDiff = 0;
double xScale = pageBodySize.getWidth()/boundingBox.getWidth();
double yScale = pageBodySize.getHeight()/boundingBox.getHeight();
if (xScale < yScale) {
yDiff = boundingBox.getHeight() * (yScale / xScale - 1) / 2;
scale = xScale;
} else {
xDiff = boundingBox.getWidth() * (xScale / yScale - 1) / 2;
scale = yScale;
}
AffineTransform transform = AffineTransform.getTranslateInstance(pageBodySize.getLeft() + xDiff, pageBodySize.getBottom() + yDiff);
transform.scale(scale, scale);
transform.translate(-boundingBox.getLeft(), -boundingBox.getBottom());
new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDocument)
.concatMatrix(transform);
}
page.setMediaBox(pageSize);
page.setCropBox(pageSize);
}
}
( วิธีScaleToA4scale
)
สำหรับขนาดหน้าผลลัพธ์ A4 ที่มีขอบด้านละหนึ่งนิ้วคุณสามารถเรียกแบบนี้ว่าPdfDocument pdfDocument
:
Rectangle pageSize = PageSize.A4;
Rectangle pageBodySize = pageSize.clone().applyMargins(72, 72, 72, 72, false);
scale(pdfDocument, pageSize, pageBodySize);
(ตัดตอนมาจากการทดสอบScaleToA4testFdaRequiresUseOfEctdFormatAndStandardizedStudyDataInFutureRegulatorySubmissionsSept
)