如何使用iTextSharp旋转PDF页面而不会导致ghostscript错误?

时间:2013-11-21 17:48:44

标签: c# asp.net pdf itextsharp ghostscript

我正在尝试使用iTextSharp旋转页面&让它由ghostscript读取以生成页面作为图像。这就是我旋转页面的方式:

    byte[] retVal = null;
    using (MemoryStream oOutput = new MemoryStream())
        {
            PdfReader oPDFReader = new PdfReader(BaseFile);
            PdfStamper stamper = new PdfStamper(oPDFReader, oOutput );

            for (var pageNum = 1; pageNum <= oPDFReader.NumberOfPages; pageNum++)
            {
                var oDocumentPage = DocumentPages.Where(page => page.PageNumber == pageNum && page.RotationDegree != 0).FirstOrDefault();
                if (oDocumentPage != null )
                {
                    if (oDocumentPage.RotationDegree < 0)
                        oDocumentPage.RotationDegree = oDocumentPage.RotationDegree + 360; //make sure this is a positive value.
                    int desiredRot = oDocumentPage.RotationDegree;
                    PdfDictionary oPageDict = oPDFReader.GetPageN(pageNum);
                    PdfNumber rotation = oPageDict.GetAsNumber(PdfName.ROTATE);
                    if (rotation != null)
                    {
                        desiredRot += rotation.IntValue;
                        desiredRot %= 360; // must be 0, 90, 180, or 270
                    }

                    oPageDict.Put(PdfName.ROTATE, new PdfNumber(desiredRot));
                }
            }
            stamper.FormFlattening = true;
            stamper.Writer.CloseStream = false;
            stamper.Close();
            retVal = oOutput.ToArray();
            oOutput.Close();
        }
        return retVal;

但是当我在ghostscript中运行时,我收到以下错误:

    **** Warning: An error occured while reading an XREF table.
    **** The file has been damaged.  This may have been caused
    **** by a problem while converting or transfering the file.
    **** Ghostscript will attempt to recover the data.
    ****
    **** This file had errors that were repaired or ignored.
    **** The file was produced by:
    **** >>>> Adobe LifeCycle Designer ES 8.2; modified using iTextSharp 4.1.6 by 1T3XT <<<<
    **** Please notify the author of the software that produced this
    **** file that it does not conform to Adobe's published
    **** PDF specification.

我目前在WCF项目中引用ghostscript,所以这个console.out消息实际上崩溃了我的程序。理想情况下,我希望在没有程序抛出此错误的情况下生成PDF,但另外我正在考虑在控制台中托管此WCF,以便它可以将可修复的错误丢弃到其内容中。

有什么想法吗?

0 个答案:

没有答案