使用 itextsharp c# 在现有 PDF 上绘制圆圈
Drawing circle on existing PDF using itextsharp c#
我在现有 pdf 上绘制圆圈时遇到了一些问题,我找到了一个代码来在现有 PDF 上添加文本,我试图调整它以绘制圆圈,但结果只是一个空白页有谁知道如何解决这个问题?
我的代码:
string oldFile = @"C:\...6166-21.pdf";
string newFile = @"C:\...NEW.pdf";
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
cb.Circle(150f, 150f, 50f);
cb.SetColorStroke(iTextSharp.text.BaseColor.GREEN);
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
您忘记添加cb.Stroke();
请这样尝试:
cb.SetColorStroke(iTextSharp.text.BaseColor.GREEN);
cb.Circle(150f, 150f, 50f);
cb.Stroke();
我在现有 pdf 上绘制圆圈时遇到了一些问题,我找到了一个代码来在现有 PDF 上添加文本,我试图调整它以绘制圆圈,但结果只是一个空白页有谁知道如何解决这个问题?
我的代码:
string oldFile = @"C:\...6166-21.pdf";
string newFile = @"C:\...NEW.pdf";
// open the reader
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
cb.Circle(150f, 150f, 50f);
cb.SetColorStroke(iTextSharp.text.BaseColor.GREEN);
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
您忘记添加cb.Stroke();
请这样尝试:
cb.SetColorStroke(iTextSharp.text.BaseColor.GREEN);
cb.Circle(150f, 150f, 50f);
cb.Stroke();