iText AddImage() 到特定页面

发布于 2024-11-04 14:52:48 字数 2098 浏览 0 评论 0原文

我在尝试将 PdfContentByte 直接定位到特定页面时遇到问题。我的问题是:我需要为每个页面添加一个图像(可行),并且需要在右下角的每个页面添加一个二维码,但这仅适用于第一页,我不知道如何重复它在其他人身上。

这是我的代码:

        public string GeneratePDFDocument(Atomic.Development.Montenegro.Data.Entities.Document document, Stamp stamp)
        {
            string filename = @"C:\Users\Sheldon\Desktop\Pdf.Pdf";
            FileStream fs = new FileStream(filename, FileMode.Create);

            iTextSharp.text.Document pdfDocument = new iTextSharp.text.Document(PageSize.LETTER, PAGE_LEFT_MARGIN, PAGE_RIGHT_MARGIN, PAGE_TOP_MARGIN, PAGE_BOTTOM_MARGIN);
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, fs);

            pdfDocument.Open();

            int count = document.Pages.Count;
            foreach (Page page in document.Pages)
            {
                Image img = Image.GetInstance(page.Image);
                img.ScaleToFit(PageSize.LETTER.Width-(PAGE_LEFT_MARGIN + PAGE_RIGHT_MARGIN), PageSize.LETTER.Height-(PAGE_TOP_MARGIN + PAGE_BOTTOM_MARGIN));
                pdfDocument.Add(img);

                PlaceCodeBar(writer);

            }

            pdfDocument.Close();
            writer.Close();
            fs.Close();

            return filename;
        }

        private static void PlaceCodeBar(iTextSharp.text.pdf.PdfWriter writer)
        {
            String codeText = "TEXT TO ENCODE";

            iTextSharp.text.pdf.BarcodePDF417 pdf417 = new iTextSharp.text.pdf.BarcodePDF417();
            pdf417.SetText(codeText);
            Image img = pdf417.GetImage();
            iTextSharp.text.pdf.BarcodeQRCode qrcode = new iTextSharp.text.pdf.BarcodeQRCode(codeText, 1, 1, null);
            img = qrcode.GetImage();


            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            cb.SaveState();
            cb.BeginText();

            img.SetAbsolutePosition(PageSize.LETTER.Width-PAGE_RIGHT_MARGIN-img.ScaledWidth, PAGE_BOTTOM_MARGIN);
            cb.AddImage(img);
            cb.EndText();
            cb.RestoreState();
        }

I'm having a problem trying to locate a PdfContentByte directly into an specific page. My problem is: I need to add an Image for each page (That works) and need to add a QRCode to each of the pages at the right bottom corner but this works only for the first Page and I don't know how to repeat it on the other ones.

This is my code:

        public string GeneratePDFDocument(Atomic.Development.Montenegro.Data.Entities.Document document, Stamp stamp)
        {
            string filename = @"C:\Users\Sheldon\Desktop\Pdf.Pdf";
            FileStream fs = new FileStream(filename, FileMode.Create);

            iTextSharp.text.Document pdfDocument = new iTextSharp.text.Document(PageSize.LETTER, PAGE_LEFT_MARGIN, PAGE_RIGHT_MARGIN, PAGE_TOP_MARGIN, PAGE_BOTTOM_MARGIN);
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDocument, fs);

            pdfDocument.Open();

            int count = document.Pages.Count;
            foreach (Page page in document.Pages)
            {
                Image img = Image.GetInstance(page.Image);
                img.ScaleToFit(PageSize.LETTER.Width-(PAGE_LEFT_MARGIN + PAGE_RIGHT_MARGIN), PageSize.LETTER.Height-(PAGE_TOP_MARGIN + PAGE_BOTTOM_MARGIN));
                pdfDocument.Add(img);

                PlaceCodeBar(writer);

            }

            pdfDocument.Close();
            writer.Close();
            fs.Close();

            return filename;
        }

        private static void PlaceCodeBar(iTextSharp.text.pdf.PdfWriter writer)
        {
            String codeText = "TEXT TO ENCODE";

            iTextSharp.text.pdf.BarcodePDF417 pdf417 = new iTextSharp.text.pdf.BarcodePDF417();
            pdf417.SetText(codeText);
            Image img = pdf417.GetImage();
            iTextSharp.text.pdf.BarcodeQRCode qrcode = new iTextSharp.text.pdf.BarcodeQRCode(codeText, 1, 1, null);
            img = qrcode.GetImage();


            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            cb.SaveState();
            cb.BeginText();

            img.SetAbsolutePosition(PageSize.LETTER.Width-PAGE_RIGHT_MARGIN-img.ScaledWidth, PAGE_BOTTOM_MARGIN);
            cb.AddImage(img);
            cb.EndText();
            cb.RestoreState();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-11-11 14:52:48

因此,将其添加到您的 foreach (Page...) 循环中:

        foreach (Page page in document.Pages)
        {
            Image img = Image.GetInstance(page.Image);
            img.ScaleToFit(PageSize.LETTER.Width-(PAGE_LEFT_MARGIN + PAGE_RIGHT_MARGIN), PageSize.LETTER.Height-(PAGE_TOP_MARGIN + PAGE_BOTTOM_MARGIN));
            pdfDocument.Add(img);
            PlaceCodeBar(writer);
        }

如果这是同一个 PDF 的第二遍(您已关闭它然后再次打开它),请使用 PdfStamper< /code> 而不是 PdfWriter。然后,您可以获得每个页面的直接内容,而不是为每个页面重用(和重置)的一个直接内容。

PS:删除 BeginText()EndText() 调用。这些运算符仅应在实际绘制文本/设置字体/等时使用。没有线条艺术。没有图像。不过,SaveState()/RestoreState() 很好。一定要保留这些。

So add it in your foreach (Page...) loop:

        foreach (Page page in document.Pages)
        {
            Image img = Image.GetInstance(page.Image);
            img.ScaleToFit(PageSize.LETTER.Width-(PAGE_LEFT_MARGIN + PAGE_RIGHT_MARGIN), PageSize.LETTER.Height-(PAGE_TOP_MARGIN + PAGE_BOTTOM_MARGIN));
            pdfDocument.Add(img);
            PlaceCodeBar(writer);
        }

If this is a second pass on the same PDF (you've closed it then opened it again), use a PdfStamper rather than a PdfWriter. You can then get the direct content of each page rather than the one direct content that is reused (and reset) for each page.

PS: Drop the BeginText() and EndText() calls. Those operators should only be used when actually drawing text/setting fonts/etc. No line art. No images. The SaveState()/RestoreState() are good though. Definitely keep those.

七秒鱼° 2024-11-11 14:52:48

我只是想知道如何解决问题。只需删除 cb.SaveState() 和 cb.RestoreState() 即可使页面上的图像实际上处于活动状态。

I just figure out how to solve the problem. Just delete the cb.SaveState() and cb.RestoreState() and it put the image on the page is actually active.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文