我很高兴在Android PDF类PdfDocument中制作PDF文档。在迷惑了"微妙的"文档我认为坐标系是PostScript(1/72")
private static final int A4_WIDTH = 595;
private static final int A4_HEIGHT = 842;
private static final int CM = 28;
private static final int footer_height = CM;
void startPage() {
if (currentPage != null) {
finishPage(currentPage);
}
PageInfo.Builder pageBuilder = new PageInfo.Builder(A4_WIDTH, A4_HEIGHT, page++);
Rect rect = new Rect(0, 0, A4_WIDTH, A4_HEIGHT);
rect.inset(CM, 2 * CM);
pageBuilder.setContentRect(rect);
PageInfo pageInfo = pageBuilder.create();
pagePosition = 0.0f;
pageWidth = rect.width();
pageHeight = rect.height();
currentPage = super.startPage(pageInfo);
page_count++;
if (has_footer)
putFooter();
}
这在我的Nexus 10上工作得很好,但是当我在三星S6上时坐标系看起来很乱,我感到非常惊讶,所以在Nexus 10上我把文字放在0,0并且它很适合2厘米向下和1厘米对面,但是在S6上它显示4厘米向下和2厘米横跨。此外,底部和右部被夹住2cm和1cm。换句话说,剪切矩形位于正确的位置,但看起来坐标系已经双重偏移。
我的绘制代码如下所示:
canvas.save();
canvas.translate(0, pagePosition);
staticLayout.draw(canvas);
canvas.restore();
其中pagePosition是一个简单的ps计数器,通过测量staticLayout.getHeight()的结果来驱动;
要明确的是,在相当复杂的文本,线条图和图像混合上没有缺陷的情况下布局很明显,只是得到了这种依赖于设备的缺陷。
虫子还是我的大脑?
编辑:左边是Nexus,右边是S6
生产代码:
import android.annotation.TargetApi;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.pdf.PdfDocument;
import android.support.v4.content.ContextCompat;
import uk.co.spennycycles.pocketcycletech.PocketCycleTechApp;
import uk.co.spennycycles.pocketcycletech.R;
@TargetApi(19)
public class TestReport extends PdfDocument {
private static final String TAG = SpennyPdfDocument.class.getSimpleName();
private static final int A4_WIDTH = 595;
private static final int A4_HEIGHT = 842;
private static final int CM = 28;
int pageWidth;
int pageHeight;
public void generateReport() {
PageInfo.Builder pageBuilder = new PageInfo.Builder(A4_WIDTH, A4_HEIGHT, 1);
Rect rect = new Rect(0, 0, A4_WIDTH, A4_HEIGHT);
rect.inset(CM, 2 * CM);
pageBuilder.setContentRect(rect);
PageInfo pageInfo = pageBuilder.create();
Page page = super.startPage(pageInfo);
pageWidth = rect.width();
pageHeight = rect.height();
Canvas canvas = page.getCanvas();
RectF r = new RectF((float) 0, 0, pageWidth, pageHeight);
r.inset(1.0f, 1.0f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(ContextCompat.getColor(PocketCycleTechApp.getContext(), R.color.lt_gray));
canvas.drawRect(r, paint);
paint.setColor(ContextCompat.getColor(PocketCycleTechApp.getContext(), R.color.black));
paint.setStrokeWidth(2.0f);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(r, paint);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawLine(r.left, r.top, r.right, r.bottom, paint);
canvas.drawLine(r.right, r.top, r.left, r.bottom, paint);
finishPage(page);
}
}
答案 0 :(得分:0)
解决方法:
currentPage = super.startPage(pageInfo);
Canvas canvas = currentPage.getCanvas();
canvas.translate(CM, CM*2);
canvas.clipRect(0, 0, rect.width(), rect.height());
将画布保持完全相同的状态。