How do I draw to a PDF page using PdfDocument?

2.3k views Asked by At

One of my Android Studio activities has a diagram, which I want the user to be able to export to a PDF file. I'm trying to use the built-in PdfDocument, but finding it difficult to find much information online (apart from the official docs).

I can create the blank document no problem. I've also been able to add a circle to it using drawCircle.

However, I'm not sure how to add the diagram using the class I already have (rather than coding it all in again). So, I'm currently drawing the diagram in a DiagramFragment as follows (I've simplified this to show the main points):

public class DiagramFragment extends Fragment {

    DiagramData       mData;
    Diagram           mDiagram;

    public DiagramFragment() {}

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, 
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // Inflate layout, and find the Diagram element
        View rootView = inflater.inflate(
                R.layout.fragment_diagram, container, false);
        mDiagram = rootView.findViewById(R.id.diagram);

        if(mData != null) mDiagram.setDiagramData(mData);

        drawDiagram();

        return rootView;
    }
}

(mData contains arrays etc which describe the diagram, where lines should be drawn, colours etc)

The mDiagram class is this:

    public class Diagram extends View {

       public ChartGrid(Context thisContext, AttributeSet attrs) {
            super(thisContext, attrs);

           // This sets up a few initial variables; nothing important

        }

        public void setDiagramData(DiagramData data) {
            mData = data;
            invalidate();
        }

        protected void onDraw(Canvas canvas) {

          // Make sure mData is set before we draw
          if (mData == null) return;

          float canvas_height = getHeight();

          // Set up canvas
          canvas.save();

          // Draw diagram
          draw_diagram(canvas, canvas_height);

          canvas.restore();
      }

    private void draw_chart(Canvas canvas, float height) {

        Paint mPaint = new Paint();

        if(mData == null) return;

        for (int ii = 0; ii < mData.length(); ii++) {

              mPaint.setColor(mData[ii].getColour());
              canvas.drawRect(mData[ii].getXStart(),
                                mData[ii].getYStart(), 
                                height, mData[ii].getYEnd(),
                                mPaint);
        }
    }
  }

That all works fine, and shows the diagram in the relevant Activity.

However, I want to show it in a PDF page. I don't know how to get the canvas to draw on, without the

View rootView = inflater.inflate(
                R.layout.fragment_diagram, container, false);
        mDiagram = rootView.findViewById(R.id.diagram);

which exists in the fragment.

So far I have (I've already checked permissions etc before this; after this I close pages and write to file etc):

    // Create document
    PdfDocument document = new PdfDocument();
    PdfDocument.PageInfo pageInfo
            = new PdfDocument.PageInfo.Builder(100, 100, 1).create();

    // Create Diagram Page
    PdfDocument.Page page1 = document.startPage(pageInfo);

    ... I'm not sure what needs to go in here ...

    // Finish Page 1
    document.finishPage(page1);

Can anyone advise me? Thank you.

1

There are 1 answers

2
Jeffrey Blattman On BEST ANSWER

Answer your comment here... No idea if this will be helpful but something to play with.

Looks like if you have a PdfDocument.Page, you can get a Canvas object from it (getCanvas()). Once you have that you can draw your View onto the Canvas with View.draw(canvas).