iText CopyAsFormXObject PdfException
当我处理主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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将我的评论概括为答案...
您将
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 sameaiDocument
.Clearly
aiDocument
is being written, it has aPdfWriter
in its constructor. But the iText API only allows copying pages from aPdfDocument
constructed without aPdfWriter
.Furthermore, you can only copy to a
PdfDocument
that is written to, i.e. that is constructed with aPdfWriter
. 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 aPdfReader
, noPdfWriter
. The targetPdfDocument
instance is generated with aPdfWriter
. If the target also is generated with aPdfReader
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.