How to rotate PDF page with iTextSharp without causing error in ghostscript?

偶尔善良 提交于 2019-12-11 08:01:12

问题


I'm trying to use iTextSharp to rotate a page & have it read by ghostscript to generate the pages as images. This is how I'm rotating the pages:

    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;

But when I run this in ghostscript, I get the following error:

    **** 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.

I'm currently referencing ghostscript in a WCF project, so this console.out message actually crashes my program. Ideally I want to generate the PDF without the program throwing this error, but alternatively I'm looking into hosting this WCF in a console so it can throw out repairable errors to its heart's content.

Any idea?

来源:https://stackoverflow.com/questions/20128286/how-to-rotate-pdf-page-with-itextsharp-without-causing-error-in-ghostscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!