将线性转换为PDF并打破许多页面



我试图将线性转换为PDF文档。我想让它打破许多页。帮我做它

try {
android.graphics.pdf.PdfDocument document = new android.graphics.pdf.PdfDocument();
android.graphics.pdf.PdfDocument.PageInfo pageInfo = new android.graphics.pdf.PdfDocument.PageInfo.Builder(linear2.getWidth(), linear2.getHeight(), 1).create();

android.graphics.pdf.PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
linear2.draw(canvas);
document.finishPage(page);

这是我如何做类似事情的概念

我以编程方式创建一个垂直的LinearLayout,然后测量并布局到正确的页面宽度和全高度。

linearLayout.setLayoutParams(
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.measure(
// A4 width
View.MeasureSpec.makeMeasureSpec(A4Short,View.MeasureSpec.EXACTLY),
// Full height
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
linearLayout.layout(0, 0, linearLayout.getMeasuredWidth(),
linearLayout.getMeasuredHeight());

现在所有的东西都被测量和大小,但是LinearLayout对于页面来说太长了。

然后为这些测量视图创建一个临时保存数组,并将LinearLayout中的所有子视图添加到临时数组列表

ArrayList<View> tempViewsArrayList = new ArrayList<>();
for (int childIndex = 0; childIndex < linearLayout.getChildCount(); childIndex++){
tempViewsArrayList.add(linearLayout.getChildAt(childIndex));
}

因为所有的东西都是测量和布局的,所以只有当添加的视图不超过我们想要的页面高度时,我们才能将视图添加回父LinearLayout。

// Create the initial PDF page
int A4Short = 594; // Postscript points
int A4Long = 841; // Postscript points
int pageNumber = 1;
PdfDocument.PageInfo.Builder pageBuilder = new
PdfDocument.PageInfo.Builder(A4Short,A4Long, pageNumber);
PdfDocument.PageInfo pageInfo = pageBuilder.create();
PdfDocument.Page page = document.startPage(pageInfo);  
boolean childrenToProcess = true;
int currentChildIndexToProcess = 0;
// Loop over all children
while(childrenToProcess){
// Empty the parent LinearLayout at the start of each page
linearLayout.removeAllViews();
// Keep track of current LinearLayout height
int currentHeight = 0;
// Loop through children
while(currentChildIndexToProcess < tempViewsArrayList.size()) {
// Get the currentChild Height
int thisChildHeight =
tempViewsArrayList.get(currentChildIndexToProcess).getHeight();
// Check if we add the current child would the
// view be too big for the page
if((currentHeight + thisChildHeight > pageCanvas.getHeight()){
// Don't process this child as it would be off the page
// start new page instead
break;
}
// Now add the view back
linearLayout.addView(
tempViewsArrayList.get(currentChildIndexToProcess));
// Add the child height to current height
currentHeight += thisChildHeight;
// Move to next child
currentChildIndexToProcess++;
}
// There are enough children to fill the page
// re-layout linear to reposition the children to be in viewable area
// really only needed for pages greater than 1
linearLayout.setLayoutParams(
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.measure(
// A4 width
View.MeasureSpec.makeMeasureSpec(A4Short,View.MeasureSpec.EXACTLY),
// Full height
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
linearLayout.layout(0, 0, linearLayout.getMeasuredWidth(),
linearLayout.getMeasuredHeight());
// Now draw LinearLayout to the PDF page
Canvas canvas = page.getCanvas();
linearLayout.draw(canvase);
// end page
document.finishPage(page);
// See if we have more children to process
if (currentChildIndexToProcess > tempViewsArrayList.size() -1) {
// Stop processing as there are no more children to process
childrenToProcess = false;
} else {
// We need a new page
pageNumber++;
PdfDocument.PageInfo.Builder pageBuilder = new
PdfDocument.PageInfo.Builder(A4Short, A4Long, pageNumber);
PdfDocument.PageInfo pageInfo = pageBuilder.create();
PdfDocument.Page page = document.startPage(pageInfo);  
}
}

请注意,这是我使用的代码的简化,因为我每个页面都有页眉和页脚,所以它没有经过测试,但希望你能得到测量和布局所有子视图的概念,所以子视图得到正确的大小,然后可以用来重建和重新布局linearLayout多次,一次为每个页面。

最新更新