PdfFormXObject 是否会导致 itext7 中的行丢失?
Does PdfFormXObject cause lines to be lost in itext7?
使用itext7,当我放大pdf文件的页面大小时,我发现它导致可见范围外的元素丢失。下面的代码。
string cardPdf = "card.pdf";float cardWidth = 266.5f;float cardHeight = 164.4f;
using (PdfDocument cardDoc = new PdfDocument(new PdfWriter(cardPdf)))
using (Document doc = new Document(cardDoc))
{
PdfPage page = cardDoc.AddNewPage(new iText.Kernel.Geom.PageSize(cardWidth, cardHeight));
Paragraph carePara = new Paragraph("CARD").SetFontSize(5f);
doc.Add(carePara);
PdfCanvas canvas = new PdfCanvas(page);
canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
canvas.SetStrokeColor(ColorConstants.BLACK);
canvas.SetLineWidth(1f);
canvas.Rectangle(0, 0, cardWidth - 0.1f, cardHeight);
canvas.SetLineWidth(0.1f);
//Draw lines outside page visible area
canvas.MoveTo(0f, -10f);
canvas.LineTo(cardWidth, -10f);//----> a_line
canvas.MoveTo(0f, cardHeight + 10f);
canvas.LineTo(cardWidth, cardHeight + 10f);//----> b_line
canvas.Stroke();
}
//to enlarge pdf
using (PdfDocument destPdf = new PdfDocument(new PdfWriter("enlarged.pdf")))
using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(cardPdf)))
{
var page = destPdf.AddNewPage(new PageSize(cardWidth+40f,cardHeight+40f));
PdfFormXObject pageCopy = sourcePdf.GetFirstPage().CopyAsFormXObject(destPdf);
PdfCanvas canvas = new PdfCanvas(page);
canvas.AddXObjectAt(pageCopy,20f, 20f);//a_line and b_line are missing
}
PdfFormXObject 会导致行丢失吗?
有什么办法可以得到完整的页面吗?
在 mkl 的回放之后,下面的代码得到了我想要的东西。
//to enlarge pdf
using (PdfDocument destPdf = new PdfDocument(new PdfWriter("enlarged.pdf")))
using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(cardPdf)))
{
var page = destPdf.AddNewPage(new PageSize(cardWidth+40f,cardHeight+40));
var oriPage = sourcePdf.GetFirstPage();
var crop = oriPage.GetCropBox();
//set a new cropbox
oriPage.SetCropBox(new Rectangle(crop.GetX(), crop.GetY()-20f, crop.GetWidth(), crop.GetHeight()+40f));
PdfFormXObject pageCopy = oriPage.CopyAsFormXObject(destPdf);
PdfCanvas canvas = new PdfCanvas(page);
canvas.AddXObjectAt(pageCopy,20f, 0f);
}
使用itext7,当我放大pdf文件的页面大小时,我发现它导致可见范围外的元素丢失。下面的代码。
string cardPdf = "card.pdf";float cardWidth = 266.5f;float cardHeight = 164.4f;
using (PdfDocument cardDoc = new PdfDocument(new PdfWriter(cardPdf)))
using (Document doc = new Document(cardDoc))
{
PdfPage page = cardDoc.AddNewPage(new iText.Kernel.Geom.PageSize(cardWidth, cardHeight));
Paragraph carePara = new Paragraph("CARD").SetFontSize(5f);
doc.Add(carePara);
PdfCanvas canvas = new PdfCanvas(page);
canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
canvas.SetStrokeColor(ColorConstants.BLACK);
canvas.SetLineWidth(1f);
canvas.Rectangle(0, 0, cardWidth - 0.1f, cardHeight);
canvas.SetLineWidth(0.1f);
//Draw lines outside page visible area
canvas.MoveTo(0f, -10f);
canvas.LineTo(cardWidth, -10f);//----> a_line
canvas.MoveTo(0f, cardHeight + 10f);
canvas.LineTo(cardWidth, cardHeight + 10f);//----> b_line
canvas.Stroke();
}
//to enlarge pdf
using (PdfDocument destPdf = new PdfDocument(new PdfWriter("enlarged.pdf")))
using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(cardPdf)))
{
var page = destPdf.AddNewPage(new PageSize(cardWidth+40f,cardHeight+40f));
PdfFormXObject pageCopy = sourcePdf.GetFirstPage().CopyAsFormXObject(destPdf);
PdfCanvas canvas = new PdfCanvas(page);
canvas.AddXObjectAt(pageCopy,20f, 20f);//a_line and b_line are missing
}
PdfFormXObject 会导致行丢失吗? 有什么办法可以得到完整的页面吗?
在 mkl 的回放之后,下面的代码得到了我想要的东西。
//to enlarge pdf
using (PdfDocument destPdf = new PdfDocument(new PdfWriter("enlarged.pdf")))
using (PdfDocument sourcePdf = new PdfDocument(new PdfReader(cardPdf)))
{
var page = destPdf.AddNewPage(new PageSize(cardWidth+40f,cardHeight+40));
var oriPage = sourcePdf.GetFirstPage();
var crop = oriPage.GetCropBox();
//set a new cropbox
oriPage.SetCropBox(new Rectangle(crop.GetX(), crop.GetY()-20f, crop.GetWidth(), crop.GetHeight()+40f));
PdfFormXObject pageCopy = oriPage.CopyAsFormXObject(destPdf);
PdfCanvas canvas = new PdfCanvas(page);
canvas.AddXObjectAt(pageCopy,20f, 0f);
}