使用 PdfStamper 和 ContentPdfByte 复制原始文档时 iTextSharp 中的内容旋转

问题描述 投票:0回答:1

我正在使用 iTextSharp 的旧版本 (5.5.9) 将 PDF 的内容复制到新的 PDF 中,并将条形码添加到第一页的顶部,并将第一页的内容缩小以适合剩余的内容页面上条形码下方的空间。

这对于没有旋转页面的标准 PDF 文件工作得很好,但是我刚刚发现,当页面旋转(例如 270)时,输出 PDF 上的内容会旋转(而在原始 PDF 上,它似乎不会旋转)观看时旋转);我只知道它是旋转的,因为调试时 pageSize.Rotation 不为零。

例如,我有一个 PDF 文档,在显示和/或打印时显示为 A4 纵向,但当我通过代码推送该文档时,它告诉我高度 = 595.2,宽度 = 841.68,旋转 = 270。

说实话,我使用(下面)来实现这一点的代码是基于我在 iTextSharp 网站和此处找到的一些示例,我不确定如何纠正这个问题;我怀疑它是我传递给 pdfContentByte.SetLiteral 的字符串中的参数之一?

我的代码如下...

    public static byte[] AddBarcodeToFirstPage(byte[] pdf, String barcodeContent)
    {

        Image barcodeImage = Image.GetInstance(Code128.GetBarcode(barcodeContent, 5), ImageFormat.Bmp);

        using (PdfReader pdfReader = new PdfReader(pdf))
        using (MemoryStream memoryStream = new MemoryStream())
        using (PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream))
        {

            Rectangle pageSize = pdfReader.GetPageSizeWithRotation(1);
            pdfStamper.Writer.SetPageSize(pageSize);

            float pageWidth = pageSize.Width, pageHeight = pageSize.Height;
            float barcodeWidth = pageWidth / 3, barcodeHeight = 40;
            barcodeImage.ScaleToFit(barcodeWidth, barcodeHeight);
            barcodeImage.SetAbsolutePosition((pageWidth - barcodeWidth) / 2, pageHeight - barcodeHeight);

            int numberOfPages = pdfReader.NumberOfPages;
            for (int pageIndex = 1; pageIndex <= numberOfPages; pageIndex++)
            {

                float percentage = 1.0f;
                if (pageIndex == 1)
                    percentage = (pageHeight - (barcodeHeight * 4)) / pageHeight /* 0.7f */;

                float offsetX = (pageWidth * (1 - percentage)) / 2;
                float offsetY = (pageHeight * (1 - percentage)) / 2;
                PdfContentByte pdfContentByte = pdfStamper.GetUnderContent(pageIndex);

                if (pageIndex == 1)
                {
                    pdfContentByte.AddImage(barcodeImage);
                    iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph(barcodeContent);
                    paragraph.Alignment = Element.ALIGN_CENTER;

                    ColumnText ct = new ColumnText(pdfContentByte);
                    float llx = (pageWidth - barcodeWidth) / 2, lly = pageHeight - (barcodeHeight * 2),
                        urx = llx + barcodeWidth, ury = lly + barcodeHeight;
                    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(llx, lly, urx, ury);
                    ct.SetSimpleColumn(rect);
                    ct.AddElement(paragraph);
                    ct.Go();
                }
                
                pdfContentByte.SetLiteral(String.Format("\nq {0} 0 0 {1} {2} {3} cm\nq\n", percentage, percentage, offsetX, offsetY));
                pdfContentByte = pdfStamper.GetOverContent(pageIndex);

            }

            pdfStamper.Close();
            pdfReader.Close();

            return memoryStream.ToArray();

        }

    }

任何人都可以向我解释如何设置输出的旋转以使其与原始内容相匹配吗?

pdf itext
1个回答
0
投票

已经有一段时间了,我不记得 100% 为什么但是......也许我的这段代码会对你有帮助

//Check if the incoming page is rotated
Rectangle currentPageRectangle = reader.GetPageSizeWithRotation(i);
if (currentPageRectangle.Rotation > 0)
{
  //if its rotated, remove the rotation
  reader.GetPageN(i).Put(PdfName.ROTATE, new PdfNumber(0));
}

//get the updated rectangle values
currentPageRectangle = reader.GetPageSizeWithRotation(i);

//Import the page from the reader
PdfImportedPage importedPage = writer.GetImportedPage(reader, i);
© www.soinside.com 2019 - 2024. All rights reserved.