简体   繁体   English

隐蔽线性到pdf并打破许多页面

[英]covert linear to pdf and break many pages

i am try to covert linear to pdf document.And i want to make it to break many pages .help me to do it我试图将线性转换为 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);

This is the concept of how I do something similar这是我如何做类似事情的概念

I programmatically create a vertical LinearLayout and then measure and lay it out to the correct width for the page and full height.我以编程方式创建一个垂直线性布局,然后测量并将其布置为页面的正确宽度和全高。

eg例如

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());

Every thing is now measured and sized but the LinearLayout is too long for the page.现在每件事都经过测量和调整大小,但是 LinearLayout 对于页面来说太长了。

Then create a temp holding array for these measured views and add all the child views from the LinearLayout to the temp ArrayList然后为这些测量的视图创建一个临时保存数组,并将 LinearLayout 中的所有子视图添加到临时 ArrayList

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

Because everything is measured and laid out we can just add back the views to the parent LinearLayout only if adding them does not go over the page height we want.因为一切都是经过测量和布局的,所以只有在添加视图不会超过我们想要的页面高度时,我们才能将视图添加回父 LinearLayout。

eg例如

// 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);  
  }
}

Note this is a simplification of the code I use as I have headers and footers for each page, so it is not tested but hopefully you get the concept of measure and layout all children so the children view get the correct size that can be then used to rebuild and re-layout the linearLayout multiple times, once for each page.请注意,这是我使用的代码的简化,因为我有每个页面的页眉和页脚,所以它没有经过测试,但希望你能了解测量和布局所有子项的概念,以便子项视图获得正确的大小,然后可以使用多次重建和重新布局linearLayout,每页一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM