当前位置: 首页 > 知识库问答 >
问题:

在PDF中插入PDF(不合并文件)

夏祺然
2023-03-14

我想在另一个缩放的PDF页面中插入一个PDF页面。我想用iTextSharp做这个。

我有一个矢量绘图,可以导出为单页PDF文件。我想将此文件添加到其他PDF文档的页面中,就像我将图像添加到PDF文档一样。

这可能吗?

这样做的目的是在不损失质量的情况下保留放大的能力。

使用PDF矢量很难再现矢量绘图,因为它是一个极其复杂的绘图。

导出矢量绘图为高分辨率图像不是一个选项,因为我不得不在一个单独的PDF文档中使用它们。最终的PDF会非常大,而且写起来太慢。

共有2个答案

訾旭
2023-03-14

此外;当我试图添加一个旋转的pdf到一个旋转的pdf,我得到了一些旋转问题。有点令人困惑,但您应该查看“PdfImportedPage。要添加到pdf的页面的“旋转”。

PdfImportedPage page;//page = writer.GetImportedPage(PdfReader reader, int pageNum);
PdfContentByte pcb;//pcb = PdfWriter.DirectContentUnder;

//create matrix to use for rotating imported page    
Matrix matrix = new Matrix(a, b, c, d, e, f);
matrix.Rotate(-(page.Rotation));
if (page.Rotation != 0)
   pcb.AddTemplate(page, matrix, true);
else
   pcb.AddTemplate(page, a, b, c, d, e, f, true);

代码看起来很傻,但我想让您注意“matrix.Rotate(导入页面的负旋转)”

爱唯
2023-03-14

这相对容易做到,尽管有几种方法。如果你正在创建一个新文档,其中包含其他文档,那么最容易使用的可能就是< code>PdfWriter。GetImportedPage(PdfReader,Int)。这将为您提供一个< code>PdfImportedPage(它从< code>PdfTemplate继承而来)。一旦有了它,您就可以使用< code>PdfWriter将其添加到新文档中。AddTemplate(PdfImportedPage,Matrix)。

< code>AddTemplate()有几个重载,但最简单的一个(至少对我来说)是采用< code >系统的那个。Drawing.Drawing2D.Matrix。如果你使用这个,你可以很容易地缩放和转换(改变x,y)而不必考虑“矩阵”的术语。

下面是演示这一点的示例代码。它以 iTextSharp 5.4.0 为目标,尽管如果您删除 using 语句,它的工作原理应与 4.1.6 几乎相同。它首先创建一个示例PDF,其中包含12个页面和随机背景颜色。然后,它创建第二个文档,并从第一个PDF中添加每个页面,缩放50%,以便4个旧页面适合1个新页面。有关更多详细信息,请参阅代码注释。此代码假定所有页面的大小相同,如果您的情况不同,您可能需要执行进一步的计算。

//Test files that we'll be creating
var file1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf");
var file2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File2.pdf");

//For test purposes we'll fill the pages with a random background color
var R = new Random();

//Standard PDF creation, nothing special here
using (var fs = new FileStream(file1, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.Open();

            //Create 12 pages with text on each one
            for (int i = 1; i <= 12; i++) {
                doc.NewPage();

                //For test purposes fill the page with a random background color
                var cb = writer.DirectContentUnder;
                cb.SaveState();
                cb.SetColorFill(new BaseColor(R.Next(0, 256), R.Next(0, 256), R.Next(0, 256)));
                cb.Rectangle(0, 0, doc.PageSize.Width, doc.PageSize.Height);
                cb.Fill();
                cb.RestoreState();

                //Add some text to the page
                doc.Add(new Paragraph("This is page " + i.ToString()));
            }
            doc.Close();
        }
    }
}

//Create our combined file
using (var fs = new FileStream(file2, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {

            //Bind a reader to the file that we created above
            using (var reader = new PdfReader(file1)) {
                doc.Open();

                //Get the number of pages in the original file
                int pageCount = reader.NumberOfPages;

                //Loop through each page
                for (int i = 0; i < pageCount; i++) {
                    //We're putting four original pages on one new page so add a new page every four pages
                    if (i % 4 == 0) {
                        doc.NewPage();
                    }

                    //Get a page from the reader (remember that PdfReader pages are one-based)
                    var imp = writer.GetImportedPage(reader, (i + 1));
                    //A transform matrix is an easier way of dealing with changing dimension and coordinates on an rectangle
                    var tm = new System.Drawing.Drawing2D.Matrix();

                    //Scale the image by half
                    tm.Scale(0.5f, 0.5f);

                    //PDF coordinates put 0,0 in the bottom left corner.
                    if (i % 4 == 0) {
                        tm.Translate(0, doc.PageSize.Height);                   //The first item on the page needs to be moved up "one square"
                    } else if (i % 4 == 1) {
                        tm.Translate(doc.PageSize.Width, doc.PageSize.Height);  //The second needs to be moved up and over
                    } else if (i % 4 == 2) {
                                                                                //Nothing needs to be done for the third
                    } else if (i % 4 == 3) {
                        tm.Translate(doc.PageSize.Width, 0);                    //The fourth needs to be moved over
                    }

                    //Add our imported page using the matrix that we set above
                    writer.DirectContent.AddTemplate(imp,tm);

                }

                doc.Close();
            }
        }
    }
}
 类似资料:
  • Mac上的预览应用程序允许合并多个PDF文件,尽管功能相当模糊。我正在用Haskell编写一个实用程序,它需要执行类似的任务,即将任意数量的PDF文件合并到一个新文件中。 有没有人建议从哪里入手?显然,如果有一个关于Hackage的库可以开箱即用地完成大部分工作,那将是理想的,但如果没有,那么一些关于从哪里开始的指针将非常感激。

  • 我正在尝试合并我的应用程序中的两个pdf文件。但我的应用程序在创建组合文件引用的时候不断出现故障。(参见代码中的注释)。有人能给我指一下这里的正确方向吗?谢了。

  • 问题内容: 我的概念是-网站中有10个pdf文件。用户可以选择一些pdf文件,然后选择合并以创建一个包含所选页面的pdf文件。我该如何用PHP做到这一点? 问题答案: 我以前做过 我有一个用fpdf生成的pdf,我需要在其中添加可变数量的PDF。 因此,我已经设置了fpdf对象和页面),并使用fpdi导入了文件通过扩展PDF类来添加FDPI: 基本上,这会将每个pdf转换为图像以放入您的其他pdf

  • 我想合并几个pdf文件。我该怎么做? 到目前为止,我得到的是这个。 我正在尝试使用这个程序包myokyawhtun/PDFMerger。 根据我尝试合并的文件的不同,错误也会有所不同。 如果我尝试合并同一个pdf两次,则会生成该文件,但全部为空 如果我尝试上面的代码,就会得到HTTP错误500 如果我尝试使用更多文件,就会出现tcpdi_解析器内存不足错误 底线是它似乎不起作用。 我已经尝试了其他

  • 我有一本书的多份副本。不同用户评论的pdf文档。我想把所有这些评论合并成一个新的pdf“合并”。 我在一个名为“路径”和“目录”属性的文档类中编写了这个子类。 这段代码导致了一个我无法解决的异常。 iText。内核PDFEException:“Pdf间接对象属于其他Pdf文档。将对象复制到当前pdf文档。' 要执行此任务,我需要更改什么?还是我完全摆脱了我的代码块?

  • 在上一章中,我们已经了解了如何合并多个PDF文档。 在本章中,我们将了解如何从PDF文档的页面中提取图像。 从PDF文档生成图像 PDFBox库为您提供了一个名为PDFRenderer的类, PDFRenderer PDF文档呈现为AWT BufferedImage。 以下是从PDF文档生成图像的步骤。 第1步:加载现有PDF文档 使用PDDocument类的静态方法load()加载现有PDF文档