Inset an image into an Open Office Draw document using Basic - openoffice-basic

I am wanting to insert an image into a LibreOffice Draw document using Basic. I create a Draw document and then I add a page to the document and then rename the pages. I want ti insert an image into Page1 and then add another Image to Page2. I have the pages created as I want but I am unable to insert the image into the page. Below is my code
sub InsertImage()
Dim Doc As Object
Dim Page1 as Object
Dim Page2 as Object
Dim DocPath1 as String
DocPath1 = ConvertToURL("MyImage1.jpg")
Dim DocPath2 as String
DocPath2 = ConvertToURL("MyImage2.jpg")
Dim noArgs() 'An empty array for the arguments
Dim sURL As String
sURL = "private:factory/sdraw"
Doc = StarDesktop.LoadComponentFromUrl(sURL, "_blank", 0, noArgs())
Page1 = Doc.DrawPages(0)
Page1.Name = "Image1"
Page2 = Doc.Drawpages.insertNewByIndex(2)
Page2.Name = "Image2"
' Page1.FillStyle = com.sun.star.drawing.FillStyle.BITMAP
' Page1.FillBitmapURL = DocPath1
End sub
I have been reading Andrew Pitonyak's book but unable to find a source for what I am trying to do. FillStyle breaks the code.

I have got the program to work.
I created two pdf's and saved them in a folder. I run the macro MergePDF and the sub opens Draw, creates the two pages, copes the pdf's onto each page. The document is ready to ExportToPDF which is my next task. Draw must be closed for this to work.
sub MergePDF()
Dim Doc As Object 'This workbook
Dim NewWorkBookURL As String
NewWorkBookURL = "private:factory/sdraw"
Dim noArgs() 'An empty array for the arguments
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
Point.x = 0
Point.y = 0
'A4
Size.Width = 21000
Size.Height = 29700
Dim Page1 As Object 'Excel sheet
Dim Page2 As Object 'AutoCAD sheet
Dim Image1 As Object 'PDF1
Dim Image2 As Object 'PDF2
Dim DocPath1 As String
Dim DocPath2 As String
Dim DocPath3 As String
DocPath1 = ConvertToURL("C:\Users\pdf1.pdf")
DocPath2 = ConvertToURL("C:\Users\\pdf2.pdf")
Doc = StarDesktop.LoadComponentFromUrl(NewWorkBookURL, "_blank", 0, noArgs())
Page1 = Doc.DrawPages(0)
Page1.Name = "PDF1"
Page2 = Doc.Drawpages.insertNewByIndex(2)
Page2.Name = "PDF2"
'Page 1
Image1 = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Image1.GraphicURL = DocPath1
Image1.Size = Size
Image1.Position = Point
Page1.add(Image1)
'Page 2
Image2 = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Image2.GraphicURL = DocPath2
Image2.Size = Size
Image2.Position = Point
Page2.add(Image2)
'ExportToPDF
'To Do
msgbox "Done"
End sub
Very basic but works. Thanks for the other forum members for the advice on the way.

Related

iTextSharp: Trying to make TextField transparent: PdfContentByte is nothing

I want to add textfields to my PDF.
These textfields' backgrounds should be transparent.
I have found an example on the web that showed how to do this the following way:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TransparencyPDF.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfGState gs1 = new PdfGState();
gs1.setFillOpacity(0.5f);
cb.setGState(gs1);
However, in my code, I don't have a PDFWriter. I have a PDFStamper.
But its properties look perfectly the same, so I adopt it like this:
Dim cb As PdfContentByte = stamper.GetOverContent(0)
Dim gs As New PdfGState
gs.FillOpacity = 0.5
cb.SetGState(gs)
The last line throws the error "System.NullReferenceException: cb is nothing."
What am I doing wrong here?
Thank you.
This is my entire code:
Dim stamper As PdfStamper = New PdfStamper(New PdfReader(sInputFile), File.Create(sOutputFile))
Dim iPageNumer As Integer = 1
Dim tf As TextField
tf = New TextField(stamper.Writer, New iTextSharp.text.Rectangle(iLowerLeftX, iLowerLeftY, iUpperRightX, iUpperRightY), n.Name)
Dim bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, False)
With tf
.Alignment = Element.ALIGN_LEFT And Element.ALIGN_MIDDLE ' Element.ALIGN_CENTER And Element.ALIGN_MIDDLE
.BackgroundColor = GrayColor.WHITE
.BorderColor = Color.RED
.BorderStyle = PdfBorderDictionary.STYLE_SOLID
.DefaultText = "This is a new text field."
.Font = bf
.FontSize = 7
.MaxCharacterLength = 25
.Options = TextField.BORDER_WIDTH_MEDIUM ' TextField.REQUIRED Or TextField.MULTILINE
.Rotation = 0 '90
.Text = "" 'This is the assigned value."
End With
stamper.AddAnnotation(tf.GetTextField(), iPageNumer)
Dim cb As PdfContentByte = stamper.GetOverContent(0)
Dim gs As New PdfGState
gs.FillOpacity = 0.5
cb.SetGState(gs) 'This line throws the error. cb is nothing. Why?
stamper.Close()
The problem with
Dim cb As PdfContentByte = stamper.GetOverContent(0)
is that it requests the OverContent of page 0 while page numbering in iText starts at 1. Thus, use
Dim cb As PdfContentByte = stamper.GetOverContent(1)
instead.

How to add text on desired coordinates on new word file using openxml

I want to create .docx file using openxml and add text on desired coordinates(location) on each page of the file. Is there any way in openxml to adjust the text. I am using the following snippet:
WordprocessingDocument doc = WordprocessingDocument.Create("E:\\test11.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
{
MainDocumentPart mainPart = doc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
ParagraphProperties oParagraphProperties = para.AppendChild(new ParagraphProperties());
Run run = para.AppendChild(new Run());
Text tt = new Text(str);
run.AppendChild(tt);
RunProperties runProp = new RunProperties(); // Create run properties.
RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
Bold bold = new Bold();
DocumentFormat.OpenXml.Wordprocessing.Color Color1 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };
Italic ita = new Italic();
runProp.Append(bold);
runProp.Append(Color1);
runProp.Append(ita);
FontSize size = new FontSize();
size.Val = new StringValue((fontSize * 2).ToString()); // 48 half-point font size
runProp.Append(runFont);
runProp.Append(size);
run.PrependChild<RunProperties>(runProp);
}
Using this I was able to add text on .docx file, but not on desired coordinate location. Can someone help with this?
Thanks.
I found a way to add text to a coordinate on the page of a Word file. I started with your generated Word file and using Word, I added a simple TextBox (Insert->Text->TextBox). I generated the code for the added TextBox using the Productivity Tool. (Note: as of this writing, the latest version of the SDK is now 2.5, which is recommended for this to work).
Add the following method to your class above:
private static void PlaceTextAtCoordinate(Paragraph para, string text, double xCoordinate, double uCoordinate)
{
var picRun = para.AppendChild(new Run());
Picture picture1 = picRun.AppendChild(new Picture());
Shapetype shapetype1 = new Shapetype() { Id = "_x0000_t202", CoordinateSize = "21600,21600", OptionalNumber = 202, EdgePath = "m,l,21600r21600,l21600,xe" };
Stroke stroke1 = new Stroke() { JoinStyle = StrokeJoinStyleValues.Miter };
Path path1 = new Path() { AllowGradientShape = true, ConnectionPointType = ConnectValues.Rectangle };
shapetype1.Append(stroke1);
shapetype1.Append(path1);
Shape shape1 = new Shape() { Id = "Text Box 2", Style = string.Format("position:absolute;margin-left:{0:F1}pt;margin-top:{1:F1}pt;width:187.1pt;height:29.7pt;z-index:251657216;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal-relative:text;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top", xCoordinate, uCoordinate), Stroked = false };
TextBox textBox1 = new TextBox() { Style = "mso-fit-shape-to-text:t" };
TextBoxContent textBoxContent1 = new TextBoxContent();
Paragraph paragraph2 = new Paragraph();
Run run2 = new Run();
Text text2 = new Text();
text2.Text = text;
run2.Append(text2);
paragraph2.Append(run2);
textBoxContent1.Append(paragraph2);
textBox1.Append(textBoxContent1);
TextWrap textWrap1 = new TextWrap() { Type = WrapValues.Square };
shape1.Append(textBox1);
shape1.Append(textWrap1);
picture1.Append(shapetype1);
picture1.Append(shape1);
}
The following usings were found in my class - your list may be different - but I wanted to detail them here just in case.
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Vml;
using DocumentFormat.OpenXml.Vml.Office;
using DocumentFormat.OpenXml.Vml.Wordprocessing;
using DocumentFormat.OpenXml.Wordprocessing;
Finally, add the following 2 calls to the very end of your method above:
PlaceTextAtCoordinate(para, "Text at 90.1,90.1", 90.1, 90.1);
PlaceTextAtCoordinate(para, "Text at 120.5,120.5", 120.1, 120.1);
and your Word Doc will look like the following:

Access innerHTML of an anchor tag

I'm having trouble accessing some innerHTML that I need to use as a title for a menu bar:
See the attached image for details...
The title "AMRIS" is what I need to store to a javascript variable as a string.
I will later be using the following code to fill a Bootstrap menu with the title I grab:
echo '<script type="text/javascript">
var subTitle = document.getElementById("mobMenuTitle2");
//This is currently filling the Sub Menu with a title//
subTitle.innerHTML = "Sub Test";
</script>';
RIGHT-CLICK > OPEN IMAGE IN NEW TAB TO SEE FULL SIZE
If you need the html of opend item, try like this
var openedList = document.getElementsByClassName('opened');
var menuLink = openedList[0].getElementsByClassName('menu-link');
var anchorText = menuLink[0].innerHTML;
Instead if you want to retrieve the html of list X (X is a numer 0 based)
var accordion = document.getElementsByClassName('accordion-menu');
var list = accordion[0].getElementsByTagName('li');
var menuLink = list[X].getElementsByClassName('menu-link');
var anchorText = menuLink[0].innerHTML;
var opn = $(".accordion-menu.opened.menu-link").context.title;
var opnr = opn.replace("MagLab", "");
var subTitle = document.getElementById("mobMenuTitle2")
subTitle.innerHTML = opnr;

Using iTextSharp v4.1.2, how do I add an image to the header?

I can add a text header in iTextSharp v4.1.2 but how do I add an image instead of text? I am currently using a text header but I would like to replace it with an image.
' Create a Document object
Dim doc As Document = New Document(PageSize.LETTER)
doc.SetMargins(pageMarginLeft, pageMarginRight, pageMarginTopBottom, pageMarginTopBottom)
' Create a new PdfWriter object, specifying the output stream
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream)
' Set header and footer
Dim headerFont As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, iTextSharp.text.Color.BLUE)
Dim header As New HeaderFooter(New Paragraph("VOLUNTEER MASTER SCHEDULE", headerFont), False)
Dim footerFont As iTextSharp.text.Font = FontFactory.GetFont("Arial", 8, iTextSharp.text.Color.BLACK)
Dim footer As New HeaderFooter(New Phrase("Page: ", footerFont), True)
doc.Header = header
doc.Footer = footer
' Open the Document for writing
doc.Open()

ITextSharp, Assigning a font to List within a cell?

Is there a way to assign a font to a list that is added to table cell in ITextsharp. I'm sure it must be straightforward but I'm missing it.
Dim tblSignature As New PdfPTable(1)
tblSignature.WidthPercentage = 90.0F
Dim _baseFont As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED)
Dim _bFont As New Font(_baseFont, 4, Font.ITALIC, Color.RED)
Dim cell As New PdfPCell(New Phrase(TheItem.Value, _bFont))
TheParagraph.Font = _bFont
Dim list As New List(list.UNORDERED, 25.0F)
list.SetListSymbol("[ ]")
If ListItems.Count > 0 Then
For Each ListItem In .ListItems
//I have tried adding as chunk/phrase and applying font but no joy
//Item not added to cell
//list.Add(New Chunk(ListItem, _bFont))
list.Add(ListItem)
Next
list.IndentationLeft = 5.0F
cell.AddElement(list)
End If
cell.Colspan = 6
cell.HorizontalAlignment = 0
cell.PaddingBottom = 5.0F
cell.PaddingLeft = 5.0F
cell.PaddingRight = 5.0F
'' add to doc
tblSignature.AddCell(cell)
TheParagraph.Add(tblSignature)
Does anyone know How I'd change this so that the list/cell has the specific font I've set at the top.
Cheers
The iTextSharp.text.ListItem object has an iTextSharp.text.Font property you can set.
Please note that declarations such as new PdfPCell or new Phrase will not work with iTextSharp.text.List's add method.But both of them fairly work with PdfPTable.
I used iTextSharp.text.List for one of my software's pdf print out in the following ways.I hope this may be useful to you also.
This is a program which is used in one of my software developed with vb.net(visual studio 2010 and windows 7 platform) and iTextSharp latest version.
for pdf print out.It demonstrates how we can manupulate iTextSharp.text.List's font properties.
Dim doc1 As New Document()
Dim pathpdf As String = "C:/temp"
'path = ServerMapPath("PDFs");
PdfWriter.GetInstance(doc1, New FileStream(pathpdf + "/Doc1.pdf", FileMode.Create))
' Now to begin actually working with the document, open it, , add a new list,and, then close it:
doc1.Open()
Dim fnt1 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 8, iTextSharp.text.Font.NORMAL)
Dim fnt2 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD)
Dim fnt3 As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.NORMAL)
Dim lst3 As New iTextSharp.text.List()
lst3.SetListSymbol("")
'add a list item in bold letters.
Dim m_DataHeadOnly As String
m_DataHeadOnly = "This is the itextsharp font setting with list"
lst3.Add(New iTextSharp.text.ListItem(m_DataHeadOnly, fnt2))
doc1.add(lst3)
doc1.close()