Android Studio: 如何识别PDF文件中的特定特殊页面?

huangapple go评论71阅读模式
英文:

(Kind of solved but still appreciate better answers) Android Studio: How to identify certain special pages in a PDF file?

问题

我正在编写一个为学校学生生成数学练习册的应用程序。例如,它将生成2到5页的简单数学问题和1到2页的答案。PDF可以保存到文件并在以后加载。然后它有一个可以打印所有页面的打印功能。我想让它跳过打印答案页。

是否有可能自动识别哪些页面是答案页?我只能想到一种解决方法,即使那些答案页面具有特殊的高度或宽度,但甚至不确定是否有效。是否有更好的方法来做到这一点?

英文:

I am writing an app that generates Maths worksheets for school students. It will, for example, generate 2 to 5 pages of simple Maths questions and 1 to 2 pages of answers. The PDF can be saved to file and loaded again later. Then it has a print function that can print all the pages. I want to make it skip printing the answer pages.

Is it possible to automatically identify which pages are the answer pages? I can only think of a workaround by making those answer pages have special height or width but not even sure if this works. Are there any better ways to do this?

答案1

得分: 0

好的,以下是您要翻译的内容:

在继续项目时,我使用了以下方法:在构建PDF时,我将单词“Answer”放在左上角,并用drawRect()绘制灰色矩形围绕它。然后,在实际打印之前,我在PrintDocumentAdapter()类中使用以下代码来检查像素0,0的颜色是否为灰色。

@Override
public void onStart() {
    if (parcelFileDescriptor != null) {
        try {
            pdfRenderer = new PdfRenderer(parcelFileDescriptor);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    int tempTotal = pdfRenderer.getPageCount();
    Bitmap[] tempBitmap = new Bitmap[tempTotal];
    finalTotal = tempTotal;
    for (int pageNum = 0; pageNum < tempTotal; pageNum++) {
        PdfRenderer.Page tempPage = pdfRenderer.openPage(pageNum);
        tempBitmap[pageNum] = Bitmap.createBitmap(WS_WIDTH, WS_HEIGHT, Bitmap.Config.ARGB_8888);
        tempPage.render(tempBitmap[pageNum], null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
        if (tempBitmap[pageNum].getPixel(0, 0) == Color.GRAY) {
            finalTotal--;
        }
        tempPage.close();
    }
}

它运行正常。至少,如果用户只尝试打印使用我的应用构建的PDF文件,不应该出现问题。 Android Studio: 如何识别PDF文件中的特定特殊页面?

如果您知道更好的方法,请告诉我。谢谢!

英文:

Ok, I continued the project and used the following method: when constructing the PDF, I put the word "Answer on the top left corner with a gray rectangle surrounding it drawn with drawRect(). Then before the actual printing, I used the following code inside the PrintDocumentAdapter() class to check whether the color of the pixel 0,0 is gray or not.

@Override
        public void onStart() {
            if (parcelFileDescriptor != null) {
                try {
                    pdfRenderer = new PdfRenderer(parcelFileDescriptor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int tempTotal = pdfRenderer.getPageCount();
            Bitmap[] tempBitmap = new Bitmap[tempTotal];
            finalTotal = tempTotal;
            for (int pageNum = 0; pageNum &lt; tempTotal; pageNum++) {
                PdfRenderer.Page tempPage = pdfRenderer.openPage(pageNum);
                tempBitmap[pageNum] = Bitmap.createBitmap(WS_WIDTH, WS_HEIGHT, Bitmap.Config.ARGB_8888);
                tempPage.render(tempBitmap[pageNum], null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
                if (tempBitmap[pageNum].getPixel(0, 0) == Color.GRAY) {
                    finalTotal--;
                }
                tempPage.close();
            }
        }

It works fine. At least should cause no problem if the users only attempt to print PDF files constructed with my app. Android Studio: 如何识别PDF文件中的特定特殊页面?

Please tell me if you know a better way to do this. Thanks!

huangapple
  • 本文由 发表于 2020年8月5日 19:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63264273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定