c# - iText 7按自定义名称重新标记c#
问题描述
我在这里找到了这个源代码。我的问题是如何通过没有数字的自定义名称重命名标签。我认为这段代码的行是相似的,但没有数字是我的目的:
pdfDoc.GetPage(4).SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, "Custom-", 2);
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Objects
{
public class PageLabels
{
public static readonly string DEST = "results/sandbox/objects/page_labels.pdf";
public static void Main(string[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new PageLabels().ManipulatePdf(DEST);
}
protected void ManipulatePdf(string dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfViewerPreferences viewerPreferences = new PdfViewerPreferences();
viewerPreferences.SetPrintScaling(PdfViewerPreferences.PdfViewerPreferencesConstants.NONE);
pdfDoc.GetCatalog().SetPageMode(PdfName.UseThumbs);
pdfDoc.GetCatalog().SetPageLayout(PdfName.TwoPageLeft);
pdfDoc.GetCatalog().SetViewerPreferences(viewerPreferences);
doc.Add(new Paragraph("Hello World"));
doc.Add(new Paragraph("Hello People"));
doc.Add(new AreaBreak());
PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
// Add the text to the direct content, but not in the right order
PdfCanvas canvas = new PdfCanvas(pdfDoc.GetPage(2));
canvas.BeginText();
canvas.SetFontAndSize(font, 12);
canvas.MoveText(88.66f, 788);
canvas.ShowText("ld");
canvas.MoveText(-22f, 0);
canvas.ShowText("Wor");
canvas.MoveText(-15.33f, 0);
canvas.ShowText("llo");
canvas.MoveText(-15.33f, 0);
canvas.ShowText("He");
canvas.EndText();
PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(250, 25));
new PdfCanvas(formXObject, pdfDoc)
.BeginText()
.SetFontAndSize(font, 12)
.MoveText(0, 7)
.ShowText("Hello People")
.EndText();
canvas.AddXObjectAt(formXObject, 36, 763);
pdfDoc.SetDefaultPageSize(new PageSize(PageSize.A4).Rotate());
doc.Add(new AreaBreak());
doc.Add(new Paragraph("Hello World"));
pdfDoc.SetDefaultPageSize(new PageSize(842, 595));
doc.Add(new AreaBreak());
doc.Add(new Paragraph("Hello World"));
pdfDoc.SetDefaultPageSize(PageSize.A4);
doc.Add(new AreaBreak());
pdfDoc.GetLastPage().SetCropBox(new Rectangle(10, 70, 525, 755));
doc.Add(new Paragraph("Hello World"));
doc.Add(new AreaBreak());
pdfDoc.GetLastPage().GetPdfObject().Put(PdfName.UserUnit, new PdfNumber(5));
doc.Add(new Paragraph("Hello World"));
doc.Add(new AreaBreak());
pdfDoc.GetLastPage().SetArtBox(new Rectangle(36, 36, 523, 770));
Paragraph p = new Paragraph("Hello ")
.Add(new Link("World", PdfAction.CreateURI("http://maps.google.com")));
doc.Add(p);
PdfAnnotation a = new PdfTextAnnotation(
new Rectangle(36, 755, 30, 30))
.SetTitle(new PdfString("Example"))
.SetContents("This is a post-it annotation");
pdfDoc.GetLastPage().AddAnnotation(a);
//Set page's label here:
pdfDoc.GetPage(1).SetPageLabel(PageLabelNumberingStyle.UPPERCASE_LETTERS, null);
pdfDoc.GetPage(3).SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null);
pdfDoc.GetPage(4).SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, "Custom-", 2);
doc.Close();
}
}
}//
解决方案
对于SetPageLabel
方法,第一个参数(编号样式)和第二个参数(页面标签前缀)都可以为空,以便从页面标签样式中省略它们。
要为第 4 页设置不带编号的页面标签:
pdfDoc.GetPage(4).SetPageLabel(null, "Label without numbering", 2);
推荐阅读
- php - php文本文件到数组数组
- java - TestNG Listener onFinish(ITestContext context) 无法获取 ThreadLocal 实例(它返回 null)但 onTestFailure(ITestResult result) 工作正常
- angular - Angular:当我通过单击对话框外部关闭 Mat 对话框时,如何定义默认值
- sql - 需要使用 pgrouting 的 Lat,long 形式的路径
- javascript - 如何比较2个数组的索引值?
- c++ - 如何在不知道他拥有哪些构造函数的情况下初始化属性?
- eclipse - Eclipse subversion checkout SSL 和 SSH 的区别
- python - 遍历目录中的文件会创建一个额外的 nan 数组
- android - 打开 CV 项目未构建
- javascript - X-Frame-Options:DENY 不适用于 Spring Boot REST API