인쇄관련 자바 예제 코딩 중 문제가 생겼습니다.
조회수 441회
import android.content.Context; ~
public class CustomPrintActivity extends AppCompatActivity {
public class MyPrintDocumentAdapter extends PrintDocumentAdapter {
Context context;
private int pageHeight;
private int pageWidth;
public PdfDocument myPdfDocument;
public int totalpages = 4;
public MyPrintDocumentAdapter(Context context)
{
this.context = context;
}
@Override
public void onLayout(PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback,
Bundle metadata) {
myPdfDocument = new PrintedPdfDocument(context, newAttributes);
pageHeight = newAttributes.getMediaSize().getHeightMils() / 1000 * 72;
pageWidth = newAttributes.getMediaSize().getWidthMils() / 1000 * 72;
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
if (totalpages > 0) {
PrintDocumentInfo.Builder builder = new PrintDocumentInfo
.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(totalpages);
PrintDocumentInfo info = builder.build();
callback.onLayoutFinished(info, true);
} else {
callback.onLayoutFailed("Page Count is zero.");
}
}
@Override
public void onWrite(final PageRange[] pageRanges,
final ParcelFileDescriptor destination,
final CancellationSignal cancellationSignal,
final WriteResultCallback callback) {
for (int i = 0; i < totalpages; i++) {
if (pageInRange(pageRanges, i)) {
PdfDocument.PageInfo newPage = new PdfDocument.PageInfo.Builder(pageWidth,
pageHeight, i).create();
PdfDocument.Page page = myPdfDocument.startPage(newPage);
if (cancellationSignal.isCanceled()) {
callback.onWriteCancelled();
myPdfDocument.close();
myPdfDocument = null;
return;
}
drawPage(page, i);
myPdfDocument.finishPage(page);
}
}
try {
myPdfDocument.writeTo(new FileOutputStream(
destination.getFileDescriptor()));
} catch (IOException e) {
callback.onWriteFailed(e.toString());
return;
} finally {
myPdfDocument.close();
myPdfDocument = null;
}
callback.onWriteFinished(pageRanges);
}
private void drawPage(PdfDocument.Page page, int pagenumber) {
Canvas canvas = page.getCanvas();
pagenumber++;
int titleBaseLine = 72;
int leftMargin = 54;
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("Test print Document Page" + pagenumber,
leftMargin,
titleBaseLine,
paint);
paint.setTextSize(14);
canvas.drawText("this is som test content to verify that custom document printing works",
leftMargin, titleBaseLine + 35, paint);
if (pagenumber % 2 == 0)
paint.setColor(Color.RED);
else
paint.setColor(Color.GREEN);
PdfDocument.PageInfo pageInfo = page.getInfo();
canvas.drawCircle(pageInfo.getPageHeight() / 2,
pageInfo.getPageHeight() / 2,
150,
paint);
}
private boolean pageInRange(PageRange[] pageRanges, int page) {
for (int i = 0; i < pageRanges.length; i++) {
if ((page >= pageRanges[i].getStart()) &&
(page <= pageRanges[i].getEnd()))
return false;
}
return false;
}
}
public void printDocument (View view)
{
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jopName = this.getString(R.string.app_name) + "Document";
printManager.print(jopName, new MyPrintDocumentAdapter(this), null);
}
}
코드는 위와 같습니다. 예제에 나온 그대로 코딩했는데요.
문제는 xml파일에 버튼 하나만 만들고
onclick으로 printDocument를 지정해서 해당 작동과 연결하는데
정작 java코드에는 xml파일과 연결시켜주는 부분이 없는것 같습니다.
그래서 위 코드를 실행했을때 화면에 아무것도 뜨질 않습니다. 액티비티 이름만 뜨구요.
예제가 잘못된걸까요?
같은 책의 다른 예제에는 oncreate 메소드 내에서 setContentView~~를 이용해서 xml과 연결해주는데 이 예제에서만 oncreate를 사용하질 않습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력