本文整理汇总了C#中iTextSharp.text.pdf.PdfLayer类的典型用法代码示例。如果您正苦于以下问题:C# PdfLayer类的具体用法?C# PdfLayer怎么用?C# PdfLayer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PdfLayer类属于iTextSharp.text.pdf命名空间,在下文中一共展示了PdfLayer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOCGOrder
private static void GetOCGOrder(PdfArray order, PdfLayer layer) {
if (!layer.OnPanel)
return;
if (layer.Title == null)
order.Add(layer.Ref);
List<PdfLayer> children = layer.Children;
if (children == null)
return;
PdfArray kids = new PdfArray();
if (layer.Title != null)
kids.Add(new PdfString(layer.Title, PdfObject.TEXT_UNICODE));
for (int k = 0; k < children.Count; ++k) {
GetOCGOrder(kids, children[k]);
}
if (kids.Size > 0)
order.Add(kids);
}
示例2: CreatePdf
// ---------------------------------------------------------------------------
public byte[] CreatePdf(bool on)
{
using (MemoryStream ms = new MemoryStream()) {
using (Document document = new Document()) {
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, ms);
writer.ViewerPreferences = PdfWriter.PageModeUseOC;
writer.PdfVersion = PdfWriter.VERSION_1_5;
// step 3
document.Open();
// step 4
PdfLayer layer = new PdfLayer("Do you see me?", writer);
layer.On = on;
BaseFont bf = BaseFont.CreateFont();
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
cb.SetFontAndSize(bf, 18);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
cb.BeginLayer(layer);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
cb.EndLayer();
cb.EndText();
}
return ms.ToArray();
}
}
示例3: CreateTitle
/**
* Creates a title layer. A title layer is not really a layer but a collection of layers
* under the same title heading.
* @param title the title text
* @param writer the <CODE>PdfWriter</CODE>
* @return the title layer
*/
public static PdfLayer CreateTitle(String title, PdfWriter writer) {
if (title == null)
throw new ArgumentNullException(MessageLocalization.GetComposedMessage("title.cannot.be.null"));
PdfLayer layer = new PdfLayer(title);
writer.RegisterLayer(layer);
return layer;
}
示例4: ApplyWaterMark
private static void ApplyWaterMark(string filePath)
{
Logger.LogI("ApplyWatermark -> " + filePath);
var watermarkedFile = Path.GetFileNameWithoutExtension(filePath) + "-w.pdf";
using (var reader1 = new PdfReader(filePath))
{
using (var fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (var stamper = new PdfStamper(reader1, fs))
{
var pageCount = reader1.NumberOfPages;
var layer = new PdfLayer("WatermarkLayer", stamper.Writer);
for (var i = 1; i <= pageCount; i++)
{
var rect = reader1.GetPageSize(i);
var cb = stamper.GetUnderContent(i);
cb.BeginLayer(layer);
cb.SetFontAndSize(BaseFont.CreateFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
var gState = new PdfGState {FillOpacity = 0.25f};
cb.SetGState(gState);
cb.SetColorFill(BaseColor.BLACK);
cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
"(c)2015 ScrapEra", rect.Width/2, rect.Height/2, 45f);
cb.EndText();
cb.EndLayer();
}
}
}
File.Delete(filePath);
}
示例5: Write
// ===========================================================================
public void Write(Stream stream)
{
// step 1
using (Document document = new Document()) {
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, stream);
writer.PdfVersion = PdfWriter.VERSION_1_6;
// step 3
document.Open();
// step 4
PdfContentByte cb = writer.DirectContent;
PdfLayer dog = new PdfLayer("layer 1", writer);
PdfLayer tiger = new PdfLayer("layer 2", writer);
PdfLayer lion = new PdfLayer("layer 3", writer);
PdfLayerMembership cat = new PdfLayerMembership(writer);
PdfVisibilityExpression ve1 = new PdfVisibilityExpression(
PdfVisibilityExpression.OR
);
ve1.Add(tiger);
ve1.Add(lion);
cat.VisibilityExpression = ve1;
PdfLayerMembership no_cat = new PdfLayerMembership(writer);
PdfVisibilityExpression ve2 = new PdfVisibilityExpression(
PdfVisibilityExpression.NOT
);
ve2.Add(ve1);
no_cat.VisibilityExpression = ve2;
cb.BeginLayer(dog);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("dog"),
50, 775, 0);
cb.EndLayer();
cb.BeginLayer(tiger);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("tiger"),
50, 750, 0);
cb.EndLayer();
cb.BeginLayer(lion);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("lion"),
50, 725, 0);
cb.EndLayer();
cb.BeginLayer(cat);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("cat"),
50, 700, 0);
cb.EndLayer();
cb.BeginLayer(no_cat);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
new Phrase("no cat"), 50, 700, 0);
cb.EndLayer();
}
}
示例6: LayerCheckTest1
public void LayerCheckTest1()
{
string filename = OUT + "LayerCheckTest1.pdf";
FileStream fos = new FileStream(filename, FileMode.Create);
Document document = new Document();
PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
writer.ViewerPreferences = PdfWriter.PageModeUseOC;
writer.PdfVersion = PdfWriter.VERSION_1_5;
document.Open();
PdfLayer layer = new PdfLayer("Do you see me?", writer);
layer.On = true;
BaseFont bf = BaseFont.CreateFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
cb.SetFontAndSize(bf, 18);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
cb.BeginLayer(layer);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
cb.EndLayer();
cb.EndText();
document.Close();
}
示例7: AddChild
/**
* Adds a child layer. Nested layers can only have one parent.
* @param child the child layer
*/
public void AddChild(PdfLayer child) {
if (child.parent != null)
throw new ArgumentException(MessageLocalization.GetComposedMessage("the.layer.1.already.has.a.parent", ((PdfString)child.Get(PdfName.NAME)).ToUnicodeString()));
child.parent = this;
if (children == null)
children = new ArrayList();
children.Add(child);
}
示例8: LayerCheckTest2
public void LayerCheckTest2() {
string filename = OUT + "LayerCheckTest2.pdf";
FileStream fos = new FileStream(filename, FileMode.Create);
Document document = new Document();
PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
writer.CreateXmpMetadata();
writer.ViewerPreferences = PdfWriter.PageModeUseOC;
writer.PdfVersion = PdfWriter.VERSION_1_5;
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfLayer nested = new PdfLayer("Nested layers", writer);
PdfLayer nested_1 = new PdfLayer("Nested layer 1", writer);
PdfLayer nested_2 = new PdfLayer("Nested layer 2", writer);
nested.AddChild(nested_1);
nested.AddChild(nested_2);
writer.LockLayer(nested_2);
cb.BeginLayer(nested);
Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers", font), 50, 775, 0);
cb.EndLayer();
cb.BeginLayer(nested_1);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1", font), 100, 800, 0);
cb.EndLayer();
cb.BeginLayer(nested_2);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2", font), 100, 750, 0);
cb.EndLayer();
FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read, FileShare.Read);
ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
iccProfileFileStream.Close();
writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
document.Close();
}
示例9: LayerCheckTest1
public void LayerCheckTest1() {
string filename = OUT + "LayerCheckTest1.pdf";
FileStream fos = new FileStream(filename, FileMode.Create);
Document document = new Document();
PdfWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_2B);
writer.CreateXmpMetadata();
writer.ViewerPreferences = PdfWriter.PageModeUseOC;
writer.PdfVersion = PdfWriter.VERSION_1_5;
document.Open();
PdfLayer layer = new PdfLayer("Do you see me?", writer);
layer.On = true;
BaseFont bf = BaseFont.CreateFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, true);
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
cb.SetFontAndSize(bf, 18);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Do you see me?", 50, 790, 0);
cb.BeginLayer(layer);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Peek-a-Boo!!!", 50, 766, 0);
cb.EndLayer();
cb.EndText();
FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read, FileShare.Read);
ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
iccProfileFileStream.Close();
writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
document.Close();
}
示例10: LayerStampingTest
public void LayerStampingTest() {
String outPdf = DestFolder + "out3.pdf";
PdfReader reader =
new PdfReader(TestResourceUtils.GetResourceAsStream(TestResourcesPath, "House_Plan_Final.pdf"));
PdfStamper stamper = new PdfStamper(reader, File.Create(outPdf));
PdfLayer logoLayer = new PdfLayer("Logos", stamper.Writer);
PdfContentByte cb = stamper.GetUnderContent(1);
cb.BeginLayer(logoLayer);
Image iImage = Image.GetInstance(TestResourceUtils.GetResourceAsStream(TestResourcesPath, "Willi-1.jpg"));
iImage.ScalePercent(24f);
iImage.SetAbsolutePosition(100, 100);
cb.AddImage(iImage);
cb.EndLayer();
stamper.Close();
Assert.Null(new CompareTool().CompareByContent(outPdf, TestResourceUtils.GetResourceAsTempFile(TestResourcesPath, "cmp_House_Plan_Final.pdf"), DestFolder, "diff_"));
}
示例11: Write
// ===========================================================================
public void Write(Stream stream)
{
// step 1
using (Document document = new Document()) {
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, stream);
writer.PdfVersion = PdfWriter.VERSION_1_5;
// step 3
document.Open();
// step 4
PdfLayer a1 = new PdfLayer("answer 1", writer);
PdfLayer a2 = new PdfLayer("answer 2", writer);
PdfLayer a3 = new PdfLayer("answer 3", writer);
a1.On = false;
a2.On = false;
a3.On = false;
BaseFont bf = BaseFont.CreateFont();
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
cb.SetFontAndSize(bf, 18);
cb.ShowTextAligned(Element.ALIGN_LEFT,
"Q1: Who is the director of the movie 'Paths of Glory'?", 50, 766, 0);
cb.ShowTextAligned(Element.ALIGN_LEFT,
"Q2: Who directed the movie 'Lawrence of Arabia'?", 50, 718, 0);
cb.ShowTextAligned(Element.ALIGN_LEFT,
"Q3: Who is the director of 'House of Flying Daggers'?", 50, 670, 0);
cb.EndText();
cb.SaveState();
cb.SetRGBColorFill(0xFF, 0x00, 0x00);
cb.BeginText();
cb.BeginLayer(a1);
cb.ShowTextAligned(Element.ALIGN_LEFT,
"A1: Stanley Kubrick", 50, 742, 0);
cb.EndLayer();
cb.BeginLayer(a2);
cb.ShowTextAligned(Element.ALIGN_LEFT,
"A2: David Lean", 50, 694, 0);
cb.EndLayer();
cb.BeginLayer(a3);
cb.ShowTextAligned(Element.ALIGN_LEFT,
"A3: Zhang Yimou", 50, 646, 0);
cb.EndLayer();
cb.EndText();
cb.RestoreState();
List<Object> stateOn = new List<Object>();
stateOn.Add("ON");
stateOn.Add(a1);
stateOn.Add(a2);
stateOn.Add(a3);
PdfAction actionOn = PdfAction.SetOCGstate(stateOn, true);
List<Object> stateOff = new List<Object>();
stateOff.Add("OFF");
stateOff.Add(a1);
stateOff.Add(a2);
stateOff.Add(a3);
PdfAction actionOff = PdfAction.SetOCGstate(stateOff, true);
List<Object> stateToggle = new List<Object>();
stateToggle.Add("Toggle");
stateToggle.Add(a1);
stateToggle.Add(a2);
stateToggle.Add(a3);
PdfAction actionToggle = PdfAction.SetOCGstate(stateToggle, true);
Phrase p = new Phrase("Change the state of the answers:");
Chunk on = new Chunk(" on ").SetAction(actionOn);
p.Add(on);
Chunk off = new Chunk("/ off ").SetAction(actionOff);
p.Add(off);
Chunk toggle = new Chunk("/ toggle").SetAction(actionToggle);
p.Add(toggle);
document.Add(p);
}
}
示例12: LockLayer
/**
* Use this method to lock an optional content group.
* The state of a locked group cannot be changed through the user interface
* of a viewer application. Producers can use this entry to prevent the visibility
* of content that depends on these groups from being changed by users.
* @param layer the layer that needs to be added to the array of locked OCGs
* @since 2.1.2
*/
public void LockLayer(PdfLayer layer) {
OCGLocked.Add(layer.Ref);
}
示例13: ReadOCProperties
/**
* Reads the OCProperties dictionary from the catalog of the existing document
* and fills the documentOCG, documentOCGorder and OCGRadioGroup variables in PdfWriter.
* Note that the original OCProperties of the existing document can contain more information.
* @since 2.1.2
*/
virtual protected void ReadOCProperties() {
if (documentOCG.Count != 0) {
return;
}
PdfDictionary dict = reader.Catalog.GetAsDict(PdfName.OCPROPERTIES);
if (dict == null) {
return;
}
PdfArray ocgs = dict.GetAsArray(PdfName.OCGS);
PdfIndirectReference refi;
PdfLayer layer;
Dictionary<string,PdfLayer> ocgmap = new Dictionary<string,PdfLayer>();
for (ListIterator<PdfObject> i = ocgs.GetListIterator(); i.HasNext();) {
refi = (PdfIndirectReference)i.Next();
layer = new PdfLayer(null);
layer.Ref = refi;
layer.OnPanel = false;
layer.Merge((PdfDictionary)PdfReader.GetPdfObject(refi));
ocgmap[refi.ToString()] = layer;
}
PdfDictionary d = dict.GetAsDict(PdfName.D);
PdfArray off = d.GetAsArray(PdfName.OFF);
if (off != null) {
for (ListIterator<PdfObject> i = off.GetListIterator(); i.HasNext(); ) {
refi = (PdfIndirectReference)i.Next();
layer = ocgmap[refi.ToString()];
layer.On = false;
}
}
PdfArray order = d.GetAsArray(PdfName.ORDER);
if (order != null) {
AddOrder(null, order, ocgmap);
}
foreach (PdfLayer o in ocgmap.Values)
documentOCG[o] = null;
OCGRadioGroup = d.GetAsArray(PdfName.RBGROUPS);
if (OCGRadioGroup == null)
OCGRadioGroup = new PdfArray();
OCGLocked = d.GetAsArray(PdfName.LOCKED);
if (OCGLocked == null)
OCGLocked = new PdfArray();
}
示例14: helper_PageEnd
/// <summary>
/// Event handler when the end of the page is reached
/// </summary>
void helper_PageEnd(object sender, PageEndEventArgs e)
{
writer.ViewerPreferences = PdfWriter.PageModeUseOC;
writer.PdfVersion = PdfWriter.VERSION_1_5;
PdfLayer layer = new PdfLayer("Pages", writer);
layer.On = false;
layer.PageElement = "HF";
layer.OnPanel = false;
layer.View = true;
under.BeginLayer(layer);
var th = new PdfStructureElement(root, PdfName.ARTIFACT);
th.Put(PdfName.TYPE, new PdfNull());
under.BeginMarkedContentSequence(th);
under.SaveState();
float bottom = .625f;
float textBase = document.Bottom - bottom.ToPts();
float textSize = 12;
under.BeginText();
under.SetFontAndSize(GrantFonts.bf_times, textSize);
float adjust = GrantFonts.bf_times.GetWidthPoint("0", textSize);
it.Rectangle pSize = document.PageSize;
float width = pSize.Width / 2;
under.SetTextMatrix(width - textSize - adjust, textBase);
under.ShowText(e.page);
under.EndText();
under.RestoreState();
under.EndMarkedContentSequence();
under.EndLayer();
}
示例15: ResetBackgroundColor
public static void ResetBackgroundColor(string originalFile, string watermarked, string color)
{
int page = 1;
PdfReader reader = new PdfReader(originalFile);
using (FileStream fs = new FileStream(watermarked, FileMode.Create, FileAccess.Write, FileShare.None))
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
PdfLayer layer = new PdfLayer("BackgroundColor", stamper.Writer);
Rectangle rect = reader.GetPageSize(page);
PdfContentByte cb = stamper.GetOverContent(page);
cb.BeginLayer(layer);
// set color
CMYKColor chosenColor;
CMYKColor green = new CMYKColor(0.0809f, 0f, 0.1915f, 0.0784f); //TO DO: get green color
// set template color
if (color.Equals("yellow", StringComparison.OrdinalIgnoreCase))
{
chosenColor = new CMYKColor(0f, 0.2092f, 0.7741f, 0.0627f);
}
else if (color.Equals("red", StringComparison.OrdinalIgnoreCase))
{
chosenColor = new CMYKColor(0f, 0.7564f, 0.7372f, 0.3882f);
}
else if (color.Equals("purple", StringComparison.OrdinalIgnoreCase))
{
chosenColor = new CMYKColor(0.5118f, 0.6693f, 0f, 0.5020f);
}
else
{
chosenColor = new CMYKColor(0f, 0f, 0f, 0f);
}
cb.SetColorFill(chosenColor);
cb.SetColorStroke(chosenColor);
// draw name label
double widthDiff = 23;
double startHeight = rect.Height - 3;
double midHeight = rect.Height - 18;
double endHeight = rect.Height - 27;
double startWidth = rect.Width * 1 / 3;
double firstMidWidth = startWidth + widthDiff;
double endWidth = rect.Width;
double secondMidWidth = endWidth - widthDiff;
cb.MoveTo(rect.Width * 1 / 3, startHeight);
cb.LineTo(rect.Width, startHeight);
cb.LineTo(rect.Width, midHeight);
cb.CurveTo(secondMidWidth + (endWidth - secondMidWidth) / 1.4, endHeight + (midHeight - endHeight) / 4, secondMidWidth, endHeight);
cb.LineTo(firstMidWidth, endHeight);
cb.CurveTo(firstMidWidth - (firstMidWidth - startWidth) / 1.4, endHeight + (midHeight - endHeight) / 4, startWidth, midHeight);
cb.ClosePathFillStroke();
// reset flag
cb.SetColorFill(green);
cb.SetColorStroke(green);
startHeight = endHeight - 0.5;
double heightDiff = 8.5;
widthDiff = 13;
cb.MoveTo(endWidth - widthDiff, startHeight);
cb.LineTo(endWidth, startHeight);
cb.LineTo(endWidth, startHeight - heightDiff);
cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
cb.ClosePathFillStroke();
startHeight = startHeight - heightDiff - 2;
cb.MoveTo(endWidth - widthDiff, startHeight);
cb.LineTo(endWidth, startHeight);
cb.LineTo(endWidth, startHeight - heightDiff);
cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
cb.ClosePathFillStroke();
startHeight = startHeight - heightDiff - 2;
cb.MoveTo(endWidth - widthDiff, startHeight);
cb.LineTo(endWidth, startHeight);
cb.LineTo(endWidth, startHeight - heightDiff);
cb.LineTo(endWidth - widthDiff, startHeight - heightDiff);
cb.ClosePathFillStroke();
double mealLabelStartHeight = rect.Height - 30;
double mealLabelHeight = 8;
double mealLabelStartWidth = rect.Width / 3;
double mealLabelWidth = 120;
cb.MoveTo(mealLabelStartWidth, mealLabelStartHeight);
cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight);
cb.LineTo(mealLabelStartWidth + mealLabelWidth, mealLabelStartHeight - mealLabelHeight);
cb.LineTo(mealLabelStartWidth, mealLabelStartHeight - mealLabelHeight);
cb.ClosePathFillStroke();
double macrosStartHeight = rect.Height - 55;
double macrosStartWidth = rect.Width / 3 + 5;
double macrosWidth = 20;
//.........这里部分代码省略.........