iText CopyAsFormXObject PdfException

发布于 2025-01-18 07:13:31 字数 2492 浏览 0 评论 0 原文

当我处理主PDF文件时,当我进入第2页时,我开始阅读此Adobe Illustrator。我在行 origpage.copyasformxobject(aidocument); rownirect对象上获得了此 pdfexception 。我不确定为什么我会遇到此错误,因为它是一个不同的文件。

itext.kernel.exceptions.pdfexception:“不能从编写的文档中复制间接对象。'

const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;
    
string aifilePath = @"PathToAIFile.ai";    
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
   var aiReader = new iText.Kernel.Pdf.PdfReader(aiStream);
   var aiDocument = new PdfDocument(aiReader, new PdfWriter(aimodifiedaiStream));
   PdfPage origPage = aiDocument.GetFirstPage();
   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(aiDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}

使用PDFtargetDocument更新的两个PDFDocument更新

const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;

string aifilePath = @"PathToAIFile.ai";    
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
   var aisourceReader = new iText.Kernel.Pdf.PdfReader(aiStream);
   var pdfsourceDocument = new PdfDocument(aisourceReader, new PdfWriter(aimodifiedaiStream));
   var pdftargetDocument = new PdfDocument(aisourceReader);
   PdfPage origPage = aiDocument.GetFirstPage();
   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(aiDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}

,其中有一个作者和PDFSourceCument,Just pdfReader仍然获得错误无法复制在阅读模式下打开的文档。

using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
   {
   var aisourceReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
   var aitargetReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
                
   var pdfsourceDocument = new PdfDocument(aisourceReader);
   var pdftargetDocument = new PdfDocument(aitargetReader, new PdfWriter(aimodifiedaiStream));
   PdfPage origPage = pdfsourceDocument.GetFirstPage();

   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(pdfsourceDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
   pdfsourceDocument.Close();
   pdftargetDocument.Close();
   }

As I am processing my main pdf file and when I get to page 2 I start to read this Adobe Illustrator. I get this PdfException at the line origPage.CopyAsFormXObject(aiDocument); about the indirect object. I am not sure why I am getting this error because its a different file.

iText.Kernel.Exceptions.PdfException: 'Cannot copy indirect object from the document that is being written.'

const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;
    
string aifilePath = @"PathToAIFile.ai";    
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
   var aiReader = new iText.Kernel.Pdf.PdfReader(aiStream);
   var aiDocument = new PdfDocument(aiReader, new PdfWriter(aimodifiedaiStream));
   PdfPage origPage = aiDocument.GetFirstPage();
   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(aiDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}

Updated with two PdfDocument

const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;

string aifilePath = @"PathToAIFile.ai";    
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
   var aisourceReader = new iText.Kernel.Pdf.PdfReader(aiStream);
   var pdfsourceDocument = new PdfDocument(aisourceReader, new PdfWriter(aimodifiedaiStream));
   var pdftargetDocument = new PdfDocument(aisourceReader);
   PdfPage origPage = aiDocument.GetFirstPage();
   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(aiDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}

Updated with pdftargetDocument has a writer and pdfsourceDocument with just PdfReader still getting the error Cannot copy to document opened in reading mode.'

using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
   {
   var aisourceReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
   var aitargetReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
                
   var pdfsourceDocument = new PdfDocument(aisourceReader);
   var pdftargetDocument = new PdfDocument(aitargetReader, new PdfWriter(aimodifiedaiStream));
   PdfPage origPage = pdfsourceDocument.GetFirstPage();

   PdfFormXObject  aifForm = origPage.CopyAsFormXObject(pdfsourceDocument);
   canvasPage.AddXObjectAt(aifForm, imageX, imageY);
   pdfsourceDocument.Close();
   pdftargetDocument.Close();
   }

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

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

发布评论

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

评论(1

灯角 2025-01-25 07:13:31

将我的评论概括为答案...

您将 pdfdocument aidocument 的页面复制为xobject的表单xobject到相同的 aid aidocument

显然, aidocument 正在编写其构造器中的 pdfwriter 。但是ITEXT API仅允许从没有 pdfwriter 构建的 pdfdocument 复制页面。

此外,您只能将写入的 pdfdocument 复制,即使用 pdfwriter 构建的。因此,您不能将文档复制到本身中。

因此,您需要不同的 pdfdocument 实例作为复制过程的源和目标。

pdfdocument 实例仅使用 pdfreader ,否 pdfwriter 。目标 pdfdocument 实例是用 pdfwriter 生成的。如果目标也是用 pdfreader 生成的,取决于您的精确用例:您要将Xobject添加到现有文档中,还是要将其添加到空文档中,而没有任何先验内容。

Summing up my comments as an answer...

You copy a page from the PdfDocument aiDocument as form XObject to the same aiDocument.

Clearly aiDocument is being written, it has a PdfWriter in its constructor. But the iText API only allows copying pages from a PdfDocument constructed without a PdfWriter.

Furthermore, you can only copy to a PdfDocument that is written to, i.e. that is constructed with a PdfWriter. Thus, you cannot copy from a document into itself.

Thus, you need different PdfDocument instances as source and target of the copy process.

The source PdfDocument instance is generated with only a PdfReader, no PdfWriter. The target PdfDocument instance is generated with a PdfWriter. If the target also is generated with a PdfReader depends on your precise use case: Do you want to add the XObject to an existing document, or do you want to add it to an empty document without any prior content.

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