[英]Itextsharp: How to avoid replacing existing line style when adding a new?
我正在使用 iText 将服装 label 生成为 pdf。 我正在尝试在每一页中添加一条虚线作为折叠线。 但是当我添加一条虚线时,现有笔划会替换为虚线吗? 知道如何阻止这种情况吗?
在创建 pdf 和创建 pdf 之后,我尝试添加虚线。 但这些都不起作用。
这是我的代码。
string inputPDF = "C:\\Users\\User\\Documents\\visual studio 2017\\Projects\\iTextSharpExample\\iTextSharpExample\\pdf\\Label_dynamicLive_SampleTemplate.pdf";
string outputPDF = "C:\\Users\\User\\Documents\\visual studio 2017\\Projects\\iTextSharpExample\\iTextSharpExample\\pdf\\Label_dynamicLive_SampleTemplate_foldline.pdf";
PdfReader reader = new PdfReader(inputPDF);
using (var fileStream = new FileStream(outputPDF, FileMode.Create, FileAccess.Write))
{
var document = new Document(reader.GetPageSizeWithRotation(1));
var writer = PdfWriter.GetInstance(document, fileStream);
document.Open();
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var importedPage = writer.GetImportedPage(reader, i);
var contentByte = writer.DirectContent;
//line start
float moveto_x = 0 + 1;
float lineto_x = 20 - 1;
float moveto_y = (110 / 2) + 5;
float lineto_y = (110 / 2) + 5;
float moveto_x2 = 0 + 1;
float lineto_x2 = 20 - 1;
float moveto_y2 = (110 / 2) - 5;
float lineto_y2 = (110 / 2) - 5;
float lineWidth = 0.5f;
float unitsOn = 5;
float unitsOff = 1;
float phase = 2;
moveto_x = iTextSharp.text.Utilities.MillimetersToPoints(moveto_x);
moveto_y = iTextSharp.text.Utilities.MillimetersToPoints(moveto_y);
contentByte.MoveTo(moveto_x, moveto_y);
lineto_x = iTextSharp.text.Utilities.MillimetersToPoints(lineto_x);
lineto_y = iTextSharp.text.Utilities.MillimetersToPoints(lineto_y);
contentByte.LineTo(lineto_x, lineto_y);
contentByte.SetLineWidth(lineWidth);
contentByte.SetLineDash(unitsOn, unitsOff, phase);
contentByte.Stroke();
//line end
contentByte.AddTemplate(importedPage, 0, 0);
}
document.Close();
writer.Close();
}
我希望在给定坐标的每一页上写虚线。 但它转而将直线替换为虚线。 知道如何在不替换现有行的情况下添加行吗?
虚线(就像线宽、填充和描边 colors 以及许多其他属性一样)是“pdf 图形状态”的一部分。
要取回较早的图形 state,pdf 支持一堆图形状态。 当您想要稍后返回当前 state 时,您将当前 state 推送到该堆栈上。 当你想回到那个 state 时,你把它从那个堆栈中弹出。
推送指令称为save-state,弹出restore-state 指令。 匹配的PdfContentByte
方法是 SaveState SaveState()
和RestoreState()
。
因此,从contentByte.SaveState()
开始,然后做你的事情,然后以contentByte.RestoreState()
结束。
另外,您的代码以无效的顺序生成指令:
contentByte.MoveTo(moveto_x, moveto_y);
contentByte.LineTo(lineto_x, lineto_y);
contentByte.SetLineWidth(lineWidth);
contentByte.SetLineDash(unitsOn, unitsOff, phase);
contentByte.Stroke();
这里首先创建一个路径,然后设置线宽和虚线,然后描边路径。 但是,严格来说,在创建路径和绘制它的指令之间最多可能有一条指令将路径与剪辑路径结合起来,但没有别的。
不过,大多数 pdf 观众并不坚持这一点,因此您可能不会因此而遇到具体问题。
但是,如果您的 pdf 文件经过验证,则此无效订单可能会报告为错误。
因此,首先设置参数,然后创建路径并绘制它。
contentByte.SaveState();
contentByte.SetLineWidth(lineWidth);
contentByte.SetLineDash(unitsOn, unitsOff, phase);
contentByte.MoveTo(moveto_x, moveto_y);
contentByte.LineTo(lineto_x, lineto_y);
contentByte.Stroke();
contentByte.RestoreState();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.