Java iText ölçek belgesini A4'e
Bir belgenin tüm sayfalarını A4 sayfa boyutlarına "yeniden boyutlandıran" aşağıdaki yönteme sahibim:
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");
}
}
Ancak, bu beklendiği gibi çalışmıyor, sayfalar A4'e (297x210) dönüştürülüyor ANCAK içerik içeri sığmıyor (ölçeklendirilmiş), orijinal sayfalar 297X210'dan daha büyük olduğu için içerik kesilmiş görünüyor. Bunu nasıl düzeltebilirim?
Yanıtlar
Bir yorumda açıkladın
Önceki içeriğin sınırlayıcı kutusunun ölçeklenmesini ve hedefe bir marj eklenmesini istiyorum
Bu nedenle, öncelikle orijinal sayfa içeriğinin sınırlayıcı kutusunu belirlememiz gerekiyor . Bu, bu cevaptakiMarginFinder sınıf kullanılarak yapılabilir . Dikkat: Bu sınıf , görsel olarak hiçbir içerikten veya daha önce kırpma kutusunun dışında kalan bir şeyden farklı olmayan beyaz bir dikdörtgen olsa bile , tüm içeriğin sınırlayıcı kutusunu belirler ... Kullanım durumunuz gerektiriyorsa, bu sınıfı genişletmeniz gerekebilir bu tür durumları da dikkate almak.
İçerik sınırlama kutusu belirlendiğinde geriye kalan tek şey biraz hesaplama yapmaktır.
Aşağıdaki yöntem, yukarıdaki sınıfı kullanarak sınırlayıcı kutuyu belirler, içeriği buna göre dönüştürür ve sonuç kırpma kutusunu değiştirir.
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);
}
}
( ScaleToA4 yöntemi scale)
Her iki tarafında birer inç kenar boşluğuna sahip bir A4 sonuç sayfası boyutu için, aşağıdaki şekilde adlandırabilirsiniz PdfDocument pdfDocument:
Rectangle pageSize = PageSize.A4;
Rectangle pageBodySize = pageSize.clone().applyMargins(72, 72, 72, 72, false);
scale(pdfDocument, pageSize, pageBodySize);
( ScaleToA4 testinden alıntı testFdaRequiresUseOfEctdFormatAndStandardizedStudyDataInFutureRegulatorySubmissionsSept)