i'm trying to find a way to get only the plain text contained in a paragraph, and exclude the special objects contained in the para.
This is an example of my case.
I have this paragraph, obtained with app.activeDocument. :
paragraph , which contains 2 graphics before the plain text.
I achived this by doing:
var style = app.activeDocument.paragraphStyles.item("My Para Style");
app.findTextPreferences.appliedParagraphStyle = style;
var list = app.activeDocument.findText();
If I print the paragraph contents by list[0].paragraphs[0].contents, this is what is written: printed paragraph.
How can I extract and replace only the plain text from this paragraph leaving the first 2 objects?
Thanks
Try list[0].paragraphs[0].texts and loop through the array of texts to put it together
Related
I'm using itext 7.2.1 and I've this situation: I have a list of letters with variable content that normally fits in one page, but occasionally can span over two pages.
My goal is to force a new blank page after the "short" letters so they start alway on odd pages. The length of text is not know in advance.
Basically I have the following code:
PdfWriter writer = new PdfWriter("letters.pdf");
Document document = new Document(new PdfDocument(writer));
List<String> letters = . . .code to retrieve letters text;
int nLetter = 0;
for (String text : letters) {
Paragraph p = new Paragraph().add(text);
doc.add(p);
nLetter++;
//now after layout of last paragraph I must ensure that
//the next paragraph starts on page (nLetter*2 + 1): how can I do this?
}
. . .
doc.close();
What is the best way to do so in iText7? I tried with custom DocumentRenderer but I haven't found a clean and working solution.
The best and simpliest way to know if your next paragraph starts on the next page of the document is to get the root renderer of your document and get current area. And it will return the area(rectangle and page) where the end of the text is located.
doc.getRenderer().getCurrentArea();
I am completely new to PDF creation including MigraDoc. I have gotten this far, which is really close to what I want for now. My question is that the text string (myMessage) that I pass to the "bodyParagraph" is up to 100 lines long, which causes three pages to be created, which is good. However the first page's Top margin is slightly greater than the second and third pages. I have no idea of why...
Basically, I am trying to create every page the same. Same header, footer and the body to take the same space regardless of the number of lines in the "bodyParagraph" content. If I have taken the completely wrong approach I would be open to suggestions.
Also, if there is a good tutorial to point me to that would be great. I can't really find anything but samples. I have learned everything from the samples, but sections, paragraph, etc is all new to me and I would like to get a better understanding of what I've done.
public static Document CreateWorkOrderPDF2(Document document, string filename, string WorkOrderHeader, string myMessage)
{
Section section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.Letter;
section.PageSetup.StartingNumber = 1;
section.PageSetup.LeftMargin = 40;
//Sets the height of the top margin
section.PageSetup.TopMargin = 100;
section.PageSetup.RightMargin = 40;
section.PageSetup.BottomMargin = 40;
//MARGIN
HeaderFooter header = section.Headers.Primary;
header.Format.Font.Size = 16;
header.Format.Font.Color = Colors.DarkBlue;
MigraDoc.DocumentObjectModel.Shapes.Image headerImage = header.AddImage("../../Fonts/castorgate.regular.png");
headerImage.Width = "2cm";
Paragraph headerParagraph = section.AddParagraph();
headerParagraph = header.AddParagraph(WorkOrderHeader);
//BODY PARAGRAPH
Paragraph bodyParagraph = section.AddParagraph();
bodyParagraph = section.AddParagraph(myMessage);
bodyParagraph.Format.Font.Size = 10;
bodyParagraph.Format.Font.Color = Colors.DarkRed;
//paragraph.Format.Distancne = "3cm";
Paragraph renderDate = section.AddParagraph();
renderDate = section.AddParagraph("Work Order Generated: ");
renderDate.AddDateField();
return document;
}
The line Paragraph bodyParagraph = section.AddParagraph(); adds an empty paragraph. I assume that is the extra space on the first page.
Same issue with renderDate in the following code block.
Just remove the calls section.AddParagraph() to remove the empty paragraphs if you don't want them.
MigraDoc is much like Word and understanding sections, paragraphs, &c. in Word will also help you with MigraDoc. That knowledge along with the samples and IntelliSense should get you going.
You can use MigraDoc to create an RTF file, open the RTF in Word, and click the pilcrow to show formatting characters in Word.
I want to generate a barcode.png file but the file generated contains the Barcode number also and I don't want that, I only want image without Text.
How to do that?
Below is my code:-
Barcode barcode3;
barcode3 = BarcodeFactory.createCode128("CODE128x1");
barcode3.setResolution(300);
BarcodeImageHandler.savePNG(barcode3, new File("Code128-1.png"));`
The text will not generate in case you pass the null parameter to setFont function of Barcode class. Your code looks like as below
Barcode barcode3;
barcode3 = BarcodeFactory.createCode128("CODE128x1");
barcode3.setFont(null);
barcode3.setResolution(300);
BarcodeImageHandler.savePNG(barcode3, new File("Code128-1.png"));
only adding this to your code :
barcode3.setDrawingText(false);
the method above will remove text from the barcode. full code
Barcode barcode3;
barcode3 = BarcodeFactory.createCode128("CODE128x1");
barcode3.setDrawingText(false);
barcode3.setResolution(300);
BarcodeImageHandler.savePNG(barcode3, new File("Code128-1.png"));
I have the code:
img = f.read.scan(/<img/)
img = img.length
links = f.read.scan(/<a/)
links = links.length
div = f.read.scan(/<div/)
div = div.length
The program opens a link, say http://stackoverflow.com. It then prints img, links, and div. For some reason, no matter what website I choose, it returns 0 for links and div, but returns the correct number for img. Why is this?
Cause when you read file then you also move pointer. Write it that way (I also added method chains):
content = f.read
img = content.scan(/<img/).length
links = content.scan(/<a/).length
div = content.scan(/<div/).length
f.read reads the whole file on the first go, so the second and third matches get an empty string to scan for tags on, and you get zero matches. See http://www.ruby-doc.org/core-1.9.3/IO.html#method-i-read:
If length is omitted or is nil, it reads until EOF and the encoding conversion is applied. It returns a string even if EOF is met at beginning.
You might reposition the input pointer after the first read back to the beginning, but that'll only work for files, so basically read the whole data to a buffer, and then use the scanning on that. See #Hauleth's answer for an example.
I'm currently adding a custom marker to tt_news. I want to keep the design in the template. But if the markers are empty I get <br /><h2></h2> and so on. This only makes sense if there is a content. Currently I have the logic and the formatting in class.tx_tt_news.php. Is there another way?
From what I read you put <h2>###GENERIC_MARKER###</h2> into the template. You better wrap the content in typoscript an set required=1. In this case the wrap will only be rendered when there is content. Example:
marker = TEXT
marker.field = title
marker.wrap = <h2>|</h2>
marker.required = 1