简体   繁体   English

Itextsharp:添加新线型时如何避免替换现有线型?

[英]Itextsharp: How to avoid replacing existing line style when adding a new?

I'm generating a clothing label as pdfs using iText.我正在使用 iText 将服装 label 生成为 pdf。 I'm trying to add a dashed line as a folding line in every page.我正在尝试在每一页中添加一条虚线作为折叠线。 But when I added a dashed line existing strokes replace to dashed lines?但是当我添加一条虚线时,现有笔划会替换为虚线吗? Any idea how to stop this?知道如何阻止这种情况吗?

I tried adding dashed lines when creating the pdf and after creating the pdf.在创建 pdf 和创建 pdf 之后,我尝试添加虚线。 But none of those works.但这些都不起作用。

This is my code.这是我的代码。

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();
}

I expected to write dashed lines in every page in given ordinates.我希望在给定坐标的每一页上写虚线。 But it turned to replace straight lines to dashed lines.但它转而将直线替换为虚线。 Any idea how to add a lines without replacing existing?知道如何在不替换现有行的情况下添加行吗?

The line dash (just like the line width, fill and stroke colors, and many other properties) is part of the "pdf graphics state".虚线(就像线宽、填充和描边 colors 以及许多其他属性一样)是“pdf 图形状态”的一部分。

To get back an earlier graphics state, pdfs support a stack of graphics states.要取回较早的图形 state,pdf 支持一堆图形状态。 When the current state is what you want to get back to later, you push the current state on that stack.当您想要稍后返回当前 state 时,您将当前 state 推送到该堆栈上。 And when you want to get back to that state, you pop it off of that stack.当你想回到那个 state 时,你把它从那个堆栈中弹出。

The instruction for pushing is called save-state, the instruction for popping restore-state.推送指令称为save-state,弹出restore-state 指令。 The matching PdfContentByte methods are SaveState() and RestoreState() .匹配的PdfContentByte方法是 SaveState SaveState()RestoreState()

Thus, start with contentByte.SaveState() , then do your stuff, then end with contentByte.RestoreState() .因此,从contentByte.SaveState()开始,然后做你的事情,然后以contentByte.RestoreState()结束。


Add an aside, your code generates instructions in an invalid order:另外,您的代码以无效的顺序生成指令:

contentByte.MoveTo(moveto_x, moveto_y);
contentByte.LineTo(lineto_x, lineto_y);
contentByte.SetLineWidth(lineWidth);
contentByte.SetLineDash(unitsOn, unitsOff, phase);
contentByte.Stroke();

Here you first create a path, then set line width and line dash, and then stroke the path.这里首先创建一个路径,然后设置线宽和虚线,然后描边路径。 Strictly speaking, though, between the creation of a path and the instruction drawing it there may at most be an instruction to combine the path with the clip path but nothing else.但是,严格来说,在创建路径和绘制它的指令之间最多可能有一条指令将路径与剪辑路径结合起来,但没有别的。

Most pdf viewers don't insist on that, though, so you'll probably not have concrete problems because of that.不过,大多数 pdf 观众并不坚持这一点,因此您可能不会因此而遇到具体问题。

But if your pdfs are validated, this invalid order may be reported as error.但是,如果您的 pdf 文件经过验证,则此无效订单可能会报告为错误。

Thus, first set the parameters, then create the path and draw it.因此,首先设置参数,然后创建路径并绘制它。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM