1

这是这个问题的后续:here,涉及 iText。我创建了一个具有不同旋转角度的新 Pdf,然后删除旧的并将新的重命名为旧的名称。我已经确定我的问题实际上发生了(仅当旋转 == null,wtf 时)调用

outFile.renameTo(inFile)

奇怪的是 renameTo() 返回 true,但文件不等于原始文件,outFile 将不再在 Windows 上的 Adob​​e Reader 中打开。我尝试在桌面 Pdf 修复程序中分析损坏的 Pdf 文件,得到的结果是:

The end-of-file marker was not found.
The ‘startxref’ keyword or the xref position was not found.
The end-of-file marker was not found.

如果我省略了对 delete() 和 renameTo() 的调用,我会留下两个文件,这两个文件都没有损坏。我还尝试使用具有相同结果的 byte[] 复制文件内容。我试过 outFile.renameTo(new File(inFile.toString()) 因为 inFile 实际上是 File 的子类,结果相同。我尝试了 new FileDescriptor().sync(),结果相同。我尝试添加这个在每个文件操作之间进行广播,结果相同:

PdfRotateService.appContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
            .parse("file://")));

我尝试过以相同的结果休眠线程。我已经验证路径是正确的。不会抛出异常,并且 delele() 和 renameTo() 返回 true。我还尝试保留对 FileOutputStream 的引用并在 finally 块中手动关闭它。

我开始认为 Android 操作系统或其他东西中存在错误(但也许我忽略了一些简单的东西),请帮忙!我想要一个与原始文件名相同的旋转 Pdf。

static boolean rotatePdf(LocalFile inFile, int angle)
{
    PdfReader reader = null;
    PdfStamper stamper = null;

    LocalFile outFile = getGoodFile(inFile, ROTATE_SUFFIX);

    boolean worked = true;

    try
    {
        reader = new PdfReader(inFile.toString());
        stamper = new PdfStamper(reader, new FileOutputStream(outFile));

        int i = FIRST_PAGE;
        int l = reader.getNumberOfPages();

        for (; i <= l; ++i)
        {
            int desiredRot = angle;
            PdfDictionary pageDict = reader.getPageN(i);

            PdfNumber rotation = pageDict.getAsNumber(PdfName.ROTATE);

            if (rotation != null)
            {
                desiredRot += rotation.intValue();
                desiredRot %= 360;
            }
            // else
            // worked = false;

            pageDict.put(PdfName.ROTATE, new PdfNumber(desiredRot));
        }
    } catch (IOException e)
    {
        worked = false;
        Log.w("Rotate", "Caught IOException in rotate");
        e.printStackTrace();
    } catch (DocumentException e)
    {
        worked = false;
        Log.w("Rotate", "Caught DocumentException in rotate");
        e.printStackTrace();
    } finally
    {

        boolean z = closeQuietly(stamper);
        boolean y = closeQuietly(reader);

        if (!(y && z))
            worked = false;
    }

    if (worked)
    {
        if (!inFile.delete())
            worked = false;

        if (!outFile.renameTo(inFile))
            worked = false;
    }
    else
    {
        outFile.delete();
    }

    return worked;
}

static boolean closeQuietly(Object resource)
{
    try
    {
        if (resource != null)
        {
            if (resource instanceof PdfReader)
                ((PdfReader) resource).close();
            else if (resource instanceof PdfStamper)
                ((PdfStamper) resource).close();
            else
                ((Closeable) resource).close();
            return true;
        }
    } catch (Exception ex)
    {
        Log.w("Exception during Resource.close()", ex);
    }
    return false;
}

public static LocalFile getGoodFile(LocalFile inFile, String suffix)
{
    @SuppressWarnings("unused")
    String outString = inFile.getParent() + DIRECTORY_SEPARATOR +
            removeExtension(inFile.getName()) + suffix + getExtension(inFile.getName());

    LocalFile outFile = new LocalFile(inFile.getParent() + DIRECTORY_SEPARATOR +
            removeExtension(inFile.getName()) + suffix + getExtension(inFile.getName()));

    int n = 1;
    while (outFile.isFile())
    {
        outFile = new LocalFile(inFile.getParent() + DIRECTORY_SEPARATOR +
                removeExtension(inFile.getName()) + suffix + n + getExtension(inFile.getName()));
        ++n;
    }
    return outFile;
}
4

0 回答 0