gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:15:19 27 4
gpt4 key购买 nike

我想将一个 PDF 页面插入到另一个已缩放的 PDF 页面中。我想使用 iTextSharp 来实现此目的。

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

这可能吗?

这样做的目的是保留放大功能而不损失质量。

使用 PDF 矢量来重现矢量绘图非常困难,因为它是一种极其复杂的绘图。

将矢量绘图导出为高分辨率图像不是一个选项,因为我必须在单个 PDF 文档中使用大量矢量绘图。最终的 PDF 会很大并且写入速度太慢。

最佳答案

尽管有多种方法可以实现,但这相对容易做到。如果您要创建一个新文档,其中包含其他文档而没有其他内容,那么最容易使用的可能是 PdfWriter.GetImportedPage(PdfReader, Int)。这将为您提供一个 PdfImportedPage (继承自 PdfTemplate)。完成后,您可以使用 PdfWriter.DirectContent.AddTemplate(PdfImportedPage, Matrix) 将其添加到新文档中。

AddTemplate() 有几个重载,但最简单的一个(至少对我来说)是采用 System.Drawing.Drawing2D.Matrix 的重载。如果您使用它,您可以轻松缩放和平移(更改 x,y),而不必考虑“矩阵”术语。

下面是展示这一点的示例代码。它以 iTextSharp 5.4.0 为目标,但如果删除 using 语句,它的工作方式应该与 4.1.6 几乎相同。它首先创建一个包含 12 页且背景颜色随机的示例 PDF。然后,它创建第二个文档,并将第一个 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();
}
}
}
}

关于pdf - 在 PDF 中插入 PDF(不合并文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16046656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com